diff --git a/app/controllers/spree/orders_controller_decorator.rb b/app/controllers/spree/orders_controller_decorator.rb index be6b716639..d442380f2d 100644 --- a/app/controllers/spree/orders_controller_decorator.rb +++ b/app/controllers/spree/orders_controller_decorator.rb @@ -2,7 +2,7 @@ Spree::OrdersController.class_eval do before_filter :populate_order_distributor, :only => :populate def populate_order_distributor - @distributor = Spree::Distributor.find params[:distributor_id] + @distributor = params.key?(:distributor_id) ? Spree::Distributor.find(params[:distributor_id]) : nil if populate_valid? @distributor order = current_order(true) diff --git a/spec/controllers/orders_controller_spec.rb b/spec/controllers/orders_controller_spec.rb index d7b82ae613..89f4ef46ba 100644 --- a/spec/controllers/orders_controller_spec.rb +++ b/spec/controllers/orders_controller_spec.rb @@ -45,8 +45,8 @@ describe Spree::OrdersController do # And the product is in the cart spree_put :populate, :variants => {@product.id => 1}, :distributor_id => @distributor.id - current_order(false).line_items.map { |li| li.product }.should == [@product] - current_order(false).distributor.should == @distributor + current_order(false).line_items.reload.map { |li| li.product }.should == [@product] + current_order(false).distributor.reload.should == @distributor end it "does not add the product if the product is not available at the order's distributor" do @@ -58,8 +58,8 @@ describe Spree::OrdersController do spree_put :populate, :variants => {p2.id => 1}, :distributor_id => d2.id # Then the product should not be added to the cart - current_order(false).line_items.map { |li| li.product }.should == [@product] - current_order(false).distributor.should == @distributor + current_order(false).line_items.reload.map { |li| li.product }.should == [@product] + current_order(false).distributor.reload.should == @distributor end it "does not add the product if the product is not available at the given distributor" do @@ -71,10 +71,21 @@ describe Spree::OrdersController do spree_put :populate, :variants => {p2.id => 1}, :distributor_id => @distributor.id # Then the product should not be added to the cart - current_order(false).line_items.map { |li| li.product }.should == [@product] - current_order(false).distributor.should == @distributor + current_order(false).line_items.reload.map { |li| li.product }.should == [@product] + current_order(false).distributor.reload.should == @distributor end - it "does not add the product if the chosen distributor is different from the order's distributor" + it "does not add the product if the chosen distributor is different from the order's distributor" do + # Given a product that's available at the chosen distributor and another distributor + d2 = create(:distributor) + p2 = create(:product, :distributors => [@distributor, d2]) + + # When I attempt to add the product to the cart with the alternate distributor + spree_put :populate, :variants => {p2.id => 1}, :distributor_id => d2 + + # Then the product should not be added to the cart + current_order(false).line_items.reload.map { |li| li.product }.should == [@product] + current_order(false).distributor.reload.should == @distributor + end end end