crown_marketplace_utils 0.1.0.beta.6 → 0.1.0.beta.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (30) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +1 -1
  3. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/accordion.rb +91 -89
  4. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/back_link.rb +24 -22
  5. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/breadcrumbs.rb +56 -54
  6. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/button.rb +109 -107
  7. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/details.rb +33 -31
  8. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/error_message.rb +49 -47
  9. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/field/character_count.rb +144 -142
  10. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/field/checkboxes.rb +176 -174
  11. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/field/date_input.rb +126 -124
  12. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/field/file_upload.rb +75 -73
  13. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/field/input.rb +137 -135
  14. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/field/radios.rb +176 -174
  15. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/field/select.rb +111 -109
  16. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/field/textarea.rb +86 -84
  17. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/field.rb +181 -179
  18. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/fieldset.rb +56 -54
  19. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/form_group.rb +37 -35
  20. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/header.rb +142 -140
  21. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/hint.rb +25 -23
  22. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/label.rb +69 -67
  23. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/notification_banner.rb +121 -119
  24. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/pagination.rb +290 -288
  25. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/step_by_step_navigation.rb +187 -185
  26. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/tag.rb +29 -27
  27. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/warning_text.rb +39 -37
  28. data/lib/crown_marketplace_utils/helpers/gov_uk_helper.rb +31 -29
  29. data/lib/crown_marketplace_utils/version.rb +1 -1
  30. metadata +1 -1
@@ -2,201 +2,203 @@
2
2
 
3
3
  require_relative '../field'
4
4
 
5
- module CrownMarketplaceUtils::Helpers
6
- module GovUkHelper
7
- module Field
8
- # = GOV.UK Radios
9
- #
10
- # This helper is used for generating the radios component from the
11
- # {https://design-system.service.gov.uk/components/radios GDS - Components - Radios}
12
- #
13
- # This is considered a Field module and so makes use of the methods in {CrownMarketplaceUtils::GovUkHelper::Field}
14
-
15
- module Radios
16
- include Field
17
-
18
- # Generates the HTML for the GOV.UK Radios component
19
- #
20
- # @param attribute [String, Symbol] the attribute of the raido buttons
21
- # @param items [Array] array of radio items, see {_govuk_radios_fields}
22
- # @param govuk_radios_options [Hash] options that will be used for the parts of the fieldset, form group, hint and radio buttons
23
- #
24
- # @option govuk_radios_options [String] :error_message (nil) the error message to be displayed
25
- # @option govuk_radios_options [ActiveModel] :model (nil) optional model that can be used to find an error message
26
- # @option govuk_radios_options [ActionView::Helpers::FormBuilder] :form (nil) optional form builder used to create
27
- # the radio tags and find the error message
28
- # @option govuk_radios_options [Hash] :form_group see {govuk_fields}
29
- # @option govuk_radios_options [Hash] :fieldset see {govuk_fields}
30
- # @option govuk_radios_options [Hash] :hint see {govuk_field}
31
- # @option govuk_radios_options [Hash] :radios ({}) the options that will be used when rendering the radio buttons.
32
- # See {govuk_radio_fields_tag} for more details.
33
- #
34
- # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Radios
35
- # which can then be rendered on the page
36
-
37
- def govuk_radios(attribute, items, **govuk_radios_options)
38
- govuk_fields(:radios, attribute, **govuk_radios_options) do |govuk_field_options|
39
- if govuk_radios_options[:model] || govuk_radios_options[:form]
40
- value = (govuk_radios_options[:model] || govuk_radios_options[:form].object).send(attribute)
41
- items.each { |item| item[:checked] = item[:value] == value }
42
- end
43
-
44
- concat(
45
- if govuk_radios_options[:form]
46
- govuk_radio_fields_form(govuk_radios_options[:form], attribute, items, **govuk_field_options)
47
- else
48
- govuk_radio_fields_tag(attribute, items, **govuk_field_options)
5
+ module CrownMarketplaceUtils
6
+ module Helpers
7
+ module GovUkHelper
8
+ module Field
9
+ # = GOV.UK Radios
10
+ #
11
+ # This helper is used for generating the radios component from the
12
+ # {https://design-system.service.gov.uk/components/radios GDS - Components - Radios}
13
+ #
14
+ # This is considered a Field module and so makes use of the methods in {CrownMarketplaceUtils::GovUkHelper::Field}
15
+
16
+ module Radios
17
+ include Field
18
+
19
+ # Generates the HTML for the GOV.UK Radios component
20
+ #
21
+ # @param attribute [String, Symbol] the attribute of the raido buttons
22
+ # @param items [Array] array of radio items, see {_govuk_radios_fields}
23
+ # @param govuk_radios_options [Hash] options that will be used for the parts of the fieldset, form group, hint and radio buttons
24
+ #
25
+ # @option govuk_radios_options [String] :error_message (nil) the error message to be displayed
26
+ # @option govuk_radios_options [ActiveModel] :model (nil) optional model that can be used to find an error message
27
+ # @option govuk_radios_options [ActionView::Helpers::FormBuilder] :form (nil) optional form builder used to create
28
+ # the radio tags and find the error message
29
+ # @option govuk_radios_options [Hash] :form_group see {govuk_fields}
30
+ # @option govuk_radios_options [Hash] :fieldset see {govuk_fields}
31
+ # @option govuk_radios_options [Hash] :hint see {govuk_field}
32
+ # @option govuk_radios_options [Hash] :radios ({}) the options that will be used when rendering the radio buttons.
33
+ # See {govuk_radio_fields_tag} for more details.
34
+ #
35
+ # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Radios
36
+ # which can then be rendered on the page
37
+
38
+ def govuk_radios(attribute, items, **govuk_radios_options)
39
+ govuk_fields(:radios, attribute, **govuk_radios_options) do |govuk_field_options|
40
+ if govuk_radios_options[:model] || govuk_radios_options[:form]
41
+ value = (govuk_radios_options[:model] || govuk_radios_options[:form].object).send(attribute)
42
+ items.each { |item| item[:checked] = item[:value] == value }
49
43
  end
50
- )
51
- end
52
- end
53
44
 
54
- private
55
-
56
- # Generates the radios HTML for {govuk_radios}
57
- #
58
- # @param attribute [String, Symbol] the attribute of the raido buttons
59
- # @param items [Array] array of radio items, see {_govuk_radios_fields}
60
- # @param govuk_radios_options [Hash] options that will be used in customising the HTML
61
- #
62
- # @option (see _govuk_radios_fields)
63
- #
64
- # @return [ActiveSupport::SafeBuffer] the HTML for the radio buttons which is used in {govuk_radios}
65
-
66
- def govuk_radio_fields_tag(attribute, items, **govuk_radios_options)
67
- _govuk_radios_fields(items, **govuk_radios_options) do |radio_item|
68
- govuk_radio_item_tag(attribute, radio_item)
45
+ concat(
46
+ if govuk_radios_options[:form]
47
+ govuk_radio_fields_form(govuk_radios_options[:form], attribute, items, **govuk_field_options)
48
+ else
49
+ govuk_radio_fields_tag(attribute, items, **govuk_field_options)
50
+ end
51
+ )
52
+ end
69
53
  end
70
- end
71
54
 
72
- # Generates the radios HTML for {govuk_radios} when there is a ActionView::Helpers::FormBuilder
73
- #
74
- # @param form [ActionView::Helpers::FormBuilder] the form builder used to create the radio buttons
75
- # @param (see govuk_radio_fields_tag)
76
- #
77
- # @option (see _govuk_radios_fields)
78
- #
79
- # @return (see govuk_radio_fields_tag)
80
-
81
- def govuk_radio_fields_form(form, attribute, items, **govuk_radios_options)
82
- _govuk_radios_fields(items, **govuk_radios_options) do |radio_item|
83
- govuk_radio_item_form(form, attribute, radio_item)
55
+ private
56
+
57
+ # Generates the radios HTML for {govuk_radios}
58
+ #
59
+ # @param attribute [String, Symbol] the attribute of the raido buttons
60
+ # @param items [Array] array of radio items, see {_govuk_radios_fields}
61
+ # @param govuk_radios_options [Hash] options that will be used in customising the HTML
62
+ #
63
+ # @option (see _govuk_radios_fields)
64
+ #
65
+ # @return [ActiveSupport::SafeBuffer] the HTML for the radio buttons which is used in {govuk_radios}
66
+
67
+ def govuk_radio_fields_tag(attribute, items, **govuk_radios_options)
68
+ _govuk_radios_fields(items, **govuk_radios_options) do |radio_item|
69
+ govuk_radio_item_tag(attribute, radio_item)
70
+ end
84
71
  end
85
- end
86
-
87
- # Wrapper method used by {govuk_radio_fields_tag} and {govuk_radio_fields_form} to generate the radios HTML
88
- #
89
- # @param items [Array] array of radio items.
90
- # Each item is a hash which can be:
91
- # - +:divider+ text to separate radio items
92
- # - radio item, see {_govuk_radio_item}
93
- # @param govuk_radios_options [Hash] options that will be used in customising the HTML
94
- #
95
- # @option govuk_radios_options [String] :classes additional CSS classes for the radios HTML
96
- # @option govuk_radios_options [Hash] :attributes ({ module: 'govuk-radios' }) any additional attributes that will added as part of the HTML
97
- #
98
- # @yield the radio item HTML generated by {govuk_radio_fields_tag} or {govuk_radio_fields_form}
99
- #
100
- # @yieldparam radio_item [Hash] the current radio item to be rendered
101
- #
102
- # @return [ActiveSupport::SafeBuffer] the HTML for the radio buttons which is used in {govuk_radio_fields_tag} or {govuk_radio_fields_form}
103
72
 
104
- def _govuk_radios_fields(items, **govuk_radios_options)
105
- govuk_radios_options[:attributes] ||= {}
106
- govuk_radios_options[:attributes][:class] = "govuk-radios #{govuk_radios_options[:classes]}".rstrip
107
- (govuk_radios_options[:attributes][:data] ||= {}).merge!({ module: 'govuk-radios' })
73
+ # Generates the radios HTML for {govuk_radios} when there is a ActionView::Helpers::FormBuilder
74
+ #
75
+ # @param form [ActionView::Helpers::FormBuilder] the form builder used to create the radio buttons
76
+ # @param (see govuk_radio_fields_tag)
77
+ #
78
+ # @option (see _govuk_radios_fields)
79
+ #
80
+ # @return (see govuk_radio_fields_tag)
81
+
82
+ def govuk_radio_fields_form(form, attribute, items, **govuk_radios_options)
83
+ _govuk_radios_fields(items, **govuk_radios_options) do |radio_item|
84
+ govuk_radio_item_form(form, attribute, radio_item)
85
+ end
86
+ end
108
87
 
109
- tag.div(**govuk_radios_options[:attributes]) do
110
- capture do
111
- items.each do |radio_item|
112
- concat(
113
- if radio_item[:divider]
114
- tag.div(radio_item[:divider], class: 'govuk-radios__divider')
115
- else
116
- tag.div(class: 'govuk-radios__item') do
117
- yield(radio_item)
88
+ # Wrapper method used by {govuk_radio_fields_tag} and {govuk_radio_fields_form} to generate the radios HTML
89
+ #
90
+ # @param items [Array] array of radio items.
91
+ # Each item is a hash which can be:
92
+ # - +:divider+ text to separate radio items
93
+ # - radio item, see {_govuk_radio_item}
94
+ # @param govuk_radios_options [Hash] options that will be used in customising the HTML
95
+ #
96
+ # @option govuk_radios_options [String] :classes additional CSS classes for the radios HTML
97
+ # @option govuk_radios_options [Hash] :attributes ({ module: 'govuk-radios' }) any additional attributes that will added as part of the HTML
98
+ #
99
+ # @yield the radio item HTML generated by {govuk_radio_fields_tag} or {govuk_radio_fields_form}
100
+ #
101
+ # @yieldparam radio_item [Hash] the current radio item to be rendered
102
+ #
103
+ # @return [ActiveSupport::SafeBuffer] the HTML for the radio buttons which is used in {govuk_radio_fields_tag} or {govuk_radio_fields_form}
104
+
105
+ def _govuk_radios_fields(items, **govuk_radios_options)
106
+ govuk_radios_options[:attributes] ||= {}
107
+ govuk_radios_options[:attributes][:class] = "govuk-radios #{govuk_radios_options[:classes]}".rstrip
108
+ (govuk_radios_options[:attributes][:data] ||= {}).merge!({ module: 'govuk-radios' })
109
+
110
+ tag.div(**govuk_radios_options[:attributes]) do
111
+ capture do
112
+ items.each do |radio_item|
113
+ concat(
114
+ if radio_item[:divider]
115
+ tag.div(radio_item[:divider], class: 'govuk-radios__divider')
116
+ else
117
+ tag.div(class: 'govuk-radios__item') do
118
+ yield(radio_item)
119
+ end
118
120
  end
119
- end
120
- )
121
+ )
122
+ end
121
123
  end
122
124
  end
123
125
  end
124
- end
125
126
 
126
- # Generates the HTML for a radio button for {govuk_radio_fields_form}
127
- #
128
- # @param (see _govuk_radio_item)
129
- #
130
- # @option (see _govuk_radio_item)
131
- #
132
- # @return (see _govuk_radio_item)
127
+ # Generates the HTML for a radio button for {govuk_radio_fields_form}
128
+ #
129
+ # @param (see _govuk_radio_item)
130
+ #
131
+ # @option (see _govuk_radio_item)
132
+ #
133
+ # @return (see _govuk_radio_item)
133
134
 
134
- def govuk_radio_item_tag(attribute, radio_item)
135
- _govuk_radio_item(attribute, radio_item) do
136
- label_attribute = radio_item[:attributes][:id] || "#{attribute}[#{radio_item[:value]}]"
135
+ def govuk_radio_item_tag(attribute, radio_item)
136
+ _govuk_radio_item(attribute, radio_item) do
137
+ label_attribute = radio_item[:attributes][:id] || "#{attribute}[#{radio_item[:value]}]"
137
138
 
138
- concat(radio_button_tag(attribute, radio_item[:value], radio_item[:checked], class: 'govuk-radios__input', **radio_item[:attributes]))
139
- concat(govuk_label(label_attribute, radio_item[:label][:text], **radio_item[:label]))
139
+ concat(radio_button_tag(attribute, radio_item[:value], radio_item[:checked], class: 'govuk-radios__input', **radio_item[:attributes]))
140
+ concat(govuk_label(label_attribute, radio_item[:label][:text], **radio_item[:label]))
141
+ end
140
142
  end
141
- end
142
-
143
- # Generates the HTML for a radio button for {govuk_radio_fields_tag}
144
- #
145
- # @param (see _govuk_radio_item)
146
- # @param form [ActionView::Helpers::FormBuilder] the form builder used to create the radio buttons
147
- #
148
- # @option (see _govuk_radio_item)
149
- #
150
- # @return (see _govuk_radio_item)
151
143
 
152
- def govuk_radio_item_form(form, attribute, radio_item)
153
- _govuk_radio_item(attribute, radio_item) do
154
- (radio_item[:label][:attributes] ||= {})[:value] = radio_item[:value]
155
- radio_item[:label][:attributes][:for] = radio_item[:attributes][:id] if radio_item[:attributes][:id]
156
-
157
- concat(form.radio_button(attribute, radio_item[:value], class: 'govuk-radios__input', **radio_item[:attributes]))
158
- concat(govuk_label(attribute, radio_item[:label][:text], form: form, **radio_item[:label]))
144
+ # Generates the HTML for a radio button for {govuk_radio_fields_tag}
145
+ #
146
+ # @param (see _govuk_radio_item)
147
+ # @param form [ActionView::Helpers::FormBuilder] the form builder used to create the radio buttons
148
+ #
149
+ # @option (see _govuk_radio_item)
150
+ #
151
+ # @return (see _govuk_radio_item)
152
+
153
+ def govuk_radio_item_form(form, attribute, radio_item)
154
+ _govuk_radio_item(attribute, radio_item) do
155
+ (radio_item[:label][:attributes] ||= {})[:value] = radio_item[:value]
156
+ radio_item[:label][:attributes][:for] = radio_item[:attributes][:id] if radio_item[:attributes][:id]
157
+
158
+ concat(form.radio_button(attribute, radio_item[:value], class: 'govuk-radios__input', **radio_item[:attributes]))
159
+ concat(govuk_label(attribute, radio_item[:label][:text], form: form, **radio_item[:label]))
160
+ end
159
161
  end
160
- end
161
-
162
- # rubocop:disable Metrics/AbcSize
163
162
 
164
- # Wrapper method used by {govuk_radio_item_tag} and {govuk_radio_item_form} to generate the radios HTML
165
- # including the label and hint, if there is one.
166
- #
167
- # @param attribute [String, Symbol] the attribute of the raido buttons
168
- # @param radio_item [Hash] the options for the radio item
169
- #
170
- # @option radio_item [String] :classes additional CSS classes for the radio button HTML
171
- # @option radio_item [Hash] :label the parameters that will be used to create the label for the radio button, see {govuk_label}
172
- # @option radio_item [Hash] :hint (nil) the parameters that will be used to create the hint for the radio button, see {govuk_hint}.
173
- # If no hint is given then no hint will be rendered
174
- # @option radio_item [Hash] :conditional (nil) content that will appear when the radio button is checked.
175
- # It can have the following options:
176
- # - +[:content]+ the content that will be shown
177
- # - +[:attributes][:id]+ the id of the conditional section
178
- # If no conditional is given then no conditional content will be rendered
179
- # @option radio_item [Hash] :attributes ({}) any additional attributes that will be added as part of the radio button HTML
180
- #
181
- # @return [ActiveSupport::SafeBuffer] the HTML for the radio buttons, label and hint
182
- # which is used in {govuk_radio_item_tag} and {govuk_radio_item_form}
163
+ # rubocop:disable Metrics/AbcSize
164
+
165
+ # Wrapper method used by {govuk_radio_item_tag} and {govuk_radio_item_form} to generate the radios HTML
166
+ # including the label and hint, if there is one.
167
+ #
168
+ # @param attribute [String, Symbol] the attribute of the raido buttons
169
+ # @param radio_item [Hash] the options for the radio item
170
+ #
171
+ # @option radio_item [String] :classes additional CSS classes for the radio button HTML
172
+ # @option radio_item [Hash] :label the parameters that will be used to create the label for the radio button, see {govuk_label}
173
+ # @option radio_item [Hash] :hint (nil) the parameters that will be used to create the hint for the radio button, see {govuk_hint}.
174
+ # If no hint is given then no hint will be rendered
175
+ # @option radio_item [Hash] :conditional (nil) content that will appear when the radio button is checked.
176
+ # It can have the following options:
177
+ # - +[:content]+ the content that will be shown
178
+ # - +[:attributes][:id]+ the id of the conditional section
179
+ # If no conditional is given then no conditional content will be rendered
180
+ # @option radio_item [Hash] :attributes ({}) any additional attributes that will be added as part of the radio button HTML
181
+ #
182
+ # @return [ActiveSupport::SafeBuffer] the HTML for the radio buttons, label and hint
183
+ # which is used in {govuk_radio_item_tag} and {govuk_radio_item_form}
184
+
185
+ def _govuk_radio_item(attribute, radio_item)
186
+ radio_item[:attributes] ||= {}
187
+ radio_item[:label] ||= {}
188
+ radio_item[:label][:classes] = "govuk-radios__label #{radio_item[:label][:classes]}".rstrip
189
+
190
+ set_item_options_for_hint('radios', attribute, radio_item)
191
+ set_conditional_item_options('radios', attribute, radio_item)
183
192
 
184
- def _govuk_radio_item(attribute, radio_item)
185
- radio_item[:attributes] ||= {}
186
- radio_item[:label] ||= {}
187
- radio_item[:label][:classes] = "govuk-radios__label #{radio_item[:label][:classes]}".rstrip
188
-
189
- set_item_options_for_hint('radios', attribute, radio_item)
190
- set_conditional_item_options('radios', attribute, radio_item)
191
-
192
- capture do
193
- yield
194
- concat(govuk_hint(radio_item[:hint][:text], **radio_item[:hint])) if radio_item[:hint]
195
- concat(tag.div(radio_item[:conditional][:content], class: radio_item[:conditional][:attributes][:class], id: radio_item[:conditional][:attributes][:id])) if radio_item[:conditional]
193
+ capture do
194
+ yield
195
+ concat(govuk_hint(radio_item[:hint][:text], **radio_item[:hint])) if radio_item[:hint]
196
+ concat(tag.div(radio_item[:conditional][:content], class: radio_item[:conditional][:attributes][:class], id: radio_item[:conditional][:attributes][:id])) if radio_item[:conditional]
197
+ end
196
198
  end
197
- end
198
199
 
199
- # rubocop:enable Metrics/AbcSize
200
+ # rubocop:enable Metrics/AbcSize
201
+ end
200
202
  end
201
203
  end
202
204
  end
@@ -3,129 +3,131 @@
3
3
  require 'action_view'
4
4
  require_relative '../field'
5
5
 
6
- module CrownMarketplaceUtils::Helpers
7
- module GovUkHelper
8
- module Field
9
- # = GOV.UK Select
10
- #
11
- # This helper is used for generating the select component from the
12
- # {https://design-system.service.gov.uk/components/select GDS - Components - Select}
13
- #
14
- # This is considered a Field module and so makes use of the methods in {CrownMarketplaceUtils::GovUkHelper::Field}
15
-
16
- module Select
17
- include ActionView::Helpers::FormOptionsHelper
18
- include Field
19
-
20
- # Generates the HTML for the GOV.UK Select component
6
+ module CrownMarketplaceUtils
7
+ module Helpers
8
+ module GovUkHelper
9
+ module Field
10
+ # = GOV.UK Select
21
11
  #
22
- # @param attribute [String, Symbol] the attribute of the select field
23
- # @param items [Array] array of option items for the select, see {govuk_map_select_items}
24
- # @param govuk_select_options [Hash] options that will be used for the parts of the form group, label, hint and select
12
+ # This helper is used for generating the select component from the
13
+ # {https://design-system.service.gov.uk/components/select GDS - Components - Select}
25
14
  #
26
- # @option govuk_select_options [String] :error_message (nil) the error message to be displayed
27
- # @option govuk_select_options [ActiveModel] :model (nil) optional model that can be used to find an error message
28
- # @option govuk_select_options [ActionView::Helpers::FormBuilder] :form (nil) optional form builder used to create
29
- # the select tag and find the error message
30
- # @option govuk_select_options [Hash] :form_group see {govuk_field}
31
- # @option govuk_select_options [Hash] :label see {govuk_field}
32
- # @option govuk_select_options [Hash] :hint see {govuk_field}
33
- # @option govuk_select_options [Hash] :select ({}) the options that will be used when rendering the select field.
34
- # See {set_govuk_select_field_options} for more details.
35
- #
36
- # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Select
37
- # which can then be rendered on the page
15
+ # This is considered a Field module and so makes use of the methods in {CrownMarketplaceUtils::GovUkHelper::Field}
38
16
 
39
- def govuk_select(attribute, items, **govuk_select_options)
40
- govuk_field(:select, attribute, **govuk_select_options) do |govuk_field_options, error_message|
41
- set_govuk_select_field_options(error_message, govuk_field_options)
42
- (govuk_select_options[:select] ||= {})[:selected] = govuk_select_options[:model].send(attribute) if govuk_select_options[:model]
17
+ module Select
18
+ include ActionView::Helpers::FormOptionsHelper
19
+ include Field
43
20
 
44
- concat(
45
- if govuk_select_options[:form]
46
- govuk_select_form(govuk_select_options[:form], attribute, items, **govuk_field_options)
47
- else
48
- govuk_select_tag(attribute, items, **govuk_field_options)
49
- end
50
- )
21
+ # Generates the HTML for the GOV.UK Select component
22
+ #
23
+ # @param attribute [String, Symbol] the attribute of the select field
24
+ # @param items [Array] array of option items for the select, see {govuk_map_select_items}
25
+ # @param govuk_select_options [Hash] options that will be used for the parts of the form group, label, hint and select
26
+ #
27
+ # @option govuk_select_options [String] :error_message (nil) the error message to be displayed
28
+ # @option govuk_select_options [ActiveModel] :model (nil) optional model that can be used to find an error message
29
+ # @option govuk_select_options [ActionView::Helpers::FormBuilder] :form (nil) optional form builder used to create
30
+ # the select tag and find the error message
31
+ # @option govuk_select_options [Hash] :form_group see {govuk_field}
32
+ # @option govuk_select_options [Hash] :label see {govuk_field}
33
+ # @option govuk_select_options [Hash] :hint see {govuk_field}
34
+ # @option govuk_select_options [Hash] :select ({}) the options that will be used when rendering the select field.
35
+ # See {set_govuk_select_field_options} for more details.
36
+ #
37
+ # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Select
38
+ # which can then be rendered on the page
39
+
40
+ def govuk_select(attribute, items, **govuk_select_options)
41
+ govuk_field(:select, attribute, **govuk_select_options) do |govuk_field_options, error_message|
42
+ set_govuk_select_field_options(error_message, govuk_field_options)
43
+ (govuk_select_options[:select] ||= {})[:selected] = govuk_select_options[:model].send(attribute) if govuk_select_options[:model]
44
+
45
+ concat(
46
+ if govuk_select_options[:form]
47
+ govuk_select_form(govuk_select_options[:form], attribute, items, **govuk_field_options)
48
+ else
49
+ govuk_select_tag(attribute, items, **govuk_field_options)
50
+ end
51
+ )
52
+ end
51
53
  end
52
- end
53
54
 
54
- private
55
+ private
55
56
 
56
- # Generates the select HTML for {govuk_select}
57
- #
58
- # @param attribute [String, Symbol] the attribute of the select field
59
- # @param items [Array] array of option items for the select, see {govuk_map_select_items}
60
- # @param govuk_select_options [Hash] options that will be used in customising the HTML
61
- #
62
- # @option (see set_govuk_select_field_options)
63
- #
64
- # @return [ActiveSupport::SafeBuffer] the HTML for the select field which is used in {govuk_select}
57
+ # Generates the select HTML for {govuk_select}
58
+ #
59
+ # @param attribute [String, Symbol] the attribute of the select field
60
+ # @param items [Array] array of option items for the select, see {govuk_map_select_items}
61
+ # @param govuk_select_options [Hash] options that will be used in customising the HTML
62
+ #
63
+ # @option (see set_govuk_select_field_options)
64
+ #
65
+ # @return [ActiveSupport::SafeBuffer] the HTML for the select field which is used in {govuk_select}
65
66
 
66
- def govuk_select_tag(attribute, items, **govuk_select_options)
67
- select_tag(
68
- attribute,
69
- options_for_select(
70
- govuk_map_select_items(items),
71
- govuk_select_options[:selected]
72
- ),
73
- **govuk_select_options[:attributes]
74
- )
75
- end
67
+ def govuk_select_tag(attribute, items, **govuk_select_options)
68
+ select_tag(
69
+ attribute,
70
+ options_for_select(
71
+ govuk_map_select_items(items),
72
+ govuk_select_options[:selected]
73
+ ),
74
+ **govuk_select_options[:attributes]
75
+ )
76
+ end
76
77
 
77
- # Generates the select HTML for {govuk_select} when there is a ActionView::Helpers::FormBuilder
78
- #
79
- # @param form [ActionView::Helpers::FormBuilder] the form builder used to create the select field
80
- # @param (see govuk_select_tag)
81
- #
82
- # @option (see set_govuk_select_field_options)
83
- #
84
- # @return (see govuk_select_tag)
78
+ # Generates the select HTML for {govuk_select} when there is a ActionView::Helpers::FormBuilder
79
+ #
80
+ # @param form [ActionView::Helpers::FormBuilder] the form builder used to create the select field
81
+ # @param (see govuk_select_tag)
82
+ #
83
+ # @option (see set_govuk_select_field_options)
84
+ #
85
+ # @return (see govuk_select_tag)
85
86
 
86
- def govuk_select_form(form, attribute, items, **govuk_select_options)
87
- form.select(
88
- attribute,
89
- govuk_map_select_items(items),
90
- govuk_select_options[:select_options] || {},
91
- **govuk_select_options[:attributes]
92
- )
93
- end
87
+ def govuk_select_form(form, attribute, items, **govuk_select_options)
88
+ form.select(
89
+ attribute,
90
+ govuk_map_select_items(items),
91
+ govuk_select_options[:select_options] || {},
92
+ **govuk_select_options[:attributes]
93
+ )
94
+ end
94
95
 
95
- # Initialises the attributes for the select input
96
- #
97
- # @param error_message [String] used to indicate if there is an error which will add extra classes
98
- # @param govuk_select_options [Hash] options that will be used in customising the HTML
99
- #
100
- # @option govuk_select_options [String] :classes additional CSS classes for the select HTML
101
- # @option govuk_select_options [String] :selected (nil) the selected option
102
- # @option govuk_select_options [Hash] :attributes ({}) any additional attributes that will added as part of the HTML
96
+ # Initialises the attributes for the select input
97
+ #
98
+ # @param error_message [String] used to indicate if there is an error which will add extra classes
99
+ # @param govuk_select_options [Hash] options that will be used in customising the HTML
100
+ #
101
+ # @option govuk_select_options [String] :classes additional CSS classes for the select HTML
102
+ # @option govuk_select_options [String] :selected (nil) the selected option
103
+ # @option govuk_select_options [Hash] :attributes ({}) any additional attributes that will added as part of the HTML
103
104
 
104
- def set_govuk_select_field_options(error_message, govuk_select_options)
105
- govuk_select_options[:attributes] ||= {}
106
- govuk_select_options[:attributes][:class] = "govuk-select #{govuk_select_options[:classes]}".rstrip
107
- govuk_select_options[:attributes][:class] << ' govuk-select--error' if error_message
108
- end
105
+ def set_govuk_select_field_options(error_message, govuk_select_options)
106
+ govuk_select_options[:attributes] ||= {}
107
+ govuk_select_options[:attributes][:class] = "govuk-select #{govuk_select_options[:classes]}".rstrip
108
+ govuk_select_options[:attributes][:class] << ' govuk-select--error' if error_message
109
+ end
109
110
 
110
- # Maps the items into an array that can be used to generate the options for
111
- # {govuk_select_tag} and {govuk_select_form}
112
- #
113
- # @param items [Array] array of option items for the select
114
- #
115
- # @option items [String] :text the text of the option item.
116
- # If this is blank the value is used
117
- # @option items [String] :value the value of the option item
118
- # @option items [Hash] :attributes ({}) any additional attributes that will added as part of the option HTML
119
- #
120
- # @return [Array] array of option params that are used in {govuk_select_tag} and {govuk_select_form}
111
+ # Maps the items into an array that can be used to generate the options for
112
+ # {govuk_select_tag} and {govuk_select_form}
113
+ #
114
+ # @param items [Array] array of option items for the select
115
+ #
116
+ # @option items [String] :text the text of the option item.
117
+ # If this is blank the value is used
118
+ # @option items [String] :value the value of the option item
119
+ # @option items [Hash] :attributes ({}) any additional attributes that will added as part of the option HTML
120
+ #
121
+ # @return [Array] array of option params that are used in {govuk_select_tag} and {govuk_select_form}
121
122
 
122
- def govuk_map_select_items(items)
123
- items.map do |item|
124
- [
125
- item[:text] || item[:value],
126
- item[:value],
127
- (item[:attributes] || {})
128
- ]
123
+ def govuk_map_select_items(items)
124
+ items.map do |item|
125
+ [
126
+ item[:text] || item[:value],
127
+ item[:value],
128
+ (item[:attributes] || {})
129
+ ]
130
+ end
129
131
  end
130
132
  end
131
133
  end