diff --git a/app/controllers/spree/orders_controller_decorator.rb b/app/controllers/spree/orders_controller_decorator.rb index 438f008ce6..2de1c82777 100644 --- a/app/controllers/spree/orders_controller_decorator.rb +++ b/app/controllers/spree/orders_controller_decorator.rb @@ -3,7 +3,7 @@ Spree::OrdersController.class_eval do after_filter :populate_variant_attributes, :only => :populate def populate_order_distributor - @distributor = params.key?(:distributor_id) ? Spree::Distributor.find(params[:distributor_id]) : nil + @distributor = params[:distributor_id].present? ? Spree::Distributor.find(params[:distributor_id]) : nil if populate_valid? @distributor order = current_order(true) @@ -11,7 +11,8 @@ Spree::OrdersController.class_eval do order.save! else - redirect_to cart_path + flash[:error] = "Please choose a distributor for this order." if @distributor.nil? + redirect_populate_to_first_product end end @@ -32,6 +33,7 @@ Spree::OrdersController.class_eval do private + def populate_valid? distributor # -- Distributor must be specified return false if distributor.nil? @@ -55,4 +57,14 @@ Spree::OrdersController.class_eval do true end + + def redirect_populate_to_first_product + product = if params[:products].present? + Spree::Product.find(params[:products].keys.first) + else + Spree::Variant.find(params[:variants].keys.first).product + end + + redirect_to product + 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 62f164c8d4..174e9e15d9 100644 --- a/app/views/spree/products/_add_to_cart.html.haml +++ b/app/views/spree/products/_add_to_cart.html.haml @@ -17,7 +17,7 @@ - order = current_order(false) - if order.nil? || order.can_change_distributor? %p Distributor - = select_tag "distributor_id", options_from_collection_for_select(@product.distributors, "id", "name", current_distributor.andand.id) + = select_tag "distributor_id", options_from_collection_for_select([Spree::Distributor.new]+@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}" diff --git a/spec/requests/consumer/add_to_cart_spec.rb b/spec/requests/consumer/add_to_cart_spec.rb index afb4440a8a..9db625c1fc 100644 --- a/spec/requests/consumer/add_to_cart_spec.rb +++ b/spec/requests/consumer/add_to_cart_spec.rb @@ -8,6 +8,26 @@ feature %q{ include AuthenticationWorkflow include WebHelper + scenario "adding a product to the cart with no distributor chosen" do + # Given a product and some distributors + d1 = create(:distributor) + d2 = create(:distributor) + p = create(:product, :distributors => [d1]) + create(:product, :distributors => [d2]) + + # When I add an item to my cart without choosing a distributor + visit spree.product_path p + click_button 'Add To Cart' + + # Then I should see an error message + page.should have_content "Please choose a distributor for this order." + + # And the product should not have been added to my cart + Spree::Order.last.should be_nil + #order.line_items.should be_empty + end + + scenario "adding the first product to the cart" do # Given a product and some distributors d1 = create(:distributor) @@ -40,6 +60,7 @@ feature %q{ # When I add a product to my cart (which sets my distributor) visit spree.product_path p + select d1.name, :from => 'distributor_id' click_button 'Add To Cart' page.should have_content "You are shopping at #{d1.name}" @@ -58,6 +79,7 @@ feature %q{ # And a product in my cart visit spree.product_path p + select d.name, :from => 'distributor_id' click_button 'Add To Cart' # When I go to add it again, I should not have a choice of distributor @@ -75,6 +97,7 @@ feature %q{ # When I add one of them to my cart visit spree.product_path p1 + select d1.name, :from => 'distributor_id' click_button 'Add To Cart' # And I attempt to add the other @@ -93,6 +116,7 @@ feature %q{ # When I add the first to my cart visit spree.product_path p1 + select d.name, :from => 'distributor_id' click_button 'Add To Cart' # And I add the second @@ -113,6 +137,7 @@ feature %q{ # When I add the item to my cart visit spree.product_path p + select d.name, :from => 'distributor_id' fill_in "variants_#{p.master.id}", :with => 2 fill_in "variant_attributes_#{p.master.id}_max_quantity", :with => 3 click_button 'Add To Cart' @@ -133,6 +158,7 @@ feature %q{ # When I add the item to my cart visit spree.product_path p + select d.name, :from => 'distributor_id' fill_in "quantity", :with => 2 fill_in "max_quantity", :with => 3 click_button 'Add To Cart' @@ -163,6 +189,7 @@ feature %q{ # When I add the item to my cart visit spree.product_path p + select d.name, :from => 'distributor_id' fill_in "variants_#{p.master.id}", :with => 2 fill_in "variant_attributes_#{p.master.id}_max_quantity", :with => 1 click_button 'Add To Cart' diff --git a/spec/requests/consumer/checkout_spec.rb b/spec/requests/consumer/checkout_spec.rb index 29d5fb1583..cfaaee8bee 100644 --- a/spec/requests/consumer/checkout_spec.rb +++ b/spec/requests/consumer/checkout_spec.rb @@ -46,6 +46,7 @@ feature %q{ # When I add some apples and some garlic to my cart click_link 'Fuji apples' + select @distributor.name, :from => 'distributor_id' click_button 'Add To Cart' click_link 'Continue shopping' @@ -71,6 +72,7 @@ feature %q{ login_to_consumer_section click_link 'Fuji apples' + select @distributor.name, :from => 'distributor_id' click_button 'Add To Cart' click_link 'Continue shopping'