Module: EasyRailsMoney::ActiveRecord::Migration::TableDefinition

Defined in:
lib/easy_rails_money/active_record/migration/table_definition.rb

Instance Method Summary collapse

Instance Method Details

#currency(options = {}) ⇒ Object

Adds a common currency column

Usually we create an currency column for each money columns. We can have multiple money columns in the same record, in which case we can have a single currency column. This helper creates that common curremcy column

Returns:

  • is not important and can change

See Also:



17
18
19
20
# File 'lib/easy_rails_money/active_record/migration/table_definition.rb', line 17

def currency options={}
  remove_currency_columns
  column :currency, :string, options
end

#money(column_names, options = {}) ⇒ Object

Note:

If we have defined a currency column for a record then only the integer column is defined.

Creates one or two columns to represent a Money object in the named table

An integer column for storing Money in its base unit eg. cents for a Dollar denomination and a string for storing its currency name. Can think of it as a persisted or serialized Money object in the database. The integer column is suffixed with ‘_money’ to aid in reflection ie. to find all money columns. Likewise the currency column is suffixed with ‘_currency’ If does not create an individual currency column if a common currency column is defined

Parameters:

  • column_names (Array|Symbol|String)

    List of money columns to add

Returns:

  • is not important and can change

See Also:



41
42
43
44
45
46
47
48
# File 'lib/easy_rails_money/active_record/migration/table_definition.rb', line 41

def money(column_names, options={})
  Array(column_names).each do |name|
    column "#{name}_money",      :integer, options
    unless columns.select { |x| x.name == "currency" }.any?
      column "#{name}_currency", :string,  options
    end
  end
end