From 9258d47e5546387a0d2ba97c71005393e6bed2a4 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Tue, 26 Jun 2012 07:52:45 +1000 Subject: [PATCH] When adding subsequent products, add valid products to cart --- app/models/spree/order_decorator.rb | 2 +- .../spree/products/_add_to_cart.html.haml | 1 + spec/models/order_spec.rb | 4 +++- spec/requests/consumer/add_to_cart_spec.rb | 20 +++++++++++++++++++ 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/app/models/spree/order_decorator.rb b/app/models/spree/order_decorator.rb index bbe933fb58..486af60695 100644 --- a/app/models/spree/order_decorator.rb +++ b/app/models/spree/order_decorator.rb @@ -7,7 +7,7 @@ Spree::Order.class_eval do end def distributor=(distributor) - raise "You cannot change the distributor of an order with products" unless can_change_distributor? + raise "You cannot change the distributor of an order with products" unless distributor == self.distributor || can_change_distributor? super(distributor) end diff --git a/app/views/spree/products/_add_to_cart.html.haml b/app/views/spree/products/_add_to_cart.html.haml index bdeda7c571..0d9747218b 100644 --- a/app/views/spree/products/_add_to_cart.html.haml +++ b/app/views/spree/products/_add_to_cart.html.haml @@ -13,6 +13,7 @@ %p Distributor = select_tag "distributor_id", options_from_collection_for_select(@product.distributors, "id", "name", current_distributor.andand.id) - else + = hidden_field_tag "distributor_id", order.distributor.id .distributor-fixed= "Your distributor for this order is #{order.distributor.name}" %br/ = button_tag :class => 'large primary', :id => 'add-to-cart-button', :type => :submit do diff --git a/spec/models/order_spec.rb b/spec/models/order_spec.rb index 33a04f4264..8a6cd8c177 100644 --- a/spec/models/order_spec.rb +++ b/spec/models/order_spec.rb @@ -10,7 +10,9 @@ describe Spree::Order do end it "raises an exception if distributor is changed without permission" do - p = build(:product) + d = build(:distributor) + p = build(:product, :distributors => [d]) + subject.distributor = d subject.add_variant(p.master, 1) subject.can_change_distributor?.should be_false diff --git a/spec/requests/consumer/add_to_cart_spec.rb b/spec/requests/consumer/add_to_cart_spec.rb index 14938dcc8b..0bef5df680 100644 --- a/spec/requests/consumer/add_to_cart_spec.rb +++ b/spec/requests/consumer/add_to_cart_spec.rb @@ -83,5 +83,25 @@ feature %q{ page.should_not have_selector "button#add-to-cart-button" page.should have_content "Please complete your order at #{d1.name} before shopping with another distributor." end + + it "adds products with valid distributors" do + # Given two products, each at the same distributor + d = create(:distributor) + p1 = create(:product, :distributors => [d]) + p2 = create(:product, :distributors => [d]) + + # When I add the first to my cart + visit spree.product_path p1 + click_button 'Add To Cart' + + # And I add the second + visit spree.product_path p2 + click_button 'Add To Cart' + + # Then both should be in my cart + visit spree.cart_path + page.should have_selector 'h4 a', :text => p1.name + page.should have_selector 'h4 a', :text => p2.name + end end end