Class: Spree::Adjustment

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/spree/adjustment.rb

Direct Known Subclasses

ShippingAdjustment

Instance Method Summary collapse

Instance Method Details

#currencyObject



94
95
96
# File 'app/models/spree/adjustment.rb', line 94

def currency
  adjustable ? adjustable.currency : Spree::Config[:currency]
end

#display_amountObject



98
99
100
# File 'app/models/spree/adjustment.rb', line 98

def display_amount
  Spree::Money.new(amount, { currency: currency })
end

#eligible_for_originator?Boolean

Allow originator of the adjustment to perform an additional eligibility of the adjustment Should return true if originator is absent or doesn’t implement eligible?

Returns:

  • (Boolean)


72
73
74
75
# File 'app/models/spree/adjustment.rb', line 72

def eligible_for_originator?
  return true if originator.nil?
  !originator.respond_to?(:eligible?) || originator.eligible?(source)
end

#immutable?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'app/models/spree/adjustment.rb', line 102

def immutable?
  state != "open"
end

#set_eligibilityObject

Update the boolean eligible attribute which determines which adjustments count towards the order’s adjustment_total.



65
66
67
68
# File 'app/models/spree/adjustment.rb', line 65

def set_eligibility
  result = self.mandatory || (self.amount != 0 && self.eligible_for_originator?)
  update_attribute_without_callbacks(:eligible, result)
end

#update!Object

Update both the eligibility and amount of the adjustment. Adjustments delegate updating of amount to their Originator when present, but only if locked is false. Adjustments that are locked will never change their amount.

order#update_adjustments passes self as the src, this is so calculations can be performed on the # current values. If we used source it would load the old record from db for the association



84
85
86
87
88
89
90
91
92
# File 'app/models/spree/adjustment.rb', line 84

def update!
  return if immutable?
  # Fix for #3381
  # If we attempt to call 'source' before the reload, then source is currently
  # the order object. After calling a reload, the source is the Shipment.
  reload
  originator.update_adjustment(self, source) if originator.present?
  set_eligibility
end