Module: LoanCreatorWeb::ApplicationHelper

Defined in:
lib/loan_creator_web/helpers/application_helper.rb

Instance Method Summary collapse

Instance Method Details

#checked(params:) ⇒ Object



61
62
63
# File 'lib/loan_creator_web/helpers/application_helper.rb', line 61

def checked(params:)
  params ? 'checked' : ''
end

#fix_params_type(params) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/loan_creator_web/helpers/application_helper.rb', line 5

def fix_params_type(params)
  params = params.merge({ initial_values: {} }) if params[:initial_values].nil?

  {}.tap do |h|
    h[:type] = params[:type]
    h[:realistic_durations] = true unless params[:realistic_durations].nil?
    h[:period] = params[:period]
    h[:amount] = to_right_format(params: 'amount', value: params[:amount])
    h[:annual_interests_rate] = to_right_format(params: 'annual_interests_rate', value: params[:annual_interests_rate])
    h[:starts_on] = to_right_format(params: 'starts_on', value: params[:starts_on])
    h[:duration_in_periods] = to_right_format(params: 'duration_in_periods', value: params[:duration_in_periods])
    h[:term_dates] = to_right_format(params: 'term_dates', value: params[:term_dates]) if params[:button] == 'update'
    h[:initial_values] = {}.tap do |ivh|
      ivh[:paid_capital] = to_right_format(params: 'paid_capital', value: params[:initial_values][:paid_capital])
      ivh[:paid_interests] = to_right_format(params: 'paid_interests', value: params[:initial_values][:paid_interests])
      ivh[:accrued_delta_interests] = to_right_format(params: 'accrued_delta_interests', value: params[:initial_values][:accrued_delta_interests])
      ivh[:due_interests] = to_right_format(params: 'due_interests', value: params[:initial_values][:due_interests])
      ivh[:starting_index] = to_right_format(params: 'starting_index', value: params[:initial_values][:starting_index])
    end
  end
end

#initial_values_for_form(initial_value) ⇒ Object



65
66
67
# File 'lib/loan_creator_web/helpers/application_helper.rb', line 65

def initial_values_for_form(initial_value)
  (initial_value.present? && initial_value.zero?) ? nil : initial_value
end

#number_to_euro(number) ⇒ Object



57
58
59
# File 'lib/loan_creator_web/helpers/application_helper.rb', line 57

def number_to_euro(number)
  "#{number} €"
end

#selected(params:, value:) ⇒ Object



53
54
55
# File 'lib/loan_creator_web/helpers/application_helper.rb', line 53

def selected(params:, value:)
  value == params ? 'selected' : ''
end

#to_right_format(params:, value:) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/loan_creator_web/helpers/application_helper.rb', line 27

def to_right_format(params:, value:)
  case params
  when *values_to_f
    value.to_f
  when 'starts_on'
    value.blank? ? nil : Date.parse(value)
  when 'duration_in_periods'
    value.to_i
  when 'starting_index'
    (value.blank? && value.to_i.zero?) ? nil : value.to_i
  when 'term_dates'
    value.values
  end
end

#values_to_fObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/loan_creator_web/helpers/application_helper.rb', line 42

def values_to_f
  %w(
    amount
    annual_interests_rate
    paid_capital
    paid_interests
    accrued_delta_interests
    due_interests
  )
end