module ActiveRecord::Extensions::ClassMethods

Public Instance Methods

has_currency(*args) click to toggle source

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)

# File lib/money_extensions/active_record/extensions.rb, line 14
      def has_currency(*args)
        options = args.extract_options!
        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 options[: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 options[:required]
      end