Decouple OrderCycle#fees_for from LineItem

This commit is contained in:
Rohan Mitchell
2014-02-05 13:24:55 +11:00
parent 7bdcb894cb
commit 04487da22a
2 changed files with 21 additions and 22 deletions

View File

@@ -144,33 +144,31 @@ class OrderCycle < ActiveRecord::Base
# -- Fees
def create_adjustments_for(line_item)
fees_for(line_item).each { |fee| create_adjustment_for_fee line_item, fee[:enterprise_fee], fee[:label], fee[:role] }
variant = line_item.variant
distributor = line_item.order.distributor
fees_for(variant, distributor).each { |fee| create_adjustment_for_fee line_item, fee[:enterprise_fee], fee[:label], fee[:role] }
end
private
# -- Fees
def fees_for(line_item)
def fees_for(variant, distributor)
fees = []
# If there are multiple distributors with this variant, won't this mean that we get a fee charged for each of them?
# We just want the one matching line_item.order.distributor
exchanges_carrying(line_item).each do |exchange|
exchanges_carrying(variant, distributor).each do |exchange|
exchange.enterprise_fees.each do |enterprise_fee|
role = exchange.incoming? ? 'supplier' : 'distributor'
fees << {enterprise_fee: enterprise_fee,
label: adjustment_label_for(line_item, enterprise_fee, role),
label: adjustment_label_for(variant, enterprise_fee, role),
role: role}
end
end
coordinator_fees.each do |enterprise_fee|
fees << {enterprise_fee: enterprise_fee,
label: adjustment_label_for(line_item, enterprise_fee, 'coordinator'),
label: adjustment_label_for(variant, enterprise_fee, 'coordinator'),
role: 'coordinator'}
end
@@ -182,14 +180,11 @@ class OrderCycle < ActiveRecord::Base
AdjustmentMetadata.create! adjustment: a, enterprise: enterprise_fee.enterprise, fee_name: enterprise_fee.name, fee_type: enterprise_fee.fee_type, enterprise_role: role
end
def adjustment_label_for(line_item, enterprise_fee, role)
"#{line_item.variant.product.name} - #{enterprise_fee.fee_type} fee by #{role} #{enterprise_fee.enterprise.name}"
def adjustment_label_for(variant, enterprise_fee, role)
"#{variant.product.name} - #{enterprise_fee.fee_type} fee by #{role} #{enterprise_fee.enterprise.name}"
end
def exchanges_carrying(line_item)
coordinator = line_item.order.order_cycle.coordinator
distributor = line_item.order.distributor
exchanges.to_enterprises([coordinator, distributor]).with_variant(line_item.variant)
def exchanges_carrying(variant, distributor)
exchanges.to_enterprises([coordinator, distributor]).with_variant(variant)
end
end

View File

@@ -311,17 +311,21 @@ describe OrderCycle do
describe "creating adjustments for a line item" do
let(:oc) { OrderCycle.new }
let(:line_item) { double(:line_item, variant: 123) }
let(:variant) { double(:variant) }
let(:distributor) { double(:distributor) }
let(:order) { double(:order, distributor: distributor) }
let(:line_item) { double(:line_item, variant: variant, order: order) }
it "creates adjustment for each fee" do
fee = {enterprise_fee: 'ef', label: 'label', role: 'role'}
oc.stub(:fees_for) { [fee] }
oc.should_receive(:fees_for).with(variant, distributor) { [fee] }
oc.should_receive(:create_adjustment_for_fee).with(line_item, 'ef', 'label', 'role')
oc.send(:create_adjustments_for, line_item)
end
it "finds fees for a line item" do
distributor = double(:distributor)
ef1 = double(:enterprise_fee)
ef2 = double(:enterprise_fee)
ef3 = double(:enterprise_fee)
@@ -331,7 +335,7 @@ describe OrderCycle do
oc.stub(:coordinator_fees) { [ef3] }
oc.stub(:adjustment_label_for) { 'label' }
oc.send(:fees_for, line_item).should ==
oc.send(:fees_for, line_item.variant, distributor).should ==
[{enterprise_fee: ef1, label: 'label', role: 'supplier'},
{enterprise_fee: ef2, label: 'label', role: 'distributor'},
{enterprise_fee: ef3, label: 'label', role: 'coordinator'}]
@@ -358,10 +362,10 @@ describe OrderCycle do
end
it "makes adjustment labels" do
line_item = double(:line_item, variant: double(:variant, product: double(:product, name: 'Bananas')))
variant = double(:variant, product: double(:product, name: 'Bananas'))
enterprise_fee = double(:enterprise_fee, fee_type: 'packing', enterprise: double(:enterprise, name: 'Ballantyne'))
oc.send(:adjustment_label_for, line_item, enterprise_fee, 'distributor').should == "Bananas - packing fee by distributor Ballantyne"
oc.send(:adjustment_label_for, variant, enterprise_fee, 'distributor').should == "Bananas - packing fee by distributor Ballantyne"
end
end