Module: ActiveRecord::Extensions::ClassMethods
- Defined in:
- lib/money_extensions/active_record/extensions.rb
Instance Method Summary collapse
-
#has_currency(*args) ⇒ Object
Assign a :currency (Money::Currency object) reader/writer for the given field name.
Instance Method Details
#has_currency(*args) ⇒ Object
Assign a :currency (Money::Currency object) reader/writer for the given field name.
NOTES:
-
Currency is identified by a 3-letter ISO code,
-
Lookup is doen via Money::Currency.find(iso_code)
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/money_extensions/active_record/extensions.rb', line 14 def has_currency(*args) = args. attr_name = args.first || :currency field_name = "#{attr_name}_iso_code" composed_of attr_name, class_name: 'Money::Currency', mapping: [field_name, 'iso_code'], allow_nil: true, constructor: proc { |value| Money::Currency.new(value) unless value.blank? } scope :for_currency, lambda { |currency| where(currency_iso_code: currency.iso_code) } if [:default] before_validation :set_default_currency class_eval <<-METHOD def set_default_currency self.currency ||= EnabledCurrency.base_currency true end METHOD end validates_presence_of :currency_iso_code if [:required] end |