Class: Adjustment

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

Overview

Adjustments represent a change to the item_total of an Order. Each adjustment has an amount that be either 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).

Instance Method Summary collapse

Instance Method Details

#applicable?Boolean

Checks if adjustment is applicable for the order. Should return true if adjustment should be preserved and false if removed. Default behaviour is to preserve adjustment if amount is present and non 0. Exceptions are made if the adjustment is considered mandatory.

Returns:

  • (Boolean)


34
35
36
# File 'app/models/adjustment.rb', line 34

def applicable?
  mandatory || amount != 0
end

#update!Object

Tells the adjustment that its time to update itself. Adjustments will delegate this request 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. If there is no originator then this method will have no effect.



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

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