Class: Spree::Currency

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/spree/currency.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all_currenciesObject

return array of all char_codes



49
50
51
# File 'app/models/spree/currency.rb', line 49

def all_currencies
    all.map(&:char_code)
end

.basicObject

Retrieves the main currency.



146
147
148
# File 'app/models/spree/currency.rb', line 146

def basic
  @basic ||= where(basic: true).first
end

.conversion_from_current(value, options = {}) ⇒ Object

Converts the currency value of the current locale to the basic currency In the parameters you can specify the locale you wish to convert FROM. Usage: Currency.conversion_from_current(100, :locale => “da”)



130
131
132
133
134
135
136
# File 'app/models/spree/currency.rb', line 130

def conversion_from_current(value, options = {})
  load_rate(options)
  convert(parse_price(value), @current.char_code, @basic.char_code)
rescue => ex
  error_logger(ex)
  value
end

.conversion_to_current(value, options = { }) ⇒ Object

Converts the basic currency value to a ‘localized’ value. In the parameters you can specify the locale you wish to convert TO. Usage: Currency.conversion_to_current(100, :locale => “da”)



119
120
121
122
123
124
125
# File 'app/models/spree/currency.rb', line 119

def conversion_to_current(value, options = { })
  load_rate(options)
  convert(value, @basic.char_code, @current.char_code)
rescue => ex
  error_logger(ex)
  value
end

.convert(value, from, to) ⇒ Object

Exchanges money between two currencies. E.g. with these args: 150, DKK, GBP returns 16.93



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/models/spree/currency.rb', line 96

def convert(value, from, to)
  begin
    from_money = ::Money.new(value.to_f * 10000, from)
    res = from_money.exchange_to(to)
    res = (res.to_f / 100).round(2)
  rescue => e
    load_rate_from(from)
    begin
      currency_config = Spree::Config.currency
      res = from_money.exchange_to(currency_config)
      load_rate_from(to)
      res = ::Money.new(res, currency_config).exchange_to(to)
      res = res.to_f
    rescue => e
      raise "Require load actual currency \t\n #{e}"
    end
  end
  res
end

.current(current_locale = nil) ⇒ Object

Get the current locale



54
55
56
57
58
59
60
61
62
63
64
# File 'app/models/spree/currency.rb', line 54

def current(current_locale = nil)
  @current ||= locale(current_locale || I18n.locale).first
  if @current
    return @current
  else
    mess = 'Require load and set default currency'
    mess << '<br/>'
    mess << 'Locale field is factor for determine current currency'
    raise mess
  end
end

.current!(current_locale = nil) ⇒ Object



66
67
68
69
70
71
72
# File 'app/models/spree/currency.rb', line 66

def current!(current_locale = nil)
  if current_locale.is_a?(Spree::Currency)
    @current = current_locale
  else
    @current = locale(current_locale || I18n.locale).first
  end
end

.error_logger(ex) ⇒ Object

write to error log ex - Exception from ‘rescue => e’



140
141
142
143
# File 'app/models/spree/currency.rb', line 140

def error_logger(ex)
  mes = " [ Currency ] :#{ex.inspect} \n #{ex.backtrace.join('\n ')}"
  Rails.logger.error mes
end

.get(num_code, options = {}) ⇒ Object



150
151
152
# File 'app/models/spree/currency.rb', line 150

def get(num_code, options = {})
  find_by_num_code(num_code) || create(options)
end

.load_rate(options = {}) ⇒ Object



74
75
76
77
# File 'app/models/spree/currency.rb', line 74

def load_rate(options = {})
  current(options[:locale] || I18n.locale)
  load_rate_from(@current.char_code)
end

.load_rate_from(from_char_code) ⇒ Object

load rate for currency(char_code) to basic



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/models/spree/currency.rb', line 80

def load_rate_from(from_char_code)
  from_cur = Spree::Currency.find_by_char_code(from_char_code)
  basic = Spree::Currency.basic
  rate = from_cur.currency_converters.get_rate(Time.now)
  if rate
    add_rate(basic.char_code,
             from_cur.char_code,
             rate.nominal/rate.value.to_f)
    add_rate(from_cur.char_code,
             basic.char_code,
             rate.value.to_f)
  end
end

Instance Method Details

#basic!Object

FIXME must be transaction



21
22
23
# File 'app/models/spree/currency.rb', line 21

def basic!
  self.class.update_all(basic: false) && update_attribute(:basic, true)
end

#locale(need_split = true) ⇒ Object



29
30
31
32
33
34
35
36
# File 'app/models/spree/currency.rb', line 29

def locale(need_split = true)
  locale_var = read_attribute(:locale).to_s
  if need_split
    locale_var.split(',')
  else
    locale_var
  end
end

#locale=(locales) ⇒ Object



25
26
27
# File 'app/models/spree/currency.rb', line 25

def locale=(locales)
  write_attribute(:locale, [locales].flatten.compact.join(','))
end

#reset_basic_currencyObject

We can only have one main currency. Therefore we reset all other currencies but the current if it’s the main.



40
41
42
43
44
# File 'app/models/spree/currency.rb', line 40

def reset_basic_currency
  if basic?
    self.class.where("id != ?", id).update_all(basic: false)
  end
end