Class: Adjustment

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

Overview

positive or negative. Adjustments have two useful boolean flags

mandatory

If this flag is set to true then it means the the charge is required and will not be removed from the order, even if the amount is zero. In other words a record will be created even if the amount is zero. This is useful for representing things such as shipping and tax charges where you may want to make it explicitly clear that no charge was made for such things.

locked

The charge is never to be udpated. Typically you would want to freeze certain adjustments after checkout. One use case for this is if you want to lock a shipping adjustment so that its value does not change in the future when making other trivial edits to the order (like an email change).

eligible?

This boolean attributes stores whether this adjustment is currently eligible for its order. Only eligible
adjustments count towards the order's adjustment total. This allows an adjustment to be preserved if it
becomes ineligible so it might be reinstated.

Instance Method Summary collapse

Instance Method Details

#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)


49
50
51
52
# File 'app/models/adjustment.rb', line 49

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

#set_eligibilityObject

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



41
42
43
44
45
# File 'app/models/adjustment.rb', line 41

def set_eligibility
  update_attribute_without_callbacks(:eligible,
                                     mandatory ||
                                     (amount != 0 && eligible_for_originator?))
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. The new adjustment amount will be set by by the originator and is not automatically saved. This makes it save to use this method in an after_save hook for other models without causing an infinite recursion problem.



59
60
61
62
63
64
65
# File 'app/models/adjustment.rb', line 59

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