diff --git a/app/models/enterprise_fee.rb b/app/models/enterprise_fee.rb index 96b062cdd0..9be96e853b 100644 --- a/app/models/enterprise_fee.rb +++ b/app/models/enterprise_fee.rb @@ -12,4 +12,9 @@ class EnterpriseFee < ActiveRecord::Base scope :for_enterprise, lambda { |enterprise| where(enterprise_id: enterprise) } + + + def self.clear_all_adjustments_for(line_item) + line_item.order.adjustments.where(originator_type: 'EnterpriseFee', source_id: line_item, source_type: 'Spree::LineItem').destroy_all + end end diff --git a/app/models/product_distribution.rb b/app/models/product_distribution.rb index fbad4e6dd9..235d369022 100644 --- a/app/models/product_distribution.rb +++ b/app/models/product_distribution.rb @@ -10,7 +10,7 @@ class ProductDistribution < ActiveRecord::Base def ensure_correct_adjustment_for(line_item) if enterprise_fee - clear_all_enterprise_fee_adjustments_for line_item + EnterpriseFee.clear_all_adjustments_for line_item create_adjustment_for line_item end end @@ -28,12 +28,8 @@ class ProductDistribution < ActiveRecord::Base AdjustmentMetadata.create! adjustment: a, enterprise: enterprise_fee.enterprise, fee_name: enterprise_fee.name, fee_type: enterprise_fee.fee_type, enterprise_role: 'distributor' end - def clear_all_enterprise_fee_adjustments_for(line_item) - line_item.order.adjustments.where(originator_type: 'EnterpriseFee', source_id: line_item, source_type: 'Spree::LineItem').destroy_all - end def adjustment_label_for(line_item) "Product distribution by #{distributor.name} for #{line_item.product.name}" end - end diff --git a/spec/models/enterprise_fee_spec.rb b/spec/models/enterprise_fee_spec.rb index 1616b0decd..4af79ef2ab 100644 --- a/spec/models/enterprise_fee_spec.rb +++ b/spec/models/enterprise_fee_spec.rb @@ -8,4 +8,32 @@ describe EnterpriseFee do describe "validations" do it { should validate_presence_of(:name) } end + + describe "clearing all enterprise fee adjustments for a line item" do + it "clears adjustments originating from many different enterprise fees" do + p = create(:simple_product) + d1, d2 = create(:distributor_enterprise), create(:distributor_enterprise) + pd1 = create(:product_distribution, product: p, distributor: d1) + pd2 = create(:product_distribution, product: p, distributor: d2) + line_item = create(:line_item, product: p) + pd1.enterprise_fee.create_adjustment('foo1', line_item.order, line_item, true) + pd2.enterprise_fee.create_adjustment('foo2', line_item.order, line_item, true) + + expect do + EnterpriseFee.clear_all_adjustments_for line_item + end.to change(line_item.order.adjustments, :count).by(-2) + end + + it "does not clear adjustments originating from another source" do + p = create(:simple_product) + pd = create(:product_distribution) + line_item = create(:line_item, product: pd.product) + tax_rate = create(:tax_rate, calculator: build(:calculator, preferred_amount: 10)) + tax_rate.create_adjustment('foo', line_item.order, line_item) + + expect do + EnterpriseFee.clear_all_adjustments_for line_item + end.to change(line_item.order.adjustments, :count).by(0) + end + end end diff --git a/spec/models/product_distribution_spec.rb b/spec/models/product_distribution_spec.rb index ad18e3f8ad..f7c4c3cbc6 100644 --- a/spec/models/product_distribution_spec.rb +++ b/spec/models/product_distribution_spec.rb @@ -74,20 +74,20 @@ describe ProductDistribution do # TODO: This spec will go away once enterprise_fee is required it "does nothing if there is no enterprise fee set" do pd.enterprise_fee = nil - pd.should_receive(:clear_all_enterprise_fee_adjustments_for).never + EnterpriseFee.should_receive(:clear_all_adjustments_for).never pd.should_receive(:create_adjustment_for).never pd.ensure_correct_adjustment_for line_item end describe "adding items to cart" do it "clears all enterprise fee adjustments on the line item" do - pd.should_receive(:clear_all_enterprise_fee_adjustments_for).with(line_item) + EnterpriseFee.should_receive(:clear_all_adjustments_for).with(line_item) pd.stub(:create_adjustment_for) pd.ensure_correct_adjustment_for line_item end it "creates an adjustment on the line item" do - pd.stub(:clear_all_enterprise_fee_adjustments_for) + EnterpriseFee.stub(:clear_all_adjustments_for) pd.should_receive(:create_adjustment_for).with(line_item) pd.ensure_correct_adjustment_for line_item end @@ -164,34 +164,6 @@ describe ProductDistribution do md.enterprise_role.should == 'distributor' end end - - describe "clearing all enterprise fee adjustments for a line item" do - it "clears adjustments originating from many different enterprise fees" do - p = create(:simple_product) - d1, d2 = create(:distributor_enterprise), create(:distributor_enterprise) - pd1 = create(:product_distribution, product: p, distributor: d1) - pd2 = create(:product_distribution, product: p, distributor: d2) - line_item = create(:line_item, product: p) - pd1.enterprise_fee.create_adjustment('foo1', line_item.order, line_item, true) - pd2.enterprise_fee.create_adjustment('foo2', line_item.order, line_item, true) - - expect do - pd1.send(:clear_all_enterprise_fee_adjustments_for, line_item) - end.to change(line_item.order.adjustments, :count).by(-2) - end - - it "does not clear adjustments originating from another source" do - p = create(:simple_product) - pd = create(:product_distribution) - line_item = create(:line_item, product: pd.product) - tax_rate = create(:tax_rate, calculator: build(:calculator, preferred_amount: 10)) - tax_rate.create_adjustment('foo', line_item.order, line_item) - - expect do - pd.send(:clear_all_enterprise_fee_adjustments_for, line_item) - end.to change(line_item.order.adjustments, :count).by(0) - end - end end