Module: Plutonium::Models::HasCents
- Extended by:
- ActiveSupport::Concern
- Included in:
- Resource::Record
- Defined in:
- lib/plutonium/models/has_cents.rb
Overview
Note:
This module automatically handles validation propagation. If a validation error is applied to the cents attribute, the decimal attribute will be marked as invalid.
Note:
The module uses BigDecimal for internal calculations to ensure precision in monetary operations.
HasCents module provides functionality to handle monetary values stored as cents and expose them as decimal values. It also ensures that validations applied to the cents attribute are inherited by the decimal attribute.
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#has_cents_unit_for(decimal_attribute) ⇒ String?
Resolves the currency unit (symbol) configured for a has_cents decimal accessor, e.g.
Instance Method Details
#has_cents_unit_for(decimal_attribute) ⇒ String?
Resolves the currency unit (symbol) configured for a has_cents decimal
accessor, e.g. :price for has_cents :price_cents, unit: "£".
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/plutonium/models/has_cents.rb', line 86 def has_cents_unit_for(decimal_attribute) decimal_attribute = decimal_attribute.to_sym config = self.class.has_cents_attributes.values.find { |opts| opts[:name] == decimal_attribute } return unless config case (unit = config[:unit]) when Symbol then public_send(unit) else unit end end |