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
Prev Previous commit
Next Next commit
Fix for Adjustment#repair_adjustments_associations_on_create
When the item gets deleted before after_commit runs.

I ran into this while working on some changes where some tax
adjustments would get created and destroyed in the same transaction.
  • Loading branch information
jordan-brough committed Sep 27, 2016
commit 2ad67d2aac3f0fdc0d5a621eef76b75f79714002
2 changes: 1 addition & 1 deletion core/app/models/spree/adjustment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def require_promotion_code?
end

def repair_adjustments_associations_on_create
if adjustable.adjustments.loaded? && !adjustable.adjustments.include?(self)
if adjustable.adjustments.loaded? && !adjustable.adjustments.include?(self) && Spree::Adjustment.exists?(id)
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
Expand Down
10 changes: 10 additions & 0 deletions core/spec/models/spree/adjustment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,16 @@ def create_adjustment
adjustment = create_adjustment
expect(adjustable.adjustments).to include(adjustment)
end

context 'when the adjustment is destroyed before after_commit runs' do
it 'does not repair' do
expect(Spree::Deprecation).not_to receive(:warn)
Spree::Adjustment.transaction do
adjustment = create_adjustment
adjustment.destroy!
end
end
end
end

context 'when adjustable.adjustments is not loaded' do
Expand Down