Module: EasyRailsMoney::ActiveRecord::Migration::Table

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

Instance Method Summary collapse

Instance Method Details

#currency(options = {}) ⇒ Object

Add a common currency column

Add a common currency column and remove the individual currrency columns if they exist

Returns:

  • is not important and can change



63
64
65
66
# File 'lib/easy_rails_money/active_record/migration/table.rb', line 63

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:



27
28
29
30
31
32
# File 'lib/easy_rails_money/active_record/migration/table.rb', line 27

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

#remove_currencyObject

Removes the 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 removes that common curremcy column. For the existing money column it adds back their currency columns as well. It reflects on the database schema by looking at the column name. By convention the money amount column is prefixed by ‘_money’ and the currency column by ‘_currency’



82
83
84
85
86
87
# File 'lib/easy_rails_money/active_record/migration/table.rb', line 82

def remove_currency
  remove :currency
  money_columns do |money_column|
    column "#{money_column}_currency", "string"
  end
end

#remove_money(*column_names) ⇒ Object

Note:

If we have defined a currency column for a record then currency column is removed only if no other money column are there

Note:

multiple remove_money calls are not supported in one migration. because at this point the schema is different from the migration defined

Removes the columns which represent the Money object

Removes the two columns added by money. The money amount and the currency column. If there are no remaining money amount columns and a common currency column exists. then it is also removed

Parameters:

  • column_names (Array|Symbol|String)

    List of money columns to remove

Returns:

  • is not important and can change

See Also:



49
50
51
52
53
54
55
# File 'lib/easy_rails_money/active_record/migration/table.rb', line 49

def remove_money(*column_names)
  column_names.each do |name|
    remove "#{name}_money"
    remove "#{name}_currency"
  end
  remove_currency unless has_money_columns?
end