Class: Spree::Adjustment

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

Instance Method Summary collapse

Instance Method Details

#currencyObject



89
90
91
# File 'app/models/spree/adjustment.rb', line 89

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

#display_amountObject



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

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)


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

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

#immutable?Boolean

Returns:

  • (Boolean)


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

def immutable?
  state != "open"
end

#set_eligibilityObject

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



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

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



83
84
85
86
87
# File 'app/models/spree/adjustment.rb', line 83

def update!
  return if immutable?
  originator.update_adjustment(self, source) if originator.present?
  set_eligibility
end