Move ProductDistribution#clear_all_enterprise_fee_adjustments_for to EnterpriseFee class

This commit is contained in:
Rohan Mitchell
2013-08-16 14:10:03 +10:00
parent e318a1591d
commit 591f6a8a57
4 changed files with 37 additions and 36 deletions

View File

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

View File

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

View File

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

View File

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