Module: MoneyAttribute::MigrationExtensions::TableDefinition

Includes:
Helper
Defined in:
lib/money_attribute/migration_extensions/table_definition.rb

Overview

Migration DSL methods for use inside create_table and change_table blocks.

Included into both ActiveRecord::ConnectionAdapters::TableDefinition and ActiveRecord::ConnectionAdapters::Table.

Examples:

Inside a create_table block

create_table :products do |t|
  t.string :name
  t.money_attribute :price
  t.money_amount :discount, type: :fiat_integer
end

Inside a change_table block

change_table :products do |t|
  t.money_attribute :price, amount: { type: :fiat_integer }
  t.remove_money_attribute :old_price
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

#money_amount(accessor, options = {}) ⇒ void

This method returns an undefined value.

Adds a single amount column within a table definition.

Examples:

t.money_amount :discount
t.money_amount :bonus, column: :bonus_cents, type: :fiat_integer

Parameters:

  • 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



76
77
78
79
80
# File 'lib/money_attribute/migration_extensions/table_definition.rb', line 76

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

  column(amount_column, amount_opts[:type], **amount_opts.except(:type))
end

#money_attribute(accessor, options = {}) ⇒ void

This method returns an undefined value.

Adds amount and currency columns within a table definition.

Examples:

t.money_attribute :price
t.money_attribute :price, amount: { type: :fiat_integer }

Parameters:

  • 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)



41
42
43
44
45
46
# File 'lib/money_attribute/migration_extensions/table_definition.rb', line 41

def money_attribute(accessor, options = {})
  amount_column, currency_column, amount_opts, currency_opts = parse_money_args(accessor, options)

  column(amount_column, amount_opts[:type], **amount_opts.except(:type))
  column(currency_column, :string, **currency_opts)
end

#remove_money_amount(accessor, options = {}) ⇒ void

This method returns an undefined value.

Removes a single amount column within a table definition.

Parameters:

  • accessor (Symbol, String)

    the money attribute accessor name

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

    column options

Options Hash (options):

  • :column (Symbol)

    explicit column name override



88
89
90
91
92
# File 'lib/money_attribute/migration_extensions/table_definition.rb', line 88

def remove_money_amount(accessor, options = {})
  amount_column, = parse_money_amount_args(accessor, options)

  remove_column(amount_column)
end

#remove_money_attribute(accessor, options = {}) ⇒ void

This method returns an undefined value.

Removes amount and currency columns within a table definition.

Parameters:

  • 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+)



55
56
57
58
59
60
# File 'lib/money_attribute/migration_extensions/table_definition.rb', line 55

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

  remove_column(amount_column)
  remove_column(currency_column)
end