Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for repair adjustments code #1474

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Improve adjustment-repair error message and add specs
  • Loading branch information
jordan-brough committed Sep 27, 2016
commit 8132da513fa8dcf4999232cb951bfb909e64bf24
4 changes: 2 additions & 2 deletions core/app/models/spree/adjustment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,14 @@ def require_promotion_code?

def repair_adjustments_associations_on_create
if adjustable.adjustments.loaded? && !adjustable.adjustments.include?(self)
Spree::Deprecation.warn("Adjustment was not added to #{adjustable.class}. Add adjustments via `adjustable.adjustments.create!`. Partial call stack: #{caller.select { |line| line =~ %r(/(app|spec)/) }}.", caller)
Spree::Deprecation.warn("Adjustment #{id} was not added to #{adjustable.class} #{adjustable.id}. Add adjustments via `adjustable.adjustments.create!`. Partial call stack: #{caller.select { |line| line =~ %r(/(app|spec)/) }}.", caller)
adjustable.adjustments.proxy_association.add_to_target(self)
end
end

def repair_adjustments_associations_on_destroy
if adjustable.adjustments.loaded? && adjustable.adjustments.include?(self)
Spree::Deprecation.warn("Adjustment was not removed from #{adjustable.class}. Remove adjustments via `adjustable.adjustments.destroy`. Partial call stack: #{caller.select { |line| line =~ %r(/(app|spec)/) }}.", caller)
Spree::Deprecation.warn("Adjustment #{id} was not removed from #{adjustable.class} #{adjustable.id}. Remove adjustments via `adjustable.adjustments.destroy`. Partial call stack: #{caller.select { |line| line =~ %r(/(app|spec)/) }}.", caller)
adjustable.adjustments.proxy_association.target.delete(self)
end
end
Expand Down
126 changes: 126 additions & 0 deletions core/spec/models/spree/adjustment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,130 @@
end
end
end

describe 'repairing adjustment associations' do
context 'on create' do
let(:adjustable) { order }
let(:adjustment_source) { promotion.actions[0] }
let(:order) { create(:order) }
let(:promotion) { create(:promotion, :with_line_item_adjustment) }

def expect_deprecation_warning
expect(Spree::Deprecation).to(
receive(:warn).
with(
/Adjustment \d+ was not added to #{adjustable.class} #{adjustable.id}/,
instance_of(Array),
)
)
end

context 'when adding adjustments via the wrong association' do
def create_adjustment
adjustment_source.adjustments.create!(
amount: 10,
adjustable: adjustable,
order: order,
label: 'some label',
)
end

context 'when adjustable.adjustments is loaded' do
before { adjustable.adjustments.to_a }

it 'repairs adjustable.adjustments' do
expect_deprecation_warning
adjustment = create_adjustment
expect(adjustable.adjustments).to include(adjustment)
end
end

context 'when adjustable.adjustments is not loaded' do
it 'does repair' do
expect(Spree::Deprecation).not_to receive(:warn)
create_adjustment
end
end
end

context 'when adding adjustments via the correct association' do
def create_adjustment
adjustable.adjustments.create!(
amount: 10,
source: adjustment_source,
order: order,
label: 'some label',
)
end

context 'when adjustable.adjustments is loaded' do
before { adjustable.adjustments.to_a }

it 'does not repair' do
expect(Spree::Deprecation).not_to receive(:warn)
create_adjustment
end
end

context 'when adjustable.adjustments is not loaded' do
it 'does not repair' do
expect(Spree::Deprecation).not_to receive(:warn)
create_adjustment
end
end
end
end

context 'on destroy' do
let(:adjustment) { create(:adjustment) }
let(:adjustable) { adjustment.adjustable }

def expect_deprecation_warning(adjustable)
expect(Spree::Deprecation).to(
receive(:warn).
with(
/Adjustment #{adjustment.id} was not removed from #{adjustable.class} #{adjustable.id}/,
instance_of(Array),
)
)
end

context 'when destroying adjustments not via association' do
context 'when adjustable.adjustments is loaded' do
before { adjustable.adjustments.to_a }

it 'repairs adjustable.adjustments' do
expect_deprecation_warning(adjustable)
adjustment.destroy!
expect(adjustable.adjustments).not_to include(adjustment)
end
end

context 'when adjustable.adjustments is not loaded' do
it 'does repair' do
expect(Spree::Deprecation).not_to receive(:warn)
adjustment.destroy!
end
end
end

context 'when destroying adjustments via the association' do
context 'when adjustable.adjustments is loaded' do
before { adjustable.adjustments.to_a }

it 'does not repair' do
expect(Spree::Deprecation).not_to receive(:warn)
adjustable.adjustments.destroy(adjustment)
end
end

context 'when adjustable.adjustments is not loaded' do
it 'does not repair' do
expect(Spree::Deprecation).not_to receive(:warn)
adjustable.adjustments.destroy(adjustment)
end
end
end
end
end
end