crown_marketplace_utils 0.1.0.beta.3 → 0.1.0.beta.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9bded690841ee7e1cbd50580542cbebe6ec6f1522dfa1ee4ef3da56028ada558
4
- data.tar.gz: 6cddd1647d22f33db11fc6120bd2f789b908d686b0c16e5d344a7ccbf188b3c7
3
+ metadata.gz: 9379a4394bcdd0b8a3f1721e917bbcfc3a30ac2848bd1d49e71b06724eebe753
4
+ data.tar.gz: 846cf0d310cd55fba57a1de24d91d10eef2743adc9c3c7869e466a31f647694b
5
5
  SHA512:
6
- metadata.gz: 051dee5a663d6355aba6d171307b7ea9965413e15c7926efa164c4ff651b9f25fa8946c66cbaa2f0be4491facfbb8dbbf366363d33d348deb8eaa5ae431aa8db
7
- data.tar.gz: 7d201bfd9d0a383bf3e5bf71402f428f6eda75bd88d17c669a1f1d0649ad5029d2400ed5626b26b9bf3ffffdadbf3ddeba9b6a65fdb8c09edc26cc0683952b5e
6
+ metadata.gz: 5056a707b0f224e7d8891b95f295cbe44c42d1141779f131f9c8228f5ff297c276b46e52a87cc7d61b8e973819b00cd78da0d0f5e24a16fb4819459399777513
7
+ data.tar.gz: ef97d1119c64536641a9bbd78f3cdb486d29f0db49c175c417aaf80311417cebe29f504701996ed2766f00d3c322f6a4ffc20b49f70d12180309eac8b5d9ef94
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- crown_marketplace_utils (0.1.0.beta.3)
4
+ crown_marketplace_utils (0.1.0.beta.4)
5
5
  actionview (>= 6.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -52,10 +52,11 @@ At the moment this Gem only contains view helpers. It would be good in the futur
52
52
  - ~~Button~~
53
53
  - ~~Checkboxes~~
54
54
  - ~~CharacterCount~~
55
- - DateInput
55
+ - ~~DateInput~~
56
56
  - ~~Details~~
57
57
  - ~~ErrorMessage~~
58
58
  - ~~Fieldset~~
59
+ - ~~File Upload~~
59
60
  - ~~FormGroup~~
60
61
  - ~~Header~~
61
62
  - ~~Hint~~
@@ -39,6 +39,8 @@ module CrownMarketplaceUtils
39
39
  govuk_button_classes << govuk_button_options[:classes]
40
40
  govuk_button_options[:attributes] ||= {}
41
41
  govuk_button_classes << 'govuk-button--disabled' if govuk_button_options[:attributes][:disabled]
42
+ govuk_button_classes << 'govuk-button--start' if govuk_button_options[:is_start_button]
43
+
42
44
  (govuk_button_options[:attributes][:data] ||= {}).merge!({ module: 'govuk-button' })
43
45
 
44
46
  button_method = case type
@@ -22,12 +22,12 @@ module CrownMarketplaceUtils
22
22
  # @param error_message [String] the error message to be displayed
23
23
  # @param govuk_character_count_options [Hash] options that will be used for the parts of the form group, label, hint, textarea and the character count
24
24
  #
25
- # @option govuk_character_count_options [Hash] :form_group_options see {govuk_field}
25
+ # @option govuk_character_count_options [Hash] :form_group see {govuk_field}
26
26
  # @option govuk_character_count_options [Hash] :label see {govuk_field}
27
27
  # @option govuk_character_count_options [Hash] :hint see {govuk_field}
28
- # @option govuk_character_count_options [Hash] :textarea_options see {govuk_textarea}
29
- # @option govuk_character_count_options [Hash] :character_count_options ({}) the options that will be used when rendering the textarea.
30
- # See {_govuk_character_count} for more details.
28
+ # @option govuk_character_count_options [Hash] :textarea see {govuk_textarea}
29
+ # @option govuk_character_count_options [Hash] :character_count ({}) the options that will be used when rendering the textarea.
30
+ # See {_govuk_character_count} for more details.
31
31
  #
32
32
  # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK chracter count
33
33
  # which can then be rendered on the page
@@ -38,36 +38,33 @@ module CrownMarketplaceUtils
38
38
  end
39
39
  end
40
40
 
41
- # Generates the HTML for the GOV.UK character count component.
41
+ # Generates the HTML for the GOV.UK character count component using anActiveModel.
42
+ # Unlike {#govuk_character_count}, the method will be able to automatically determine if the error message needs to be shown.
42
43
  # It works by warpping the govuk_textarea in HTML which will trigger the JavaScript to do the character count.
43
44
  #
44
- # @param attribute [String, Symbol] the attribute of the character count
45
- # @param error_message [String] the error message to be displayed
46
- # @param govuk_character_count_options [Hash] options that will be used for the parts of the form group, label, hint and textarea
45
+ # @param model [ActiveModel] model that will be used to find an error message and content of the textarea
46
+ # @param attribute [String, Symbol] the attribute of the character count text area
47
+ # @param govuk_character_count_options [Hash] options that will be used for the parts of the form group, label, hint, textarea and the character count
47
48
  #
48
- # @option govuk_character_count_options [Hash] :form_group_options see {govuk_field}
49
- # @option govuk_character_count_options [Hash] :label see {govuk_field}
50
- # @option govuk_character_count_options [Hash] :hint see {govuk_field}
51
- # @option govuk_character_count_options [Hash] :textarea_options see {govuk_textarea}
52
- # @option govuk_character_count_options [Hash] :character_count_options ({}) the options that will be used when rendering the textarea.
53
- # See {_govuk_character_count} for more details.
49
+ # @option (see govuk_character_count)
54
50
  #
55
- # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK chracter count
56
- # which can then be rendered on the page
51
+ # @return (see govuk_character_count)
57
52
 
58
- # Generates the HTML for the GOV.UK textarea component using an ActionView::Helpers::FormBuilder.
59
- # Unlike {#govuk_textarea}, the method will be able to automatically determine if the error message needs to be shown.
53
+ def govuk_character_count_with_model(model, attribute, **govuk_character_count_options)
54
+ _govuk_character_count(attribute, **govuk_character_count_options) do |govuk_text_textarea_options|
55
+ govuk_textarea_with_model(model, attribute, **govuk_text_textarea_options)
56
+ end
57
+ end
58
+
59
+ # Generates the HTML for the GOV.UK character count component using an ActionView::Helpers::FormBuilder.
60
+ # Unlike {#govuk_character_count}, the method will be able to automatically determine if the error message needs to be shown.
61
+ # It works by warpping the govuk_textarea in HTML which will trigger the JavaScript to do the character count.
60
62
  #
61
- # @param form [ActionView::Helpers::FormBuilder] :form the form builder used to create the character count textarea
63
+ # @param form [ActionView::Helpers::FormBuilder] the form builder used to create the character count textarea
62
64
  # @param attribute [String, Symbol] the attribute of the character count text area
63
65
  # @param govuk_character_count_options [Hash] options that will be used for the parts of the form group, label, hint, textarea and the character count
64
66
  #
65
- # @option govuk_character_count_options [Hash] :form_group_options see {govuk_field_with_form}
66
- # @option govuk_character_count_options [Hash] :label see {govuk_field_with_form}
67
- # @option govuk_character_count_options [Hash] :hint see {govuk_field_with_form}
68
- # @option govuk_character_count_options [Hash] :textarea_options see {govuk_textarea_with_form}
69
- # @option govuk_character_count_options [Hash] :character_count_options ({}) the options that will be used when rendering the textarea.
70
- # See {_govuk_character_count} for more details.
67
+ # @option (see govuk_character_count)
71
68
  #
72
69
  # @return (see govuk_character_count)
73
70
 
@@ -84,7 +81,7 @@ module CrownMarketplaceUtils
84
81
  # @param attribute [String, Symbol] the attribute of the character count
85
82
  # @param govuk_character_count_options [Hash] options that will be used in customising the HTML.
86
83
  # This includes everything described in {govuk_character_count} and {govuk_character_count_with_form}
87
- # with the addition of the +:character_count_options+ which are described below
84
+ # with the addition of the +:character_count+ which are described below
88
85
  #
89
86
  # @option govuk_character_count_options [String] :maxlength (required) - if +maxwords+ is set, this is not required.
90
87
  # The maximum number of characters.
@@ -106,10 +103,10 @@ module CrownMarketplaceUtils
106
103
  # @return [ActiveSupport::SafeBuffer] the HTML for the chracter count which wrpas arround {govuk_character_count} or {govuk_character_count_with_form}
107
104
 
108
105
  def _govuk_character_count(attribute, **govuk_character_count_options)
109
- deep_init_hash(govuk_character_count_options, :textarea_options, :attributes, :aria)
110
- govuk_character_count_options[:textarea_options][:attributes][:aria][:describedby] = [govuk_character_count_options.dig(:textarea_options, :attributes, :aria, :describedby), "#{attribute}-hint-info"].compact.join(' ')
106
+ deep_init_hash(govuk_character_count_options, :textarea, :attributes, :aria)
107
+ govuk_character_count_options[:textarea][:attributes][:aria][:describedby] = [govuk_character_count_options.dig(:textarea, :attributes, :aria, :describedby), "#{attribute}-hint-info"].compact.join(' ')
111
108
 
112
- govuk_character_count_options[:textarea_options][:classes] = [govuk_character_count_options[:textarea_options][:classes], 'govuk-js-character-count'].compact
109
+ govuk_character_count_options[:textarea][:classes] = "#{govuk_character_count_options[:textarea][:classes]} govuk-js-character-count".lstrip
113
110
 
114
111
  tag.div(class: 'govuk-character-count', **get_character_count_attributes(**govuk_character_count_options)) do
115
112
  capture do
@@ -128,24 +125,24 @@ module CrownMarketplaceUtils
128
125
  # @return [ActiveSupport::SafeBuffer] the HTML for the fullback hint used in {_govuk_character_count}
129
126
 
130
127
  def character_count_hint(attribute, **govuk_character_count_options)
131
- fallback_hint_length = govuk_character_count_options[:character_count_options][:maxwords] || govuk_character_count_options[:character_count_options][:maxlength]
132
- fallback_hint_default = "You can enter up to %<count>s #{govuk_character_count_options[:character_count_options][:maxwords] ? 'words' : 'characters'}"
128
+ fallback_hint_length = govuk_character_count_options[:character_count][:maxwords] || govuk_character_count_options[:character_count][:maxlength]
129
+ fallback_hint_default = "You can enter up to %<count>s #{govuk_character_count_options[:character_count][:maxwords] ? 'words' : 'characters'}"
133
130
 
134
- deep_init_hash(govuk_character_count_options, :character_count_options, :fallback_hint, :attributes)
131
+ deep_init_hash(govuk_character_count_options, :character_count, :fallback_hint, :attributes)
135
132
 
136
- fallback_hint_text = format(govuk_character_count_options[:character_count_options][:fallback_hint][:count_message] || fallback_hint_default, count: fallback_hint_length)
133
+ fallback_hint_text = format(govuk_character_count_options[:character_count][:fallback_hint][:count_message] || fallback_hint_default, count: fallback_hint_length)
137
134
 
138
- govuk_character_count_options[:character_count_options][:fallback_hint][:classes] = [govuk_character_count_options.dig(:character_count_options, :fallback_hint, :classes), 'govuk-character-count__message']
139
- govuk_character_count_options[:character_count_options][:fallback_hint][:attributes].merge!(id: "#{attribute}-hint-info")
135
+ govuk_character_count_options[:character_count][:fallback_hint][:classes] = "#{govuk_character_count_options.dig(:character_count, :fallback_hint, :classes)} govuk-character-count__message".lstrip
136
+ govuk_character_count_options[:character_count][:fallback_hint][:attributes].merge!(id: "#{attribute}-hint-info")
140
137
 
141
- govuk_hint(fallback_hint_text, **govuk_character_count_options[:character_count_options][:fallback_hint])
138
+ govuk_hint(fallback_hint_text, **govuk_character_count_options[:character_count][:fallback_hint])
142
139
  end
143
140
 
144
141
  # Generates a hash with the character count attributes used in {_govuk_character_count}
145
142
  #
146
143
  # @param govuk_character_count_options [Hash] options that will be used in customising the HTML.
147
144
  # This includes everything described in {govuk_character_count} and {govuk_character_count_with_form}
148
- # with the addition of the +:character_count_options+ which are described below
145
+ # with the addition of the +:character_count+ which are described below
149
146
  #
150
147
  # @option (see _govuk_character_count)
151
148
  #
@@ -155,7 +152,7 @@ module CrownMarketplaceUtils
155
152
  govuk_character_count_attributes = { data: { module: 'govuk-character-count' } }
156
153
 
157
154
  %i[maxlength threshold maxwords].each do |data_attribute|
158
- govuk_character_count_attributes[:data][data_attribute] = govuk_character_count_options[:character_count_options][data_attribute].to_s if govuk_character_count_options[:character_count_options][data_attribute]
155
+ govuk_character_count_attributes[:data][data_attribute] = govuk_character_count_options[:character_count][data_attribute].to_s if govuk_character_count_options[:character_count][data_attribute]
159
156
  end
160
157
 
161
158
  govuk_character_count_attributes
@@ -22,10 +22,10 @@ module CrownMarketplaceUtils
22
22
  # @param error_message [String] the error message to be displayed
23
23
  # @param govuk_checkboxes_options [Hash] options that will be used for the parts of the fieldset, form group, hint and checkbox buttons
24
24
  #
25
- # @option govuk_checkboxes_options [Hash] :form_group_options see {govuk_fields}
26
- # @option govuk_checkboxes_options [Hash] :fieldset_options see {govuk_fields}
25
+ # @option govuk_checkboxes_options [Hash] :form_group see {govuk_fields}
26
+ # @option govuk_checkboxes_options [Hash] :fieldset see {govuk_fields}
27
27
  # @option govuk_checkboxes_options [Hash] :hint see {govuk_field}
28
- # @option govuk_checkboxes_options [Hash] :checkboxes_options ({}) the options that will be used when rendering the checkbox buttons.
28
+ # @option govuk_checkboxes_options [Hash] :checkboxes ({}) the options that will be used when rendering the checkbox buttons.
29
29
  # See {govuk_checkboxes_fields} for more details.
30
30
  #
31
31
  # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Checkboxes
@@ -37,6 +37,27 @@ module CrownMarketplaceUtils
37
37
  end
38
38
  end
39
39
 
40
+ # Generates the HTML for the GOV.UK Checkboxes component using an ActiveModel.
41
+ # Unlike {govuk_checkboxes}, the method will be able to automatically determine if the error message needs to be shown.
42
+ #
43
+ # @param model [ActiveModel] model that will be used to find an error message and the checked checkboxes
44
+ # @param attribute [String, Symbol] the attribute of the raido buttons
45
+ # @param items [Array] array of checkbox items, see {_govuk_checkboxes_fields}
46
+ # @param govuk_checkboxes_options [Hash] options that will be used for the parts of the fieldset, form group, hint and checkbox buttons
47
+ #
48
+ # @option (see govuk_checkboxes)
49
+ #
50
+ # @return (see govuk_checkboxes)
51
+
52
+ def govuk_checkboxes_with_model(model, attribute, items, **govuk_checkboxes_options)
53
+ values = model.send(attribute) || []
54
+ items.each { |item| item[:checked] = values.include?(item[:value]) }
55
+
56
+ govuk_fields_with_model(:checkboxes, model, attribute, **govuk_checkboxes_options) do |govuk_field_options|
57
+ concat(govuk_checkboxes_fields(attribute, items, **govuk_field_options))
58
+ end
59
+ end
60
+
40
61
  # Generates the HTML for the GOV.UK Checkboxes component using an ActionView::Helpers::FormBuilder.
41
62
  # Unlike {govuk_checkboxes}, the method will be able to automatically determine if the error message needs to be shown.
42
63
  #
@@ -45,11 +66,7 @@ module CrownMarketplaceUtils
45
66
  # @param items [Array] array of checkbox items, see {_govuk_checkboxes_fields}
46
67
  # @param govuk_checkboxes_options [Hash] options that will be used for the parts of the fieldset, form group, hint and checkbox buttons
47
68
  #
48
- # @option govuk_checkboxes_options [Hash] :form_group_options see {govuk_fields_with_form}
49
- # @option govuk_checkboxes_options [Hash] :fieldset_options see {govuk_fields_with_form}
50
- # @option govuk_checkboxes_options [Hash] :hint see {govuk_fields_with_form}
51
- # @option govuk_checkboxes_options [Hash] :checkboxes_options ({}) the options that will be used when rendering the checkbox buttons.
52
- # See {govuk_checkboxes_fields_with_form} for more details.
69
+ # @option (see govuk_checkboxes)
53
70
  #
54
71
  # @return (see govuk_checkboxes)
55
72
 
@@ -79,7 +96,7 @@ module CrownMarketplaceUtils
79
96
 
80
97
  # Generates the checkboxes HTML for {govuk_checkboxes_with_form}
81
98
  #
82
- # @param form [ActionView::Helpers::FormBuilder] :form the form builder used to create the checkbox buttons
99
+ # @param form [ActionView::Helpers::FormBuilder] the form builder used to create the checkbox buttons
83
100
  # @param attribute [String, Symbol] the attribute of the raido buttons
84
101
  # @param items [Array] array of checkbox items, see {_govuk_checkboxes_fields}
85
102
  # @param govuk_checkboxes_options [Hash] options that will be used in customising the HTML
@@ -146,12 +163,10 @@ module CrownMarketplaceUtils
146
163
  checkbox_item[:attributes][:id] ||= "#{sanitize_to_id(attribute)}_#{sanitize_to_id(checkbox_item[:value])}"
147
164
 
148
165
  concat(check_box_tag("#{attribute}[]", checkbox_item[:value], checkbox_item[:checked], class: 'govuk-checkboxes__input', **checkbox_item[:attributes]))
149
- concat(govuk_label(checkbox_item[:attributes][:id], checkbox_item[:label][:text], **checkbox_item[:label][:options]))
166
+ concat(govuk_label(checkbox_item[:attributes][:id], checkbox_item[:label][:text], **checkbox_item[:label]))
150
167
  end
151
168
  end
152
169
 
153
- # rubocop:disable Metrics/AbcSize
154
-
155
170
  # Generates the HTML for a checkbox button for {govuk_checkboxes_fields}
156
171
  #
157
172
  # @param (see _govuk_checkbox_item)
@@ -163,11 +178,11 @@ module CrownMarketplaceUtils
163
178
 
164
179
  def govuk_checkbox_item_with_form(form, attribute, checkbox_item)
165
180
  _govuk_checkbox_item(attribute, checkbox_item) do
166
- (checkbox_item[:label][:options][:attributes] ||= {})[:value] = checkbox_item[:value]
167
- checkbox_item[:label][:options][:attributes][:for] = checkbox_item[:attributes][:id] if checkbox_item[:attributes][:id]
181
+ (checkbox_item[:label][:attributes] ||= {})[:value] = checkbox_item[:value]
182
+ checkbox_item[:label][:attributes][:for] = checkbox_item[:attributes][:id] if checkbox_item[:attributes][:id]
168
183
 
169
184
  concat(form.check_box(attribute, checkbox_item[:attributes].merge({ class: 'govuk-checkboxes__input', multiple: true, include_hidden: false }), checkbox_item[:value]))
170
- concat(govuk_label_with_form(form, attribute, checkbox_item[:label][:text], **checkbox_item[:label][:options]))
185
+ concat(govuk_label_with_form(form, attribute, checkbox_item[:label][:text], **checkbox_item[:label]))
171
186
  end
172
187
  end
173
188
 
@@ -178,29 +193,24 @@ module CrownMarketplaceUtils
178
193
  # @param checkbox_item [Hash] the options for the checkbox item
179
194
  #
180
195
  # @option checkbox_item [String] :classes additional CSS classes for the checkbox button HTML
181
- # @option checkbox_item [Hash] :label the parameters that will be used to create the label for the checkbox button:
182
- # - +:text+ (required) - the label text
183
- # - +:options+ - default: { } - the options for govuk_label {govuk_label}
184
- # @option checkbox_item [Hash] :hint (nil) the parameters that will be used to create the hint for the field
185
- # - +:text+ (required) - the hint text
186
- # - +:options+ - default: { } - the options for govuk_hint {govuk_hint}
196
+ # @option checkbox_item [Hash] :label the parameters that will be used to create the label for the checkbox button, see {govuk_label}
197
+ # @option checkbox_item [Hash] :hint (nil) the parameters that will be used to create the hint for the checkbox button, see {govuk_hint}.
198
+ # If no hint is given then no hint will be rendered
187
199
  # @option checkbox_item [Hash] :attributes ({}) any additional attributes that will be added as part of the checkbox button HTML
188
200
  #
189
201
  # @return [ActiveSupport::SafeBuffer] the HTML for the checkbox buttons, label and hint
190
202
  # which is used in {govuk_checkbox_item} and {govuk_checkbox_item_with_form}
191
203
 
192
- # rubocop:enable Metrics/AbcSize
193
-
194
204
  def _govuk_checkbox_item(attribute, checkbox_item)
195
205
  checkbox_item[:attributes] ||= {}
196
- checkbox_item[:label][:options] ||= {}
197
- checkbox_item[:label][:options][:classes] = "govuk-checkboxes__label #{checkbox_item[:label][:options][:classes]}".rstrip
206
+ checkbox_item[:label] ||= {}
207
+ checkbox_item[:label][:classes] = "govuk-checkboxes__label #{checkbox_item[:label][:classes]}".rstrip
198
208
 
199
209
  set_item_options_for_hint('checkboxes', attribute, checkbox_item) if checkbox_item[:hint]
200
210
 
201
211
  capture do
202
212
  yield
203
- concat(govuk_hint(checkbox_item[:hint][:text], **checkbox_item[:hint][:options])) if checkbox_item[:hint]
213
+ concat(govuk_hint(checkbox_item[:hint][:text], **checkbox_item[:hint])) if checkbox_item[:hint]
204
214
  end
205
215
  end
206
216
  end
@@ -0,0 +1,234 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../field'
4
+ require_relative 'input'
5
+
6
+ module CrownMarketplaceUtils
7
+ module GovUkHelper
8
+ module Field
9
+ # = GOV.UK Date Input
10
+ #
11
+ # This helper is used for generating the date input component from the
12
+ # {https://design-system.service.gov.uk/components/date-input GDS - Components - Date Input}
13
+ #
14
+ # This is considered a Field module and so makes use of the methods in {CrownMarketplaceUtils::GovUkHelper::Field}
15
+
16
+ module DateInput
17
+ include Field
18
+ include Input
19
+
20
+ # Generates the HTML for the GOV.UK date input component
21
+ #
22
+ # @param attribute [String, Symbol] the attribute of the date input
23
+ # @param error_message [String] the error message to be displayed
24
+ # @param govuk_date_input_options [Hash] options that will be used for the parts of the fieldset, form group, hint and date input
25
+ #
26
+ # @option govuk_date_input_options [Hash] :form_group see {govuk_fields}
27
+ # @option govuk_date_input_options [Hash] :fieldset see {govuk_fields}
28
+ # @option govuk_date_input_options [Hash] :hint see {govuk_field}
29
+ # @option govuk_date_input_options [Hash] :date_input_options ({}) the options that will be used when rendering the date input.
30
+ # See {govuk_date_input_fields} for more details.
31
+ #
32
+ # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Date Input
33
+ # which can then be rendered on the page
34
+
35
+ def govuk_date_input(attribute, error_message = nil, **govuk_date_input_options)
36
+ set_govuk_date_input_fieldset_options(**govuk_date_input_options)
37
+
38
+ govuk_fields(:date_input, attribute, error_message, **govuk_date_input_options) do |govuk_field_options|
39
+ concat(govuk_date_input_fields(attribute, error_message, **govuk_field_options))
40
+ end
41
+ end
42
+
43
+ # Generates the HTML for the GOV.UK date input component using an ActiveModel.
44
+ # Unlike {govuk_date_input_with_model}, the method will be able to automatically determine if the error message needs to be shown.
45
+ #
46
+ # @param model [ActiveModel] model that will be used to find an error message and the value of the date inputs
47
+ # @param attribute [String, Symbol] the attribute of the date input
48
+ # @param govuk_date_input_options [Hash] options that will be used for the parts of the fieldset, form group, hint and date input
49
+ #
50
+ # @option (see govuk_date_input)
51
+ #
52
+ # @return (see govuk_date_input)
53
+
54
+ def govuk_date_input_with_model(model, attribute, **govuk_date_input_options)
55
+ set_govuk_date_input_fieldset_options(**govuk_date_input_options)
56
+
57
+ govuk_fields_with_model(:date_input, model, attribute, **govuk_date_input_options) do |govuk_field_options, any_errors|
58
+ concat(govuk_date_input_fields_with_model(model, attribute, any_errors, **govuk_field_options))
59
+ end
60
+ end
61
+
62
+ # Generates the HTML for the GOV.UK date input component using an ActionView::Helpers::FormBuilder.
63
+ # Unlike {govuk_date_input_with_model}, the method will be able to automatically determine if the error message needs to be shown.
64
+ #
65
+ # @param form [ActionView::Helpers::FormBuilder] the form builder used to create the date inputs
66
+ # @param attribute [String, Symbol] the attribute of the date input
67
+ # @param govuk_date_input_options [Hash] options that will be used for the parts of the fieldset, form group, hint and date input
68
+ #
69
+ # @option (see govuk_date_input)
70
+ #
71
+ # @return (see govuk_date_input)
72
+
73
+ def govuk_date_input_with_form(form, attribute, **govuk_date_input_options)
74
+ set_govuk_date_input_fieldset_options(**govuk_date_input_options)
75
+
76
+ govuk_fields_with_form(:date_input, form, attribute, **govuk_date_input_options) do |govuk_field_options, any_errors|
77
+ concat(govuk_date_input_fields_with_form(form, attribute, any_errors, **govuk_field_options))
78
+ end
79
+ end
80
+
81
+ private
82
+
83
+ # Generates the date inputs HTML for {govuk_date_input}
84
+ #
85
+ # @param (see _govuk_date_input_fields)
86
+ #
87
+ # @option (see _govuk_date_input_fields)
88
+ #
89
+ # @return [ActiveSupport::SafeBuffer] the HTML for the date inputs which is used in {govuk_date_input}
90
+
91
+ def govuk_date_input_fields(attribute, any_errors, **govuk_date_input_options)
92
+ _govuk_date_input_fields(attribute, any_errors, **govuk_date_input_options) do |date_item_attribute, date_item_input_options|
93
+ govuk_input(
94
+ date_item_attribute,
95
+ **date_item_input_options
96
+ )
97
+ end
98
+ end
99
+
100
+ # Generates the date inputs HTML for {govuk_date_input_with_model}
101
+ #
102
+ # @param model [ActiveModel] model that will be used to find an error message and the value of the date inputs
103
+ # @param (see _govuk_date_input_fields)
104
+ #
105
+ # @option (see _govuk_date_input_fields)
106
+ #
107
+ # @return [ActiveSupport::SafeBuffer] the HTML for the date inputs which is used in {govuk_date_input_with_model}
108
+
109
+ def govuk_date_input_fields_with_model(model, attribute, any_errors, **govuk_date_input_options)
110
+ _govuk_date_input_fields(attribute, any_errors, **govuk_date_input_options) do |date_item_attribute, date_item_input_options|
111
+ govuk_input_with_model(
112
+ model,
113
+ date_item_attribute,
114
+ **date_item_input_options
115
+ )
116
+ end
117
+ end
118
+
119
+ # Generates the date inputs HTML for {govuk_date_input_with_form}
120
+ #
121
+ # @param form [ActionView::Helpers::FormBuilder] the form builder used to create the date inputs
122
+ # @param (see _govuk_date_input_fields)
123
+ #
124
+ # @option (see _govuk_date_input_fields)
125
+ #
126
+ # @return [ActiveSupport::SafeBuffer] the HTML for the date inputs which is used in {govuk_date_input_with_form}
127
+
128
+ def govuk_date_input_fields_with_form(form, attribute, any_errors, **govuk_date_input_options)
129
+ _govuk_date_input_fields(attribute, any_errors, **govuk_date_input_options) do |date_item_attribute, date_item_input_options|
130
+ govuk_input_with_form(
131
+ form,
132
+ date_item_attribute,
133
+ **date_item_input_options
134
+ )
135
+ end
136
+ end
137
+
138
+ # Sets the role for the fieldset to group
139
+ #
140
+ # @param govuk_date_input_options [Hash] options that will be used for the parts of the fieldset, form group, hint and date input
141
+
142
+ def set_govuk_date_input_fieldset_options(**govuk_date_input_options)
143
+ (govuk_date_input_options[:fieldset][:attributes] ||= {})[:role] = 'group'
144
+ end
145
+
146
+ # Wrapper method used by {govuk_date_input_fields}, {govuk_date_input_fields_with_model} and {govuk_date_input_fields_with_form}
147
+ # to generate the date inputs HTML
148
+ #
149
+ # @param attribute [String, Symbol] the attribute of the date input
150
+ # @param any_errors [Boolean] flag to indicate if the inputs need the error class
151
+ # @param govuk_date_input_options [Hash] options that will be used in customising the HTML
152
+ #
153
+ # @option govuk_date_input_options [String] :classes additional CSS classes for the date inputs HTML
154
+ # @option govuk_date_input_options[Array] :date_items an array of the date items that will be rendered.
155
+ # Each date item hash must contain the +:name+ to add as suffix to attribute.
156
+ # Anything else in the hash will be used as options in a {govuk_input}.
157
+ # @option govuk_date_input_options [Hash] :attributes ({}) any additional attributes that will added as part of the HTML
158
+ #
159
+ # @yield the date input item HTML generated by the parent method
160
+ #
161
+ # @yieldparam date_item_attribute [String] the attribute for individual date input
162
+ # @yieldparam date_item_input_options [Hash] the HTML options used for the date input item
163
+ #
164
+ # @return [ActiveSupport::SafeBuffer] the HTML for the date inputs which is used in
165
+ # {govuk_date_input_fields}, {govuk_date_input_fields_with_model} and {govuk_date_input_fields_with_form}
166
+
167
+ def _govuk_date_input_fields(attribute, any_errors, **govuk_date_input_options)
168
+ govuk_date_input_classes = ['govuk-date-input']
169
+ govuk_date_input_classes << govuk_date_input_options[:classes]
170
+ date_items = govuk_date_input_options[:date_items] || [
171
+ {
172
+ name: 'day',
173
+ input: {
174
+ classes: 'govuk-input--width-2'
175
+ }
176
+ },
177
+ {
178
+ name: 'month',
179
+ input: {
180
+ classes: 'govuk-input--width-2'
181
+ }
182
+ },
183
+ {
184
+ name: 'year',
185
+ input: {
186
+ classes: 'govuk-input--width-4'
187
+ }
188
+ }
189
+ ]
190
+
191
+ govuk_date_input_options[:attributes] ||= {}
192
+
193
+ tag.div(class: govuk_date_input_classes, **govuk_date_input_options[:attributes]) do
194
+ capture do
195
+ enumerate_and_set_date_input_options(date_items, any_errors) do |date_item_input_options|
196
+ concat(tag.div(class: 'govuk-date-input__item') do
197
+ concat(yield("#{attribute}_#{date_item_input_options[:name]}", date_item_input_options))
198
+ end)
199
+ end
200
+ end
201
+ end
202
+ end
203
+
204
+ # rubocop:disable Metrics/CyclomaticComplexity
205
+
206
+ # Enumerates and inititalises the attributes of each date input item and is used by {_govuk_date_input_fields}
207
+ #
208
+ # @param date_items [Array] an array of the date item hashes that will be inititalised
209
+ # @param any_errors [Boolean] flag to indicate if the inputs need the error class
210
+ #
211
+ # @yield the date input item HTML generated by teach date input item
212
+ #
213
+ # @yieldparam date_item_input_options [Hash] the HTML options used for the date input item
214
+
215
+ def enumerate_and_set_date_input_options(date_items, any_errors)
216
+ date_items.each do |date_item_input_options|
217
+ (date_item_input_options[:input] ||= {})[:attributes] ||= {}
218
+
219
+ date_item_input_options[:input][:classes] = "govuk-date-input__input #{date_item_input_options[:input][:classes]} #{'govuk-input--error' if any_errors}".rstrip
220
+
221
+ date_item_input_options[:input][:attributes][:inputmode] ||= 'numeric'
222
+
223
+ date_item_input_options[:label] ||= {}
224
+ date_item_input_options[:label][:text] ||= date_item_input_options[:name].capitalize
225
+ date_item_input_options[:label][:classes] = 'govuk-date-input__label'
226
+
227
+ yield(date_item_input_options)
228
+ end
229
+ end
230
+ # rubocop:enable Metrics/CyclomaticComplexity
231
+ end
232
+ end
233
+ end
234
+ end