Module: ConcernsOnRails::Models::Monetizable
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/concerns_on_rails/models/monetizable.rb
Overview
Money handling for an integer “subunit” column (e.g. cents) — exact, float-free, via BigDecimal.
Declaring ‘monetizable :price_cents` adds three methods derived from the column name (the `_cents` suffix is stripped):
* `price` — the amount as a BigDecimal (cents / 100)
* `price=` — assign in major units; rounded to whole cents
* `formatted_price` — a display string ("$1,234.56")
class Product < ApplicationRecord
include ConcernsOnRails::Models::Monetizable
monetizable :price_cents # => price / price= / formatted_price
monetizable :shipping_cents, as: :shipping
monetizable :total_cents, unit: "€", separator: ",", delimiter: "."
end
product.price = 19.99 # stores price_cents = 1999
product.price # => 0.1999e2 (BigDecimal 19.99)
product.formatted_price # => "$19.99"
Options: ‘as:` (explicit method name — required when the column does not end in `_cents`), `unit:` (“$”), `precision:` (2), `delimiter:` (“,”), `separator:` (“.”), `subunit_to_unit:` (100).