-
Notifications
You must be signed in to change notification settings - Fork 20
/
govuk_rails_compatibile_link_helper.rb
157 lines (123 loc) · 4.65 KB
/
govuk_rails_compatibile_link_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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
require "html_attributes_utils"
module GovukRailsCompatibileLinkHelper
using HTMLAttributesUtils
def govuk_link_classes(*styles, default_class: "#{brand}-link")
if (invalid_styles = (styles - link_styles.keys)) && invalid_styles.any?
fail(ArgumentError, "invalid styles #{invalid_styles.to_sentence}. Valid styles are #{link_styles.keys.to_sentence}")
end
[default_class] + link_styles.values_at(*styles).compact
end
def govuk_button_classes(*styles, default_class: "#{brand}-button")
if (invalid_styles = (styles - button_styles.keys)) && invalid_styles.any?
fail(ArgumentError, "invalid styles #{invalid_styles.to_sentence}. Valid styles are #{button_styles.keys.to_sentence}")
end
[default_class] + button_styles.values_at(*styles).compact
end
def govuk_link_to(name = nil, options = nil, extra_options = {}, &block)
extra_options = options if block_given?
html_options = build_html_options(extra_options)
if block_given?
link_to(name, html_options, &block)
else
link_to(name, options, html_options)
end
end
def govuk_mail_to(email_address, name = nil, extra_options = {}, &block)
extra_options = name if block_given?
html_options = build_html_options(extra_options)
if block_given?
mail_to(email_address, html_options, &block)
else
mail_to(email_address, name, html_options)
end
end
def govuk_button_to(name = nil, options = nil, extra_options = {}, &block)
extra_options = options if block_given?
html_options = {
data: { module: "govuk-button" }
}
if extra_options && extra_options[:prevent_double_click]
html_options[:data]["prevent-double-click"] = "true"
extra_options = extra_options.except(:prevent_double_click)
end
html_options.merge! build_html_options(extra_options, style: :button)
if block_given?
button_to(options, html_options, &block)
else
button_to(name, options, html_options)
end
end
def govuk_button_link_to(name = nil, options = nil, extra_options = {}, &block)
extra_options = options if block_given?
html_options = {
data: { module: "#{brand}-button" },
draggable: 'false',
role: 'button',
}.merge build_html_options(extra_options, style: :button)
if block_given?
link_to(name, html_options, &block)
else
link_to(name, options, html_options)
end
end
def govuk_breadcrumb_link_to(name = nil, options = nil, extra_options = {}, &block)
extra_options = options if block_given?
html_options = build_html_options(extra_options, style: :breadcrumb)
if block_given?
link_to(name, html_options, &block)
else
link_to(name, options, html_options)
end
end
private
def brand
Govuk::Components.brand
end
def link_styles
{
inverse: "#{brand}-link--inverse",
muted: "#{brand}-link--muted",
no_underline: "#{brand}-link--no-underline",
no_visited_state: "#{brand}-link--no-visited-state",
text_colour: "#{brand}-link--text-colour",
}
end
def button_styles
{
disabled: "#{brand}-button--disabled",
secondary: "#{brand}-button--secondary",
warning: "#{brand}-button--warning",
inverse: "#{brand}-button--inverse",
}
end
def build_html_options(provided_options, style: :link)
element_styles = { link: link_styles, button: button_styles }.fetch(style, {})
# we need to take a couple of extra steps here because we don't want the style
# params (inverse, muted, etc) to end up as extra attributes on the link.
remaining_options = remove_styles_from_provided_options(element_styles, provided_options)
style_classes = build_style_classes(style, extract_styles_from_provided_options(element_styles, provided_options))
combine_attributes(remaining_options, class_name: style_classes)
end
def build_style_classes(style, provided_options)
keys = *provided_options&.keys
case style
when :link then govuk_link_classes(*keys)
when :button then govuk_button_classes(*keys)
when :breadcrumb then "#{brand}-breadcrumbs__link"
end
end
def combine_attributes(attributes, class_name:)
attributes ||= {}
attributes.with_indifferent_access.tap do |attrs|
attrs[:class] = Array.wrap(attrs[:class]).prepend(class_name).flatten.join(" ")
end
end
def extract_styles_from_provided_options(styles, provided_options)
return {} if provided_options.blank?
provided_options.slice(*styles.keys)
end
def remove_styles_from_provided_options(styles, provided_options)
return {} if provided_options.blank?
provided_options&.except(*styles.keys)
end
end