Module: MoneyField::ClassMethods
- Defined in:
- lib/money_extensions/active_record/money_field.rb
Instance Method Summary collapse
- #money(*fields) ⇒ Object
-
#money_field(attribute) ⇒ Object
Add a money field attribute.
- #money_fields(*attributes) ⇒ Object
Instance Method Details
#money(*fields) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/money_extensions/active_record/money_field.rb', line 29 def money(*fields) fields.each do |field| class_eval <<-METHOD def #{field} Money.new(#{field}_in_cents) end METHOD end end |
#money_field(attribute) ⇒ Object
Add a money field attribute
By default it will use attribute_in_cents for cents value
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/money_extensions/active_record/money_field.rb', line 10 def money_field(attribute) module_eval <<-METHOD def #{attribute} Money.new(#{attribute}_in_cents) if #{attribute}_in_cents.present? end # Allow assigning of non money objects directly # Allows form data to be directly passed through # e.g. object.cost = '5.1' def #{attribute}=(money) self.#{attribute}_in_cents = money.try(:to_money).try(:cents) end METHOD end |
#money_fields(*attributes) ⇒ Object
25 26 27 |
# File 'lib/money_extensions/active_record/money_field.rb', line 25 def money_fields(*attributes) attributes.each { |a| money_field(a) } end |