Module: MoneyAttribute::MigrationExtensions::SchemaStatements
- Includes:
- Helper
- Defined in:
- lib/money_attribute/migration_extensions/schema_statements.rb
Overview
Migration helper methods for ActiveRecord::Migration.
Provides reversible methods to add and remove money attribute columns
from within a change migration block.
Constant Summary
Constants included from Helper
Helper::AMOUNT_CONFIG, Helper::CURRENCY_DEFAULT_LIMIT, Helper::CURRENCY_MIN_LIMIT
Instance Attribute Summary
Attributes included from Helper
#AMOUNT_CONFIG, #CURRENCY_DEFAULT_LIMIT, #CURRENCY_MIN_LIMIT
Instance Method Summary collapse
-
#add_money_amount(table_name, accessor, options = {}) ⇒ void
Adds a single amount column for a fixed-currency money attribute.
-
#add_money_attribute(table_name, accessor, options = {}) ⇒ void
Adds an amount column and a currency column for a composite money attribute.
-
#remove_money_amount(table_name, accessor, options = {}) ⇒ void
Removes the amount column for a fixed-currency money attribute.
-
#remove_money_attribute(table_name, accessor, options = {}) ⇒ void
Removes the amount and currency columns for a composite money attribute.
Instance Method Details
#add_money_amount(table_name, accessor, options = {}) ⇒ void
This method returns an undefined value.
Adds a single amount column for a fixed-currency money attribute.
No currency column is created — the application default currency is used for all rows.
99 100 101 102 103 104 |
# File 'lib/money_attribute/migration_extensions/schema_statements.rb', line 99 def add_money_amount(table_name, accessor, = {}) amount_column, amount_opts = parse_money_amount_args(accessor, ) type = amount_opts.delete(:type) add_column(table_name, amount_column, type, **amount_opts) end |
#add_money_attribute(table_name, accessor, options = {}) ⇒ void
This method returns an undefined value.
Adds an amount column and a currency column for a composite money attribute.
The amount column type is determined by the :type option inside
amount: { type: } (defaults to :fiat_decimal). The currency column
is a string with a configurable limit.
52 53 54 55 56 57 58 |
# File 'lib/money_attribute/migration_extensions/schema_statements.rb', line 52 def add_money_attribute(table_name, accessor, = {}) amount_column, currency_column, amount_opts, currency_opts = parse_money_args(accessor, ) type = amount_opts.delete(:type) add_column(table_name, amount_column, type, **amount_opts) add_column(table_name, currency_column, :string, **currency_opts) end |
#remove_money_amount(table_name, accessor, options = {}) ⇒ void
This method returns an undefined value.
Removes the amount column for a fixed-currency money attribute.
113 114 115 |
# File 'lib/money_attribute/migration_extensions/schema_statements.rb', line 113 def remove_money_amount(table_name, accessor, = {}) remove_column(table_name, ([:column] || accessor).to_s) end |
#remove_money_attribute(table_name, accessor, options = {}) ⇒ void
This method returns an undefined value.
Removes the amount and currency columns for a composite money attribute.
Accepts the same :amount and :currency options as
#add_money_attribute to identify the columns.
71 72 73 74 75 76 |
# File 'lib/money_attribute/migration_extensions/schema_statements.rb', line 71 def remove_money_attribute(table_name, accessor, = {}) amount_column, currency_column, = parse_money_args(accessor, ) remove_column(table_name, amount_column) remove_column(table_name, currency_column) end |