diff --git a/app/models/spree/order_populator_decorator.rb b/app/models/spree/order_populator_decorator.rb index fa2278309c..c1581c351d 100644 --- a/app/models/spree/order_populator_decorator.rb +++ b/app/models/spree/order_populator_decorator.rb @@ -58,12 +58,10 @@ Spree::OrderPopulator.class_eval do end def check_distribution_provided_for(variant) - order_cycle_required = order_cycle_required_for(variant) - distribution_provided = - @distributor.present? && (!order_cycle_required || @order_cycle.present?) + distribution_provided = distribution_provided_for variant unless distribution_provided - if order_cycle_required + if order_cycle_required_for variant errors.add(:base, "Please choose a distributor and order cycle for this order.") else errors.add(:base, "Please choose a distributor for this order.") @@ -82,8 +80,11 @@ Spree::OrderPopulator.class_eval do end end + def distribution_provided_for(variant) + @distributor.present? && (!order_cycle_required_for(variant) || @order_cycle.present?) + end + def order_cycle_required_for(variant) variant.product.product_distributions.empty? end - end diff --git a/spec/models/order_populator_spec.rb b/spec/models/order_populator_spec.rb index 8135f1069a..d9e4e483bf 100644 --- a/spec/models/order_populator_spec.rb +++ b/spec/models/order_populator_spec.rb @@ -97,6 +97,33 @@ module Spree op.send(:set_cart_distributor_and_order_cycle, distributor, order_cycle) end + + describe "checking if distribution is provided for a variant" do + let(:variant) { double(:variant) } + + it "returns false when distributor is nil" do + op.instance_eval { @distributor = nil } + op.send(:distribution_provided_for, variant).should be_false + end + + it "returns false when order cycle is nil when it's required" do + op.instance_eval { @distributor = 1; @order_cycle = nil } + op.should_receive(:order_cycle_required_for).with(variant).and_return(true) + op.send(:distribution_provided_for, variant).should be_false + end + + it "returns true when distributor is present and order cycle is not required" do + op.instance_eval { @distributor = 1; @order_cycle = nil } + op.should_receive(:order_cycle_required_for).with(variant).and_return(false) + op.send(:distribution_provided_for, variant).should be_true + end + + it "returns true when distributor is present and order cycle is required and present" do + op.instance_eval { @distributor = 1; @order_cycle = 1 } + op.should_receive(:order_cycle_required_for).with(variant).and_return(true) + op.send(:distribution_provided_for, variant).should be_true + end + end end describe "validations" do @@ -121,6 +148,30 @@ module Spree end describe "checking distribution is provided for a variant" do + let(:variant) { double(:variant) } + + it "returns false and errors when distribution is not provided and order cycle is required" do + op.should_receive(:distribution_provided_for).with(variant).and_return(false) + op.should_receive(:order_cycle_required_for).with(variant).and_return(true) + + op.send(:check_distribution_provided_for, variant).should be_false + op.errors.to_a.should == ["Please choose a distributor and order cycle for this order."] + end + + it "returns false and errors when distribution is not provided and order cycle is not required" do + op.should_receive(:distribution_provided_for).with(variant).and_return(false) + op.should_receive(:order_cycle_required_for).with(variant).and_return(false) + + op.send(:check_distribution_provided_for, variant).should be_false + op.errors.to_a.should == ["Please choose a distributor for this order."] + end + + it "returns true and does not error otherwise" do + op.should_receive(:distribution_provided_for).with(variant).and_return(true) + + op.send(:check_distribution_provided_for, variant).should be_true + op.errors.to_a.should be_empty + end end describe "checking variant is available under the distributor" do