Class: Spree::Adjustment

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

Instance Method Summary collapse

Instance Method Details

#currencyObject



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

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

#display_amountObject



107
108
109
# File 'app/models/spree/adjustment.rb', line 107

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?



74
75
76
77
# File 'app/models/spree/adjustment.rb', line 74

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

#immutable?Boolean



111
112
113
# File 'app/models/spree/adjustment.rb', line 111

def immutable?
  state != "open"
end

#promotion?Boolean



61
62
63
# File 'app/models/spree/adjustment.rb', line 61

def promotion?
  originator_type == 'Spree::PromotionAction'
end

#set_eligibilityObject

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



67
68
69
70
# File 'app/models/spree/adjustment.rb', line 67

def set_eligibility
  result = mandatory || ((amount != 0 || promotion?) && eligible_for_originator?)
  update_column(:eligible, result)
end

#update!(calculable = nil) ⇒ 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.

Adjustments delegate updating of amount to their Originator when present, but only if when they're in “open” state, closed or finalized adjustments are not recalculated.

It receives calculable as the updated source here so calculations can be performed on the current values of that source. If we used source it could load the old record from db for the association. e.g. when updating more than on line items at once via accepted_nested_attributes the order object on the association would be in a old state and therefore the adjustment calculations would not performed on proper values



93
94
95
96
97
98
99
100
101
# File 'app/models/spree/adjustment.rb', line 93

def update!(calculable = nil)
  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, calculable || source) if originator.present?
  set_eligibility
end