-
Notifications
You must be signed in to change notification settings - Fork 9
/
view_helper.rb
125 lines (99 loc) · 3.2 KB
/
view_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
module ViewHelper
include DfE::Autocomplete::ApplicationHelper
def govuk_back_link_to(url = :back, body = 'Back', force_text: false)
classes = 'govuk-!-display-none-print'
url = back_link_url if url == :back
text = if force_text.present?
body
elsif url.to_s.end_with?(candidate_interface_details_path)
'Back to your details'
elsif url.to_s.end_with?(candidate_interface_application_choices_path)
'Back to your applications'
end
text ||= body
render GovukComponent::BackLinkComponent.new(
text: text,
href: url,
classes:,
)
end
def breadcrumbs(breadcrumbs)
render GovukComponent::BreadcrumbsComponent.new(
breadcrumbs:,
hide_in_print: true,
)
end
def break_email_address(email_address)
email_address.gsub(/@/, '<wbr>@').html_safe
end
def bat_contact_mail_to(name = 'becomingateacher<wbr>@digital.education.gov.uk', html_options: {})
govuk_mail_to('becomingateacher@digital.education.gov.uk', name.html_safe, **html_options)
end
def submitted_at_date
dates = ApplicationDates.new(@application_form)
return if dates.submitted_at.nil?
dates.submitted_at.to_fs(:govuk_date).strip
end
def title_with_error_prefix(title, error)
"#{t('page_titles.error_prefix') if error}#{title}"
end
def title_with_success_prefix(title, success)
"#{t('page_titles.success_prefix') if success}#{title}"
end
def format_months_to_years_and_months(number_of_months)
duration_parts = ActiveSupport::Duration.build(number_of_months.months).parts
if duration_parts[:years] && duration_parts[:months]
"#{pluralize(duration_parts[:years], 'year')} and #{pluralize(duration_parts[:months], 'month')}"
elsif duration_parts[:years]
pluralize(duration_parts[:years], 'year')
else
pluralize(number_of_months, 'month')
end
end
def days_since(days)
return unless days.is_a?(Integer)
if days.zero?
'today'
else
"#{pluralize(days, 'day')} ago"
end
end
def boolean_to_word(boolean)
return nil if boolean.nil?
boolean ? 'Yes' : 'No'
end
def percent_of(numerator, denominator)
numerator.to_f / denominator * 100.0
end
def formatted_percentage(count, total)
return '-' if total.zero? && count.positive?
return '0%' if total.zero?
percentage = percent_of(count, total)
precision = (percentage % 1).zero? ? 0 : 2
number_to_percentage(percentage, precision:, strip_insignificant_zeros: true)
end
def protect_against_mistakes(anchor:)
if session[:confirmed_environment_at] && session[:confirmed_environment_at] > 5.minutes.ago
yield
else
govuk_link_to 'Confirm environment to make changes', support_interface_confirm_environment_path(from: [request.fullpath, anchor].join('#'))
end
end
def application_form_path
BackLinks.application_form_path
end
private
def back_link_url
referer = controller.request.env['HTTP_REFERER']
if referer
referer_host = URI(referer).host
if referer_host.present? && referer_host != request.host
service_link
else
referer
end
else
service_link
end
end
end