module MoneyColumn::ActiveRecordHooks

Public Class Methods

included(base) click to toggle source
# File lib/money_column/active_record_hooks.rb, line 4
def self.included(base)
  base.extend(ClassMethods)
end

Public Instance Methods

initialize_dup(*) click to toggle source
Calls superclass method
# File lib/money_column/active_record_hooks.rb, line 13
def initialize_dup(*)
  @money_column_cache = {}
  super
end
reload(*) click to toggle source
Calls superclass method
# File lib/money_column/active_record_hooks.rb, line 8
def reload(*)
  clear_money_column_cache if persisted?
  super
end

Private Instance Methods

clear_money_column_cache() click to toggle source
# File lib/money_column/active_record_hooks.rb, line 20
def clear_money_column_cache
  @money_column_cache.clear
end
init_internals() click to toggle source
Calls superclass method
# File lib/money_column/active_record_hooks.rb, line 24
def init_internals
  @money_column_cache = {}
  super
end
read_money_attribute(column) click to toggle source
# File lib/money_column/active_record_hooks.rb, line 29
def read_money_attribute(column)
  column = column.to_s
  options = self.class.money_column_options[column]

  return @money_column_cache[column] if @money_column_cache[column]

  value = self[column]

  return if value.nil? && !options[:coerce_null]

  @money_column_cache[column] = Money.new(value, options[:currency] || send(options[:currency_column]))
end
write_money_attribute(column, money) click to toggle source
# File lib/money_column/active_record_hooks.rb, line 42
def write_money_attribute(column, money)
  column = column.to_s
  options = self.class.money_column_options[column]

  @money_column_cache[column] = nil

  if money.blank?
    return self[column] = nil
  end

  currency_raw_source = options[:currency] || (send(options[:currency_column]) rescue nil)

  if !money.is_a?(Money)
    return self[column] = Money.new(money, currency_raw_source).value
  end

  if options[:currency_read_only]
    currency_source = Money::Helpers.value_to_currency(currency_raw_source)
    if currency_raw_source && !money.currency.compatible?(currency_source)
      Money.deprecate("[money_column] currency mismatch between #{currency_source} and #{money.currency} in column #{column}.")
    end
  else
    self[options[:currency_column]] = money.currency.to_s unless money.no_currency?
  end

  self[column] = money.value
end