diff --git a/app/controllers/spree/products_controller_decorator.rb b/app/controllers/spree/products_controller_decorator.rb index c25ca0f02a..e7964028e9 100644 --- a/app/controllers/spree/products_controller_decorator.rb +++ b/app/controllers/spree/products_controller_decorator.rb @@ -4,15 +4,8 @@ Spree::ProductsController.class_eval do include Spree::DistributorsHelper include OpenFoodWeb::SplitProductsByDistributor - before_filter :load_distributors, :only => :show - respond_override :index => { :html => { :success => lambda { @products, @products_local, @products_remote = split_products_by_distributor @products, current_distributor } } } - - def load_distributors - @distributors = Spree::Distributor.by_name - end - end diff --git a/app/views/spree/products/_add_to_cart.html.haml b/app/views/spree/products/_add_to_cart.html.haml index 3c6ef381a8..bfe44578ac 100644 --- a/app/views/spree/products/_add_to_cart.html.haml +++ b/app/views/spree/products/_add_to_cart.html.haml @@ -3,7 +3,7 @@ %p Quantity = number_field_tag (@product.has_variants? ? :quantity : "variants[#{@product.master.id}]"), 1, :class => 'title', :in => 1..@product.on_hand %p Distributor - = select_tag "distributor_id", options_from_collection_for_select(@distributors, "id", "name", current_distributor.andand.id) + = select_tag "distributor_id", options_from_collection_for_select(@product.distributors, "id", "name", current_distributor.andand.id) %br/ = button_tag :class => 'large primary', :id => 'add-to-cart-button', :type => :submit do = t(:add_to_cart) diff --git a/spec/requests/consumer/distributors_spec.rb b/spec/requests/consumer/distributors_spec.rb index 9c58d03f84..69942f4e07 100644 --- a/spec/requests/consumer/distributors_spec.rb +++ b/spec/requests/consumer/distributors_spec.rb @@ -78,6 +78,19 @@ feature %q{ end context "viewing a product, it provides a choice of distributor when adding to cart" do + it "works when no distributor is chosen" do + # Given a distributor and a product under it + distributor = create(:distributor) + product = create(:product, :distributors => [distributor]) + + # When we view the product + visit spree.product_path(product) + + # Then we should see a choice of distributor, with no default + page.should have_selector "select#distributor_id option", :text => distributor.name + page.should_not have_selector "select#distributor_id option[selected='selected']" + end + it "displays the local distributor as the default choice when available for the current product" do # Given a distributor and a product under it distributor = create(:distributor) @@ -92,7 +105,23 @@ feature %q{ page.should have_selector "select#distributor_id option[value='#{distributor.id}'][selected='selected']" end - it "functions with remote distributors also" + it "works when viewing a product from a remote distributor" do + # Given two distributors and a product under one + distributor_product = create(:distributor) + distributor_no_product = create(:distributor) + product = create(:product, :distributors => [distributor_product]) + + # When we select the distributor without the product and then view the product + visit spree.root_path + click_link distributor_no_product.name + visit spree.product_path(product) + + # Then we should see a choice of distributor, + # with no default and no option for the distributor that the product does not belong to + page.should have_selector "select#distributor_id option", :text => distributor_product.name + page.should_not have_selector "select#distributor_id option", :text => distributor_no_product.name + page.should_not have_selector "select#distributor_id option[selected='selected']" + end end end end