Module: EasyRailsMoney::ActiveRecord::Migration::Table
- Defined in:
- lib/easy_rails_money/active_record/migration/table.rb
Instance Method Summary collapse
-
#currency(options = {}) ⇒ Object
Add a common currency column.
-
#money(column_names, options = {}) ⇒ Object
Creates one or two columns to represent a Money object in the named table.
-
#remove_currency ⇒ Object
Removes the common currency column.
-
#remove_money(*column_names) ⇒ Object
Removes the columns which represent the Money object.
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
63 64 65 66 |
# File 'lib/easy_rails_money/active_record/migration/table.rb', line 63 def currency = {} remove_currency_columns column :currency, :string, end |
#money(column_names, options = {}) ⇒ Object
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
27 28 29 30 31 32 |
# File 'lib/easy_rails_money/active_record/migration/table.rb', line 27 def money(column_names, ={}) Array(column_names).each do |name| column "#{name}_money", :integer, column "#{name}_currency", :string, unless has_currency_column? end end |
#remove_currency ⇒ Object
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
If we have defined a currency column for a record then currency column is removed only if no other money column are there
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
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 |