Class: Spree::Adjustment
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Spree::Adjustment
- Defined in:
- app/models/spree/adjustment.rb
Instance Method Summary collapse
- #currency ⇒ Object
- #display_amount ⇒ Object
-
#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?.
- #immutable? ⇒ Boolean
- #promotion? ⇒ Boolean
-
#set_eligibility ⇒ Object
Update the boolean eligible attribute which determines which adjustments count towards the order’s adjustment_total.
-
#update!(calculable = nil) ⇒ Object
Update both the eligibility and amount of the adjustment.
Instance Method Details
#currency ⇒ Object
104 105 106 |
# File 'app/models/spree/adjustment.rb', line 104 def currency adjustable ? adjustable.currency : Spree::Config[:currency] end |
#display_amount ⇒ Object
108 109 110 |
# File 'app/models/spree/adjustment.rb', line 108 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?
75 76 77 78 |
# File 'app/models/spree/adjustment.rb', line 75 def eligible_for_originator? return true if originator.nil? !originator.respond_to?(:eligible?) || originator.eligible?(source) end |
#immutable? ⇒ Boolean
112 113 114 |
# File 'app/models/spree/adjustment.rb', line 112 def immutable? state != "open" end |
#promotion? ⇒ Boolean
62 63 64 |
# File 'app/models/spree/adjustment.rb', line 62 def promotion? originator_type == 'Spree::PromotionAction' end |
#set_eligibility ⇒ Object
Update the boolean eligible attribute which determines which adjustments count towards the order’s adjustment_total.
68 69 70 71 |
# File 'app/models/spree/adjustment.rb', line 68 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
94 95 96 97 98 99 100 101 102 |
# File 'app/models/spree/adjustment.rb', line 94 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 |