Class: Currency

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.basicObject

Основная валюта



80
81
82
# File 'app/models/currency.rb', line 80

def basic
 @basic ||= first(:conditions => { :basic => true })
end

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

Конвертируем значение из валюты текущей локали к основной валюте в параметрах можно указать локаль из которой можно делать конвертацияю :locale и дату курса :date, курс берется последний найденный до указанной даты Currency.conversion_from_current(100, :locale => “ru”)



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

def conversion_from_current(value, options={})
  load_rate(options)
  convert(value,  @current.char_code, @basic.char_code)
end

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

Конвертируем сумму к валюте текущей локале Если валюта или локаль не найдена то возвращается та же сумма Money.new(value.to_f, “Основаня”).exchange_to(“К Текущей”).to_f Currency.conversion_to_current(100, :locale => “ru”)



64
65
66
67
# File 'app/models/currency.rb', line 64

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

.convert(value, from, to) ⇒ Object



56
57
58
# File 'app/models/currency.rb', line 56

def convert(value, from, to)
  ( Money.new(value.to_f * 10000, from).exchange_to(to).to_f / 100).round(2)
end

.current(current_locale = I18n.locale) ⇒ Object

Текущая валюта



36
37
38
39
# File 'app/models/currency.rb', line 36

def current(current_locale = I18n.locale )
  @current ||= locale(current_locale).first
  @current
end

.current!(current_locale = nil) ⇒ Object



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

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

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



84
85
86
# File 'app/models/currency.rb', line 84

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

.load_rate(options = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'app/models/currency.rb', line 45

def load_rate(options= {})
  current(options[:locale] || I18n.locale)
  basic

  if @rate = @current.currency_converters.get_rate(options[:date] || Time.now)
    add_rate(@basic.char_code,   @current.char_code, @rate.nominal/@rate.value.to_f)
    add_rate(@current.char_code, @basic.char_code,   @rate.value.to_f)
  end

end

Instance Method Details

#basic!Object



14
15
16
# File 'app/models/currency.rb', line 14

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

#locale(need_split = true) ⇒ Object



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

def locale(need_split = true)
  need_split ? read_attribute(:locale).to_s.split(',') : read_attribute(:locale).to_s
end

#locale=(locales) ⇒ Object



18
19
20
# File 'app/models/currency.rb', line 18

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

#reset_basic_currencyObject

Сбрасываем для всех валют флаг “основная”, кроме текущей если она установлена как основная



28
29
30
# File 'app/models/currency.rb', line 28

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