Module: Spree::MultiCurrency

Defined in:
lib/spree_multi_currency.rb

Instance Method Summary collapse

Instance Method Details

#multi_currency(*args) ⇒ Object

Order.class_eval do extend MultiCurrency multi_currency :item_total, :total,

              :rate_at_date => lambda{ |t| t.created_at },
              :only_read => true
only_read - define just the getter method (and not the setter)
rate_at_date - use the exchange rate at the specified date


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
41
42
# File 'lib/spree_multi_currency.rb', line 15

def multi_currency(*args)
  options = args.extract_options!

  [args].flatten.compact.each do |number_field|

    # define for example method price
    define_method(number_field.to_sym) do
      num_field = read_attribute(number_field.to_sym)
      if options.has_key?(:rate_at_date) &&
            options[:rate_at_date].is_a?(Proc)
        Spree::Currency.conversion_to_current(
          num_field,
          { date: options[:rate_at_date].call(self) }
        )
      else
        Spree::Currency.conversion_to_current(num_field)
      end
    end

    unless options[:only_read]
      define_method(:"#{number_field}=") do |value|
        write_attribute(number_field.to_sym,
                        Spree::Currency.conversion_from_current(value))
      end
    end

  end
end