Module: EasyRailsMoney::ActiveRecord::Migration::SchemaStatements
- Defined in:
- lib/easy_rails_money/active_record/migration/schema_statements.rb
Instance Method Summary collapse
-
#add_money(table_name, *args) ⇒ Object
Creates one or two columns to represent a Money object in the named table.
-
#remove_currency(table_name, options = {}) ⇒ Object
Removes the common currency column.
-
#remove_money(table_name, *args) ⇒ Object
Removes the columns which represent the Money object.
Instance Method Details
#add_money(table_name, *args) ⇒ 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 33 |
# File 'lib/easy_rails_money/active_record/migration/schema_statements.rb', line 27 def add_money table_name, *args = args. Array(args).each do |name| add_column table_name, "#{name}_money", :integer, add_column table_name, "#{name}_currency", :string, unless has_currency_column?(table_name) end end |
#remove_currency(table_name, options = {}) ⇒ 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’
76 77 78 79 80 81 |
# File 'lib/easy_rails_money/active_record/migration/schema_statements.rb', line 76 def remove_currency table_name, ={} remove_column table_name, :currency money_columns(table_name) do |money_column| add_column table_name, "#{money_column}_currency", :string, end end |
#remove_money(table_name, *args) ⇒ 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 add_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
50 51 52 53 54 55 56 57 |
# File 'lib/easy_rails_money/active_record/migration/schema_statements.rb', line 50 def remove_money table_name, *args = args. Array(args).each do |name| remove_column table_name, "#{name}_money" remove_column table_name, "#{name}_currency" end remove_currency table_name, unless has_money_columns?(table_name) end |