diff --git a/app/controllers/spree/products_controller_decorator.rb b/app/controllers/spree/products_controller_decorator.rb index aa9e6db748..c25ca0f02a 100644 --- a/app/controllers/spree/products_controller_decorator.rb +++ b/app/controllers/spree/products_controller_decorator.rb @@ -4,7 +4,15 @@ 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/models/spree/distributor.rb b/app/models/spree/distributor.rb index 29a17d7293..083e3fc2fc 100644 --- a/app/models/spree/distributor.rb +++ b/app/models/spree/distributor.rb @@ -8,6 +8,8 @@ module Spree validates :name, :pickup_address, :country_id, :state_id, :city, :post_code, :presence => true + scope :by_name, order('name') + after_initialize :initialize_country def initialize_country diff --git a/app/overrides/add_distributor_to_add_to_cart_form.rb b/app/overrides/add_distributor_to_add_to_cart_form.rb new file mode 100644 index 0000000000..0b0bd07ac6 --- /dev/null +++ b/app/overrides/add_distributor_to_add_to_cart_form.rb @@ -0,0 +1,4 @@ +Deface::Override.new(:virtual_path => "spree/products/_cart_form", + :replace => "[data-hook='product_price'] .add-to-cart", + :partial => "spree/products/add_to_cart", + :name => "product_add_to_cart") diff --git a/app/views/spree/products/_add_to_cart.html.haml b/app/views/spree/products/_add_to_cart.html.haml new file mode 100644 index 0000000000..3c6ef381a8 --- /dev/null +++ b/app/views/spree/products/_add_to_cart.html.haml @@ -0,0 +1,12 @@ +.add-to-cart + - if @product.has_stock? || Spree::Config[:allow_backorders] + %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) + %br/ + = button_tag :class => 'large primary', :id => 'add-to-cart-button', :type => :submit do + = t(:add_to_cart) + + - else + = content_tag('strong', t(:out_of_stock)) diff --git a/spec/requests/consumer/distributors_spec.rb b/spec/requests/consumer/distributors_spec.rb index c1b3747ba8..9c58d03f84 100644 --- a/spec/requests/consumer/distributors_spec.rb +++ b/spec/requests/consumer/distributors_spec.rb @@ -78,28 +78,21 @@ feature %q{ end context "viewing a product, it provides a choice of distributor when adding to cart" do - it "displays the local distributor as the default choice when available for the current product" - it "functions with remote distributors" + 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) + product = create(:product, :distributors => [distributor]) + + # When we select the distributor and view the product + visit spree.root_path + click_link distributor.name + visit spree.product_path(product) + + # Then we should see our distributor as the default option when adding the item to our cart + page.should have_selector "select#distributor_id option[value='#{distributor.id}'][selected='selected']" + end + + it "functions with remote distributors also" end end - - - - # scenario "browsing products by distributor" do - # # Given a product at each of two distributors - # d1 = create(:distributor) - # d2 = create(:distributor) - # p1 = create(:product, :distributors => [d1]) - # p2 = create(:product, :distributors => [d2]) - - # # When I go to the home page, I should see both products - # visit spree.root_path - # page.should have_content p1.name - # page.should have_content p2.name - - # # When I filter by one distributor, I should see only the product from that distributor - # click_link d1.name - # page.should have_content p1.name - # page.should_not have_content p2.name - # end end