Module: Amount::ActiveRecord::MigrationMethods

Defined in:
lib/amount/active_record/migration_methods.rb

Overview

Adds ‘t.amount` to migrations.

Multi-symbol amounts create ‘*_atomic` and `*_symbol` columns. Fixed symbol amounts create only the atomic column.

Examples:

create_table :holdings do |t|
  t.amount :amount
  t.amount :fee, symbol: :SOL
  t.amount :reserve, precision: 40
end

Instance Method Summary collapse

Instance Method Details

#amount(name, precision: 78, symbol: nil, **options) ⇒ void

This method returns an undefined value.

Adds one amount attribute to the table definition.

Parameters:

  • name (Symbol, String)

    logical attribute name

  • precision (Integer) (defaults to: 78)

    numeric precision for the atomic column

  • symbol (Symbol, nil) (defaults to: nil)

    fixed symbol for a single-column amount

  • options (Hash)

    standard column options applied to the generated columns



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/amount/active_record/migration_methods.rb', line 24

def amount(name, precision: 78, symbol: nil, **options)
  default = options.delete(:default)
  null = options.key?(:null) ? options[:null] : nil
  comment = options[:comment]

  defaults = normalize_defaults(default, symbol)

  decimal_options = {
    precision:,
    scale: 0,
    default: defaults[:atomic],
    comment:
  }
  decimal_options[:null] = null unless null.nil?
  decimal(name_to_atomic(name), **decimal_options.compact)

  return if symbol

  string_options = {
    limit: 10,
    default: defaults[:symbol],
    comment:
  }
  string_options[:null] = null unless null.nil?
  string(name_to_symbol(name), **string_options.compact)
end