Module: MoneyColumn::ActiveRecordHooks::ClassMethods

Defined in:
lib/money_column/active_record_hooks.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#money_column_optionsObject (readonly)

Returns the value of attribute money_column_options.



77
78
79
# File 'lib/money_column/active_record_hooks.rb', line 77

def money_column_options
  @money_column_options
end

Instance Method Details

#money_column(*columns, currency_column: nil, currency: nil, currency_read_only: false, coerce_null: false) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/money_column/active_record_hooks.rb', line 79

def money_column(*columns, currency_column: nil, currency: nil, currency_read_only: false, coerce_null: false)
  @money_column_options ||= {}

  options = normalize_money_column_options(
    currency_column: currency_column,
    currency: currency,
    currency_read_only: currency_read_only,
    coerce_null: coerce_null,
  )

  if options[:currency_column]
    clear_cache_on_currency_change(options[:currency_column])
  end

  columns.flatten.each do |column|
    column_string = column.to_s.freeze

    @money_column_options[column_string] = options

    attribute(column_string, MoneyColumn::ActiveRecordType.new)

    define_method(column) do
      read_money_attribute(column_string)
    end

    define_method("#{column}=") do |money|
      write_money_attribute(column_string, money)
    end
  end
end