Module: Gera::ApplicationHelper

Defined in:
app/helpers/gera/application_helper.rb

Constant Summary collapse

MINIMAL_EPSILON =
0.001

Instance Method Summary collapse

Instance Method Details

#currency_rate_cell_data_attr(rate) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/helpers/gera/application_helper.rb', line 33

def currency_rate_cell_data_attr(rate)
  {
    toggle: :popover,
    container: :body,
    content: "Calculation method: #{currency_rate_mode_detailed rate}",
    trigger: :hover,
    html: 'true',
    placement: :bottom,
    animation: false,
    delay: 0,
    href: currency_rate_path(rate)
  }
end

#humanized_currency_rate(rate_value, pair, currency) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/helpers/gera/application_helper.rb', line 7

def humanized_currency_rate(rate_value, pair, currency)
  rate = if rate_value < 1
           format('%.9f', rate_value.to_f)
         else
           format('%.3f', rate_value.to_f)
         end

  if pair.first == currency
    "#{rate} #{currency}" # покупка
  elsif pair.second == currency
    "#{rate} #{currency}" # продажа
  else
    raise
  end
end

#humanized_rate(rate, _currency = nil) ⇒ Object

‘%.12f’ % humanized_money rate.buy_money



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/helpers/gera/application_helper.rb', line 71

def humanized_rate(rate, _currency = nil)
  rate = rate.to_f if rate.is_a? Gera::Rate
  if rate.is_a? Money
    raise 'Dont use Money, it has bad round'
    currency = rate.currency
    rate = rate.to_f
  end
  if rate < 1
    rate1 = format('%.3f', (1.0 / rate))
    "<span class=text-muted>1/</span>#{rate1}".html_safe
  else
    format('%.3f', rate)
    # = '%.12f' % humanized_money rate.buy_money
    # humanized_money Money.from_amount(rate, currency)
  end
end

#humanized_rate_detailed(rate, separator = ' ') ⇒ Object



101
102
103
104
105
106
107
108
# File 'app/helpers/gera/application_helper.rb', line 101

def humanized_rate_detailed(rate, separator = ' ')
  buffer = if rate < 1
             "#{rate}#{separator}#{rate_inversed(rate)}".html_safe
           else
             rate
           end
   :span, buffer.to_s, class: 'text-nowrap'
end

#humanized_rate_text(rate, _currency = nil) ⇒ Object



88
89
90
91
92
93
94
95
# File 'app/helpers/gera/application_helper.rb', line 88

def humanized_rate_text(rate, _currency = nil)
  if rate < 1
    rate1 = format('%.3f', (1.0 / rate))
    "1/#{rate1}".html_safe
  else
    format('%.3f', rate)
  end
end

#rate_cell_with_currency(rate, currency) ⇒ Object



64
65
66
67
68
# File 'app/helpers/gera/application_helper.rb', line 64

def rate_cell_with_currency(rate, currency)
  buffer = [rate_with_currency(rate, currency)]
  buffer << rate_with_currency(rate_inversed(rate), currency) if rate < 1
  [buffer].join('<br/>').html_safe
end

#rate_diff(rv1, rv2, _reverse = false) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'app/helpers/gera/application_helper.rb', line 47

def rate_diff(rv1, rv2, _reverse = false)
  return rate_diff(1.0 / rv1, 1.0 / rv2, true) if rv1 < 1

  diff = rv2 - rv1
  return 0 if diff.abs.round(3) == 0

  p = 100 * diff / rv2

  buffer = "#{format('%.2f', p.round(3))}%"
   :span, buffer, data: { toggle: :tooltip, title: "#{rv2} - #{rv1}" }
end

#rate_inversed(rate) ⇒ Object



97
98
99
# File 'app/helpers/gera/application_helper.rb', line 97

def rate_inversed(rate)
  "<span class='text-muted'>(1/#{1.0 / rate.to_f})</span>".html_safe
end

#rate_source(currency_rate, level = 0) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'app/helpers/gera/application_helper.rb', line 23

def rate_source(currency_rate, level = 0)
  if currency_rate.mode_cross?
    buffer = currency_rate_mode_detailed currency_rate, level
    "⟮ <span class='text-muted'>#{buffer}</span> ⟯".html_safe
  else
    buffer = currency_rate.rate_source.presence || currency_rate.mode
    "(#{buffer})".html_safe
  end
end

#rate_with_currency(rate, currency) ⇒ Object



59
60
61
62
# File 'app/helpers/gera/application_helper.rb', line 59

def rate_with_currency(rate, currency)
  rate = format('%.12f', rate) if rate.is_a?(Float) && rate < MINIMAL_EPSILON
  "#{rate} <span class=text-muted>#{currency}</span>".html_safe
end