Refactor order_cycle_distributed_variants, better method names and simpler code

This commit is contained in:
luisramos0
2019-04-11 22:03:57 +01:00
parent 44b0592223
commit 59ec52babe
6 changed files with 31 additions and 34 deletions

View File

@@ -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)

View File

@@ -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?

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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