Class: Spree::Adjustment

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

Instance Method Summary collapse

Methods included from DisplayMoney

money_methods

Methods inherited from Base

belongs_to_required_by_default, page, spree_base_scopes

Methods included from Preferences::Preferable

#clear_preferences, #default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference

Instance Method Details

#amount=(amount) ⇒ Object



86
87
88
# File 'app/models/spree/adjustment.rb', line 86

def amount=(amount)
  self[:amount] = Spree::LocalizedNumber.parse(amount)
end

#currencyObject



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

def currency
  adjustable ? adjustable.currency : order.currency
end

#promotion?Boolean

Returns:

  • (Boolean)


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

def promotion?
  source_type == 'Spree::PromotionAction'
end

#update!(target = adjustable) ⇒ Object

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.



101
102
103
104
105
106
107
108
109
# File 'app/models/spree/adjustment.rb', line 101

def update!(target = adjustable)
  return amount if closed? || source.blank?

  amount = source.compute_amount(target)
  attributes = { amount: amount, updated_at: Time.current }
  attributes[:eligible] = source.promotion.eligible?(target) if promotion?
  update_columns(attributes)
  amount
end