Skip to content

Commit

Permalink
Remove hooks to repair adjustments associations
Browse files Browse the repository at this point in the history
It was happening when not using the provided interface, which is:

- adjustable.adjustments.create!
- adjustable.adjustments.destroy
  • Loading branch information
kennyadsl committed Nov 10, 2020
1 parent 50a5a5e commit dfece2c
Showing 1 changed file with 0 additions and 19 deletions.
19 changes: 0 additions & 19 deletions core/app/models/spree/adjustment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ class Adjustment < Spree::Base
validates :amount, numericality: true
validates :promotion_code, presence: true, if: :require_promotion_code?

# We need to use `after_commit` here because otherwise it's too early to
# tell if any repair is needed.
after_commit :repair_adjustments_associations_on_create, on: [:create]
after_commit :repair_adjustments_associations_on_destroy, on: [:destroy]

scope :not_finalized, -> { where(finalized: false) }
scope :finalized, -> { where(finalized: true) }
scope :cancellation, -> { where(source_type: 'Spree::UnitCancel') }
Expand Down Expand Up @@ -152,19 +147,5 @@ def calculate_eligibility
def require_promotion_code?
promotion? && !source.promotion.apply_automatically && source.promotion.codes.any?
end

def repair_adjustments_associations_on_create
if adjustable.adjustments.loaded? && !adjustable.adjustments.include?(self) && !destroyed?
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 #{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
end
end

0 comments on commit dfece2c

Please sign in to comment.