diff --git a/app/controllers/spree/orders_controller_decorator.rb b/app/controllers/spree/orders_controller_decorator.rb index d9cdc8bebe..b87beb7e2c 100644 --- a/app/controllers/spree/orders_controller_decorator.rb +++ b/app/controllers/spree/orders_controller_decorator.rb @@ -4,7 +4,14 @@ Spree::OrdersController.class_eval do def populate_order_distributor @distributor = Spree::Distributor.find params[:distributor_id] - redirect_to cart_path unless populate_valid? @distributor + if populate_valid? @distributor + order = current_order(true) + order.distributor = @distributor + order.save! + + else + redirect_to cart_path + end end private diff --git a/spec/controllers/orders_controller_spec.rb b/spec/controllers/orders_controller_spec.rb index 9d500062a4..1bac3596dc 100644 --- a/spec/controllers/orders_controller_spec.rb +++ b/spec/controllers/orders_controller_spec.rb @@ -1,6 +1,9 @@ require 'spec_helper' +require 'spree/core/current_order' describe Spree::OrdersController do + include Spree::Core::CurrentOrder + context "adding the first product to the cart" do it "does not add the product if the user does not specify a distributor" do create(:distributor) @@ -20,5 +23,18 @@ describe Spree::OrdersController do spree_put :populate, :variants => {p.id => 1}, :distributor_id => distributor_no_product.id end.to change(Spree::LineItem, :count).by(0) end + + it "sets the order's distributor" do + # Given a product in a distributor + d = create(:distributor) + p = create(:product, :distributors => [d]) + + # When we add the product to our cart + spree_put :populate, :variants => {p.id => 1}, :distributor_id => d.id + + # Then our order should have its distributor set to the chosen distributor + current_order(false).distributor.should == d + end + end end diff --git a/spec/requests/consumer/add_to_cart_spec.rb b/spec/requests/consumer/add_to_cart_spec.rb index fe9043d9fa..9ac434ac9b 100644 --- a/spec/requests/consumer/add_to_cart_spec.rb +++ b/spec/requests/consumer/add_to_cart_spec.rb @@ -8,13 +8,27 @@ feature %q{ include AuthenticationWorkflow include WebHelper - context "adding the first product to the cart" do - it "requires the user choose a distributor" do - # Tested in orders_controller_spec.rb - end + scenario "adding the first product to the cart" do + # Given a product and some distributors + d1 = create(:distributor) + d2 = create(:distributor) + p = create(:product, :distributors => [d1]) - it "sets the order's distributor" - it "sets the user's distributor when adding a product at a remote distributor" + # When I choose a distributor + visit spree.root_path + click_link d2.name + + # When I add an item to my cart from a different distributor + visit spree.product_path p + select d1.name, :from => 'distributor_id' + click_button 'Add To Cart' + + # Then the item should be in my cart + order = Spree::Order.last + order.line_items.first.product.should == p + + # And my order should have its distributor set to the chosen distributor + order.distributor.should == d1 end it "does not allow the user to change distributor after a product has been added to the cart"