Skip to content

Module WhittakerTech::Midas::Bankable

Extended by: ActiveSupport::Concern

The Bankable module provides currency and monetary value management functionality for Active Record models. It allows models to have associated monetary values (coins) with different currencies and provides convenient methods for setting, retrieving, and formatting these values.

When included in a model, Bankable automatically sets up a polymorphic association to the Midas::Coin model and provides class methods to define specific monetary attributes.

Associations Created

When included, the module automatically creates:

  • midas_coins: A polymorphic has_many association to all Coin records associated with this model instance

Methods Created by has_coin

For each coin defined with has_coin :name, the following methods are created:

  • name: Returns the associated Coin object
  • name_amount: Returns the Money object representing the amount
  • name_format: Returns a formatted string representation of the amount
  • set_name(amount:, currency_code:): Sets the coin value with the given amount and currency

Supported Amount Types

The set_* methods accept amounts in various formats:

  • Money objects: Used directly for cents value
  • Integer: Treated as cents/minor currency units
  • Numeric: Converted to cents using currency-specific decimal places

Currency Configuration

The module uses I18n for currency-specific configuration:

  • midas.ui.currencies.<ISO_CODE>.decimal_count: Decimal places for specific currency
  • midas.ui.defaults.decimal_count: Default decimal places (defaults to 2)

Thread Safety

This module is designed to be thread-safe when used with Rails' standard Active Record patterns.

See also: WhittakerTech::Midas::Coin

  • @since 0.1.0

@example Basic usage

class Product < ApplicationRecord
  include WhittakerTech::Midas::Bankable

  has_coin :price
end

# Create and set a price
product = Product.create!
product.set_price(amount: 29.99, currency_code: 'USD')

# Access the price
product.price          # => Coin object
product.price_amount   # => Money object
product.price_format   # => "$29.99"

@example Multiple coins

class Invoice < ApplicationRecord
  include WhittakerTech::Midas::Bankable

  has_coins :subtotal, :tax, :total
end

@example Custom dependency handling

class Order < ApplicationRecord
  include WhittakerTech::Midas::Bankable

  has_coin :deposit, dependent: :nullify
end