MoneyFilter

Includes:
ActionView::Helpers::NumberHelper
Defined in:
app/filters/money_filter.rb

Overview

Filtros para auxiliar a apresentação de valores monetários para os usuários.

Instance Method Summary (collapse)

Instance Method Details

- (String) money(number)

Retorna o valor convertido para o moeda.

Parameters:

  • number (Integer)

    valor em centavos

Returns:

  • (String)

    preço formatado em Real



10
11
12
# File 'app/filters/money_filter.rb', line 10

def money(number)
  number.is_a?(Numeric) ? number_to_currency(number / 100.0) : number
end

- (String) money_option_presentation(number, kind, period, price_format, installments)

Retorna um string humanizada para apresentar a opção ao usuário

Parameters:

  • number (Integer)

    valor em centavos

  • kind (String)

    tipo de pagamento

  • period (String)

    período de recorrência

  • installments (number)

    número de parcelas para exibir o preço

  • price_format (number)

    string full ou installments, indicando como formatá-lo

Returns:

  • (String)

    preço formatado em uma string apresentável ao usuário



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/filters/money_filter.rb', line 49

def money_option_presentation(number, kind, period, price_format, installments)
  count = installments
  count = 1 if price_format == 'full'

  price = case kind
          when 'common' then money(number / count)
          when 'credit' then number_with_precision(number, precision: 0, delimiter: '.')
          else               money(number)
          end

  t("product.pricing.#{kind}.#{period}",
    'count' => count, 'price' => price)
end

- (Object) money_option_presentation_with_taxes(number, kind, period, gateway)



63
64
65
66
67
68
# File 'app/filters/money_filter.rb', line 63

def money_option_presentation_with_taxes(number, kind, period, gateway)
  number = number_with_taxes(number, gateway) if gateway && gateway['price_format'] == 'installments'

  money_option_presentation number, kind, period, gateway.try(:fetch, 'price_format', nil),
    gateway.try(:fetch, 'installments_limit', nil)
end

- (String) money_without_currency(number)

Retorna o valor convertido para o moeda sem a unidade.

Parameters:

  • number (Integer)

    valor em centavos

Returns:

  • (String)

    preço formatado em Real sem unidade



36
37
38
# File 'app/filters/money_filter.rb', line 36

def money_without_currency(number)
  number.is_a?(Numeric) ? number_to_currency(number / 100.0, unit: '').strip : number
end

- (String) money_without_trailing_zeros(number)

Retorna o valor convertido para o moeda sem os zeros a direita. Note que só os zeros vão ser removidos, outros numeros vão continuar.

Parameters:

  • number (Integer)

    valor em centavos

Returns:

  • (String)

    preço formatado em Real sem zeros



20
21
22
23
24
25
26
27
28
29
# File 'app/filters/money_filter.rb', line 20

def money_without_trailing_zeros(number)
  if number.is_a?(Numeric)
    calculated = number / 100.0
    result     = number_to_currency calculated

    calculated % 1 == 0 ? result.remove(',00') : result
  else
    number
  end
end

- (Object) number_with_taxes(number, gateway, installments_count = nil)



70
71
72
73
74
75
76
77
78
# File 'app/filters/money_filter.rb', line 70

def number_with_taxes(number, gateway, installments_count = nil)
  installments_count ||= gateway['installments_limit']

  if gateway['installments_forward_taxes']
    number * tax_for(gateway, installments_count)
  else
    number
  end
end

- (Object) payment_option_to_subscription_sentence(payment_option)



86
87
88
89
90
91
92
# File 'app/filters/money_filter.rb', line 86

def payment_option_to_subscription_sentence(payment_option)
  price = money(payment_option['price'])
  pricing = t("pages.orders.recurring.pricing.#{payment_option['period']}", 'price' => price)
  type = t('pages.orders.recurring.subscription')

  "#{type} - #{pricing}"
end

- (Object) payment_options_to_sentence(options, price_format, installments)



80
81
82
83
84
# File 'app/filters/money_filter.rb', line 80

def payment_options_to_sentence(options, price_format, installments)
  options.map do |opt|
    money_option_presentation(opt['price'], opt['kind'], opt['period'], price_format, installments)
  end.to_sentence
end