Module: SolidusLegacyPromotions::SpreeAdjustmentDecorator

Defined in:
app/decorators/solidus_legacy_promotions/models/spree_adjustment_decorator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



5
6
7
8
9
10
# File 'app/decorators/solidus_legacy_promotions/models/spree_adjustment_decorator.rb', line 5

def self.prepended(base)
  base.belongs_to :promotion_code, class_name: 'Spree::PromotionCode', optional: true
  base.validates :promotion_code, presence: true, if: :require_promotion_code?

  base.scope :eligible, -> { where(eligible: true) }
end

Instance Method Details

#calculate_eligibilitytrue, false

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Calculates based on attached promotion (if this is a promotion adjustment) whether this promotion is still eligible.

Returns:

  • (true, false)

    Whether this adjustment is eligible



50
51
52
53
54
55
56
# File 'app/decorators/solidus_legacy_promotions/models/spree_adjustment_decorator.rb', line 50

def calculate_eligibility
  if !finalized? && source && promotion?
    source.promotion.eligible?(adjustable, promotion_code:)
  else
    eligible?
  end
end

#eligibleObject Also known as: eligible?



59
60
61
# File 'app/decorators/solidus_legacy_promotions/models/spree_adjustment_decorator.rb', line 59

def eligible
  self[:eligible]
end

#recalculateBigDecimal

Recalculate and persist the amount from this adjustment’s source based on the adjustable (Order, Shipment, or LineItem)

If the adjustment has no source (such as when created manually from the admin) or is closed, this is a noop.

Returns:

  • (BigDecimal)

    New amount of this adjustment



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/decorators/solidus_legacy_promotions/models/spree_adjustment_decorator.rb', line 19

def recalculate
  if finalized? && !tax?
    return amount
  end

  # If the adjustment has no source, do not attempt to re-calculate the
  # amount.
  # Some scenarios where this happens:
  #   - Adjustments that are manually created via the admin backend
  #   - PromotionAction adjustments where the PromotionAction was deleted
  #     after the order was completed.
  if source.present?
    self.amount = source.compute_amount(adjustable)

    if promotion?
      self.eligible = calculate_eligibility
    end

    # Persist only if changed
    # This is only not a save! to avoid the extra queries to load the order
    # (for validations) and to touch the adjustment.
    update_columns(eligible:, amount:, updated_at: Time.current) if changed?
  end
  amount
end