From 612e6aed2cc51128c1e9759d7fa85946fbcb6ac4 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Mon, 27 May 2013 13:12:29 +1000 Subject: [PATCH] Include order cycle distributions in distributor choice when adding product to cart --- .../spree/orders_controller_decorator.rb | 4 +- app/helpers/add_to_cart_helper.rb | 2 +- spec/features/consumer/add_to_cart_spec.rb | 54 +++++++++++++++++++ 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/app/controllers/spree/orders_controller_decorator.rb b/app/controllers/spree/orders_controller_decorator.rb index c5c8ffb38f..6d81459303 100644 --- a/app/controllers/spree/orders_controller_decorator.rb +++ b/app/controllers/spree/orders_controller_decorator.rb @@ -100,12 +100,12 @@ Spree::OrdersController.class_eval do # -- All products must be available under that distributor params[:products].each do |product_id, variant_id| product = Spree::Product.find product_id - return false unless product.distributors.include? distributor + return false unless Enterprise.distributing_product(product).include? distributor end if params[:products] params[:variants].each do |variant_id, quantity| variant = Spree::Variant.find variant_id - return false unless variant.product.distributors.include? distributor + return false unless Enterprise.distributing_product(variant.product).include? distributor end if params[:variants] # -- If products in cart, distributor can't be changed diff --git a/app/helpers/add_to_cart_helper.rb b/app/helpers/add_to_cart_helper.rb index 1e41219d4e..445e65d9ae 100644 --- a/app/helpers/add_to_cart_helper.rb +++ b/app/helpers/add_to_cart_helper.rb @@ -4,7 +4,7 @@ module AddToCartHelper end def product_incompatible_with_current_order(order, product) - !order.nil? && !DistributorChangeValidator.new(order).can_change_distributor? && !Enterprise.distributing_product(product).include?(order.distributor) + order.present? && available_distributors_for(order, product).empty? end def available_distributors_for(order, product) diff --git a/spec/features/consumer/add_to_cart_spec.rb b/spec/features/consumer/add_to_cart_spec.rb index dd68b59184..6aa4c3311c 100644 --- a/spec/features/consumer/add_to_cart_spec.rb +++ b/spec/features/consumer/add_to_cart_spec.rb @@ -141,6 +141,60 @@ feature %q{ end end + context "with distribution via order cycle" do + it "allows us to add two products from the same distributor" do + # Given two products, each at the same distributor + d = create(:distributor_enterprise) + p1 = create(:product) + p2 = create(:product) + create(:simple_order_cycle, :distributors => [d], :variants => [p1.master, p2.master]) + + # 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 + 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 + + it "offers to automatically change distributors when there is only one choice", js: true do + # Given two products, one available at only one distributor + d1 = create(:distributor_enterprise) + d2 = create(:distributor_enterprise) + p1 = create(:product) + p2 = create(:product) + create(:simple_order_cycle, :distributors => [d1], :variants => [p1.master]) + create(:simple_order_cycle, :distributors => [d2], :variants => [p1.master, p2.master]) + + # p1 - available at both, select d1 + # p2 - available at the one not selected + + # When I add the first to my cart + visit spree.product_path p1 + select d1.name, from: 'distributor_id' + click_button 'Add To Cart' + + # And I go to add the second + visit spree.product_path p2 + + # Then I should see a message offering to change distributor for my order + page.should have_content "Your distributor for this order will be changed to #{d2.name} if you add this product to your cart." + + # When I add the second to my cart + click_button 'Add To Cart' + + # Then My distributor should have changed + page.should have_selector "#current-distributor a", :text => d2.name.upcase + end + end + context "group buys" do scenario "adding a product to the cart for a group buy" do # Given a group buy product and a distributor