Method: Amountable::TableMethods#set_amount

Defined in:
lib/amountable/table_methods.rb

#set_amount(name, value) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/amountable/table_methods.rb', line 7

def set_amount(name, value)
  amount = find_amount(name) || amounts.build(name: name)
  amount.value = value.to_money
  if value.zero?
    amounts.delete(amount)
    all_amounts.delete(amount)
    @amounts_by_name.delete(name)
    amount.destroy if amount.persisted?
  else
    all_amounts << amount if amount.new_record?
    (@amounts_by_name ||= {})[name.to_sym] = amount
  end
  amount.value
end