diff --git a/app/jobs/subscription_placement_job.rb b/app/jobs/subscription_placement_job.rb index 50ace6e74d..51056da676 100644 --- a/app/jobs/subscription_placement_job.rb +++ b/app/jobs/subscription_placement_job.rb @@ -67,7 +67,7 @@ class SubscriptionPlacementJob end def available_variants_for(order) - OrderCycleDistributedVariants.new(order).variants_available_for_distribution(order.distributor, order.order_cycle) + OrderCycleDistributedVariants.new(order.order_cycle, order.distributor).available_variants end def send_placement_email(order, changes) diff --git a/app/models/spree/order_decorator.rb b/app/models/spree/order_decorator.rb index f2e405dc38..31a6752859 100644 --- a/app/models/spree/order_decorator.rb +++ b/app/models/spree/order_decorator.rb @@ -80,7 +80,7 @@ Spree::Order.class_eval do # -- Methods def products_available_from_new_distribution # Check that the line_items in the current order are available from a newly selected distribution - errors.add(:base, I18n.t(:spree_order_availability_error)) unless OrderCycleDistributedVariants.new(self).can_change_to_distribution?(distributor, order_cycle) + errors.add(:base, I18n.t(:spree_order_availability_error)) unless OrderCycleDistributedVariants.new(order_cycle, distributor).distributes_order_variants?(self) end def using_guest_checkout? diff --git a/app/services/cart_service.rb b/app/services/cart_service.rb index 15984bc829..49d157a836 100644 --- a/app/services/cart_service.rb +++ b/app/services/cart_service.rb @@ -137,7 +137,7 @@ class CartService end def check_variant_available_under_distribution(variant) - return true if OrderCycleDistributedVariants.new(@order).variants_available_for_distribution(@distributor, @order_cycle).include? variant + return true if OrderCycleDistributedVariants.new(@order_cycle, @distributor).available_variants.include? variant errors.add(:base, I18n.t(:spree_order_populator_availability_error)) false diff --git a/app/services/order_cycle_distributed_variants.rb b/app/services/order_cycle_distributed_variants.rb index 2f66c12cd6..0718b9b393 100644 --- a/app/services/order_cycle_distributed_variants.rb +++ b/app/services/order_cycle_distributed_variants.rb @@ -1,15 +1,15 @@ class OrderCycleDistributedVariants - - def initialize order - @order = order + def initialize(order_cycle, distributor) + @order_cycle = order_cycle + @distributor = distributor end - def can_change_to_distribution?(distributor, order_cycle) - (@order.line_item_variants - variants_available_for_distribution(distributor, order_cycle)).empty? + def distributes_order_variants?(order) + (order.line_item_variants - available_variants).empty? end - def variants_available_for_distribution(distributor, order_cycle) - return [] unless order_cycle - order_cycle.variants_distributed_by(distributor) + def available_variants + return [] unless @order_cycle + @order_cycle.variants_distributed_by(@distributor) end end diff --git a/spec/services/cart_service_spec.rb b/spec/services/cart_service_spec.rb index 6c3a01d98b..3fd70c27fe 100644 --- a/spec/services/cart_service_spec.rb +++ b/spec/services/cart_service_spec.rb @@ -227,21 +227,23 @@ describe CartService do describe "checking variant is available under the distributor" do let(:product) { double(:product) } let(:variant) { double(:variant, product: product) } + let(:order_cycle_distributed_variants) { double(:order_cycle_distributed_variants) } + + before do + expect(OrderCycleDistributedVariants).to receive(:new).with(234, 123).and_return(order_cycle_distributed_variants) + cart_service.instance_eval { @distributor = 123; @order_cycle = 234 } + end it "delegates to OrderCycleDistributedVariants, returning true when available" do - dcv = double(:dcv) - expect(dcv).to receive(:variants_available_for_distribution).with(123, 234).and_return([variant]) - expect(OrderCycleDistributedVariants).to receive(:new).with(order).and_return(dcv) - cart_service.instance_eval { @distributor = 123; @order_cycle = 234 } + expect(order_cycle_distributed_variants).to receive(:available_variants).and_return([variant]) + expect(cart_service.send(:check_variant_available_under_distribution, variant)).to be true expect(cart_service.errors).to be_empty end it "delegates to OrderCycleDistributedVariants, returning false and erroring otherwise" do - dcv = double(:dcv) - expect(dcv).to receive(:variants_available_for_distribution).with(123, 234).and_return([]) - expect(OrderCycleDistributedVariants).to receive(:new).with(order).and_return(dcv) - cart_service.instance_eval { @distributor = 123; @order_cycle = 234 } + expect(order_cycle_distributed_variants).to receive(:available_variants).and_return([]) + expect(cart_service.send(:check_variant_available_under_distribution, variant)).to be false expect(cart_service.errors.to_a).to eq(["That product is not available from the chosen distributor or order cycle."]) end diff --git a/spec/services/order_cycle_distributed_variants_spec.rb b/spec/services/order_cycle_distributed_variants_spec.rb index f8f2c65772..fd55e1ada5 100644 --- a/spec/services/order_cycle_distributed_variants_spec.rb +++ b/spec/services/order_cycle_distributed_variants_spec.rb @@ -2,41 +2,36 @@ require 'spec_helper' describe OrderCycleDistributedVariants do let(:order) { double(:order) } - let(:subject) { OrderCycleDistributedVariants.new(order) } + let(:distributor) { double(:distributor) } + let(:order_cycle) { double(:order_cycle) } + let(:subject) { OrderCycleDistributedVariants.new(order_cycle, distributor) } let(:product) { double(:product) } describe "checking if an order can change to a specified new distribution" do - let(:distributor) { double(:distributor) } - let(:order_cycle) { double(:order_cycle) } - it "returns false when a variant is not available for the specified distribution" do order.should_receive(:line_item_variants) { [1] } - subject.should_receive(:variants_available_for_distribution). - with(distributor, order_cycle) { [] } - expect(subject.can_change_to_distribution?(distributor, order_cycle)).to be false + subject.should_receive(:available_variants) { [] } + expect(subject.distributes_order_variants?(order)).to be false end it "returns true when all variants are available for the specified distribution" do order.should_receive(:line_item_variants) { [1] } - subject.should_receive(:variants_available_for_distribution). - with(distributor, order_cycle) { [1] } - expect(subject.can_change_to_distribution?(distributor, order_cycle)).to be true + subject.should_receive(:available_variants) { [1] } + expect(subject.distributes_order_variants?(order)).to be true end end describe "finding variants that are available through a particular order cycle" do it "finds variants distributed by order cycle" do variant = double(:variant) - distributor = double(:distributor) - order_cycle = double(:order_cycle) - order_cycle.should_receive(:variants_distributed_by).with(distributor) { [variant] } - expect(subject.variants_available_for_distribution(distributor, order_cycle)).to eq [variant] + expect(subject.available_variants).to eq [variant] end it "returns an empty array when order cycle is nil" do - expect(subject.variants_available_for_distribution(nil, nil)).to eq [] + subject = OrderCycleDistributedVariants.new(nil, nil) + expect(subject.available_variants).to eq [] end end end