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.

Examples:

Adding a composite money attribute

class AddPriceToProducts < ActiveRecord::Migration[8.0]
  def change
    add_money_attribute :products, :price
  end
end

Adding a single-column money amount

class AddDiscountToProducts < ActiveRecord::Migration[8.0]
  def change
    add_money_amount :products, :discount, type: :fiat_integer
  end
end

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

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.

Examples:

Default naming and type

add_money_amount :products, :discount
# => add_column :products, :discount, :decimal, precision: 20, scale: 4

Integer column with explicit name

add_money_amount :products, :bonus, column: :bonus_cents, type: :fiat_integer

Parameters:

  • table_name (Symbol, String)

    the table to alter

  • accessor (Symbol, String)

    the money attribute accessor name

  • options (Hash) (defaults to: {})

    column options

Options Hash (options):

  • :column (Symbol)

    explicit column name override

  • :type (Symbol)

    amount type (+:fiat_decimal+, :crypto_decimal, :fiat_integer)

  • :null (Boolean)

    whether the column allows NULL

  • :default (Object)

    default value for the column



99
100
101
102
103
104
# File 'lib/money_attribute/migration_extensions/schema_statements.rb', line 99

def add_money_amount(table_name, accessor, options = {})
  amount_column, amount_opts = parse_money_amount_args(accessor, options)

  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.

Examples:

Default naming and type

add_money_attribute :products, :price
# => add_column :products, :price, :decimal, precision: 20, scale: 4
# => add_column :products, :price_currency, :string, limit: 20

Custom columns and integer type

add_money_attribute :products, :price,
  amount: { column: :base_price, type: :fiat_integer },
  currency: { column: :base_currency, limit: 3 }

Parameters:

  • table_name (Symbol, String)

    the table to alter

  • accessor (Symbol, String)

    the money attribute accessor name

  • options (Hash) (defaults to: {})

    migration options

Options Hash (options):

  • :amount (Hash)

    amount column options (+:column+, :type, :null, :default)

  • :currency (Hash)

    currency column options (+:column+, :limit, :null, :default)



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, options = {})
  amount_column, currency_column, amount_opts, currency_opts = parse_money_args(accessor, options)

  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.

Parameters:

  • table_name (Symbol, String)

    the table to alter

  • accessor (Symbol, String)

    the money attribute accessor name

  • options (Hash) (defaults to: {})

    column options

Options Hash (options):

  • :column (Symbol)

    explicit column name override



113
114
115
# File 'lib/money_attribute/migration_extensions/schema_statements.rb', line 113

def remove_money_amount(table_name, accessor, options = {})
  remove_column(table_name, (options[: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.

Parameters:

  • table_name (Symbol, String)

    the table to alter

  • accessor (Symbol, String)

    the money attribute accessor name

  • options (Hash) (defaults to: {})

    migration options

Options Hash (options):

  • :amount (Hash)

    amount column options (+:column+)

  • :currency (Hash)

    currency column options (+:column+)



71
72
73
74
75
76
# File 'lib/money_attribute/migration_extensions/schema_statements.rb', line 71

def remove_money_attribute(table_name, accessor, options = {})
  amount_column, currency_column, = parse_money_args(accessor, options)

  remove_column(table_name, amount_column)
  remove_column(table_name, currency_column)
end