Class: Spree::Adjustment

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

Instance Method Summary collapse

Instance Method Details

#closed?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'app/models/spree/adjustment.rb', line 57

def closed?
  state == "closed"
end

#currencyObject



90
91
92
# File 'app/models/spree/adjustment.rb', line 90

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

#display_amountObject



94
95
96
# File 'app/models/spree/adjustment.rb', line 94

def display_amount
  Spree::Money.new(amount, { currency: currency })
end

#promotion?Boolean

Returns:

  • (Boolean)


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

def promotion?
  source.class < Spree::PromotionAction
end

#update!(target = nil) ⇒ Object

Recalculate amount given a target e.g. Order, Shipment, LineItem

Passing a target here would always be recommended as it would avoid hitting the database again and would ensure you’re compute values over the specific object amount passed here.

Noop if the adjustment is locked.

If the adjustment has no source, do not attempt to re-calculate the amount. Chances are likely that this was a manually created adjustment in the admin backend.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/models/spree/adjustment.rb', line 75

def update!(target = nil)
  return amount if closed?
  if source.present?
    amount = source.compute_amount(target || adjustable)
    self.update_columns(
      amount: amount,
      updated_at: Time.now,
    )
    if promotion?
      self.update_column(:eligible, source.promotion.eligible?(adjustable))
    end
  end
  amount
end