diff --git a/app/controllers/spree/products_controller_decorator.rb b/app/controllers/spree/products_controller_decorator.rb new file mode 100644 index 0000000000..aa9e6db748 --- /dev/null +++ b/app/controllers/spree/products_controller_decorator.rb @@ -0,0 +1,10 @@ +require 'open_food_web/split_products_by_distributor' + +Spree::ProductsController.class_eval do + include Spree::DistributorsHelper + include OpenFoodWeb::SplitProductsByDistributor + + respond_override :index => { :html => { :success => lambda { + @products, @products_local, @products_remote = split_products_by_distributor @products, current_distributor + } } } +end diff --git a/app/controllers/spree/taxons_controller_decorator.rb b/app/controllers/spree/taxons_controller_decorator.rb new file mode 100644 index 0000000000..a6aacc38a1 --- /dev/null +++ b/app/controllers/spree/taxons_controller_decorator.rb @@ -0,0 +1,10 @@ +require 'open_food_web/split_products_by_distributor' + +Spree::TaxonsController.class_eval do + include Spree::DistributorsHelper + include OpenFoodWeb::SplitProductsByDistributor + + respond_override :show => { :html => { :success => lambda { + @products, @products_local, @products_remote = split_products_by_distributor @products, current_distributor + } } } +end diff --git a/app/overrides/split_products_by_distributor.rb b/app/overrides/split_products_by_distributor.rb index 7f78d1eb3a..7e57b1909a 100644 --- a/app/overrides/split_products_by_distributor.rb +++ b/app/overrides/split_products_by_distributor.rb @@ -1,4 +1,19 @@ Deface::Override.new(:virtual_path => "spree/home/index", :replace => "[data-hook='homepage_products']", :partial => "spree/shared/products_by_distributor", - :name => "products") + :name => "products_home") + +Deface::Override.new(:virtual_path => "spree/products/index", + :replace => "[data-hook='homepage_products']", + :partial => "spree/shared/products_by_distributor", + :name => "products_products") + +Deface::Override.new(:virtual_path => "spree/products/index", + :replace => "[data-hook='search_results']", + :partial => "spree/shared/products_by_distributor", + :name => "products_search") + +Deface::Override.new(:virtual_path => "spree/taxons/show", + :replace => "[data-hook='taxon_products']", + :partial => "spree/shared/products_by_distributor", + :name => "products_taxon") diff --git a/app/views/spree/shared/_products_by_distributor.html.haml b/app/views/spree/shared/_products_by_distributor.html.haml index 86ae4fda0f..96b0f1973d 100644 --- a/app/views/spree/shared/_products_by_distributor.html.haml +++ b/app/views/spree/shared/_products_by_distributor.html.haml @@ -1,5 +1,5 @@ - if @products - #products= render 'spree/shared/products', :products => @products + #products= render 'spree/shared/products', :products => @products, :taxon => @taxon - else - #products-local= render 'spree/shared/products', :products => @products_local - #products-remote= render 'spree/shared/products', :products => @products_remote + #products-local= render 'spree/shared/products', :products => @products_local, :taxon => @taxon + #products-remote= render 'spree/shared/products', :products => @products_remote, :taxon => @taxon diff --git a/spec/requests/consumer/distributors_spec.rb b/spec/requests/consumer/distributors_spec.rb index c6ef0cbd79..c1b3747ba8 100644 --- a/spec/requests/consumer/distributors_spec.rb +++ b/spec/requests/consumer/distributors_spec.rb @@ -36,23 +36,32 @@ feature %q{ end it "splits the product listing by local/remote distributor" do - # Given two distributors, with a product under each + # Given two distributors, with a product under each, and each product under a taxon + taxonomy = Spree::Taxonomy.find_by_name('Products') || create(:taxonomy, :name => 'Products') + taxonomy_root = taxonomy.root + taxon = create(:taxon, :name => 'Taxon one', :parent_id => taxonomy_root.id) d1 = create(:distributor) d2 = create(:distributor) - p1 = create(:product, :distributors => [d1]) - p2 = create(:product, :distributors => [d2]) + p1 = create(:product, :distributors => [d1], :taxons => [taxon]) + p2 = create(:product, :distributors => [d2], :taxons => [taxon]) # When I select the first distributor visit spree.root_path click_link d1.name # Then I should see products split by local/remote distributor - page.should_not have_selector '#products' - page.should have_selector '#products-local', :text => p1.name - page.should have_selector '#products-remote', :text => p2.name - - # TODO: Also see on products page and taxon page + # on the home page, the products page, the search results page and the taxon page + [spree.root_path, + spree.products_path, + spree.products_path(:keywords => 'Product'), + spree.nested_taxons_path(taxon.permalink) + ].each do |path| + visit path + page.should_not have_selector '#products' + page.should have_selector '#products-local', :text => p1.name + page.should have_selector '#products-remote', :text => p2.name + end end it "allows the user to leave the distributor" do