From 04487da22a380904cd55608e0b15bde74100b51c Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Wed, 5 Feb 2014 13:24:55 +1100 Subject: [PATCH] Decouple OrderCycle#fees_for from LineItem --- app/models/order_cycle.rb | 29 ++++++++++++----------------- spec/models/order_cycle_spec.rb | 14 +++++++++----- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/app/models/order_cycle.rb b/app/models/order_cycle.rb index 187a93e0ef..99a3ff7406 100644 --- a/app/models/order_cycle.rb +++ b/app/models/order_cycle.rb @@ -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 diff --git a/spec/models/order_cycle_spec.rb b/spec/models/order_cycle_spec.rb index 9b024b058e..512298fc51 100644 --- a/spec/models/order_cycle_spec.rb +++ b/spec/models/order_cycle_spec.rb @@ -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