Class: Spree::Adjustment

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

Instance Method Summary collapse

Instance Method Details

#currencyObject



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

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

#display_amountObject



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

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)


78
79
80
81
# File 'app/models/spree/adjustment.rb', line 78

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

#immutable?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'app/models/spree/adjustment.rb', line 116

def immutable?
  state != "open"
end

#promotion?Boolean

Returns:

  • (Boolean)


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

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.



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

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



97
98
99
100
101
102
103
104
105
106
# File 'app/models/spree/adjustment.rb', line 97

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
  calculable = source unless calculable == source
  originator.update_adjustment(self, calculable) if originator.present?
  set_eligibility
end