crown_marketplace_utils 0.1.0.beta.4 → 0.1.0.beta.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +1 -1
  3. data/Gemfile.lock +5 -5
  4. data/README.md +1 -3
  5. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/accordion.rb +116 -0
  6. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/back_link.rb +34 -0
  7. data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/breadcrumbs.rb +2 -2
  8. data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/button.rb +11 -19
  9. data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/details.rb +1 -1
  10. data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/error_message.rb +1 -1
  11. data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/field/character_count.rb +25 -50
  12. data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/field/checkboxes.rb +52 -68
  13. data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/field/date_input.rb +46 -127
  14. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/field/file_upload.rb +94 -0
  15. data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/field/input.rb +34 -72
  16. data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/field/radios.rb +52 -68
  17. data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/field/select.rb +28 -64
  18. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/field/textarea.rb +105 -0
  19. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/field.rb +215 -0
  20. data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/fieldset.rb +1 -1
  21. data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/form_group.rb +10 -21
  22. data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/header.rb +1 -1
  23. data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/hint.rb +1 -1
  24. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/label.rb +88 -0
  25. data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/notification_banner.rb +1 -1
  26. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/pagination.rb +338 -0
  27. data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/step_by_step_navigation.rb +1 -1
  28. data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/tag.rb +1 -1
  29. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/warning_text.rb +52 -0
  30. data/lib/crown_marketplace_utils/{gov_uk_helper.rb → helpers/gov_uk_helper.rb} +7 -1
  31. data/lib/crown_marketplace_utils/version.rb +1 -1
  32. data/lib/crown_marketplace_utils.rb +1 -1
  33. metadata +28 -25
  34. data/lib/crown_marketplace_utils/gov_uk_helper/field/file_upload.rb +0 -125
  35. data/lib/crown_marketplace_utils/gov_uk_helper/field/textarea.rb +0 -140
  36. data/lib/crown_marketplace_utils/gov_uk_helper/field.rb +0 -305
  37. data/lib/crown_marketplace_utils/gov_uk_helper/label.rb +0 -97
  38. data/lib/crown_marketplace_utils/gov_uk_helper/pagination.rb +0 -214
@@ -2,7 +2,7 @@
2
2
 
3
3
  require_relative '../field'
4
4
 
5
- module CrownMarketplaceUtils
5
+ module CrownMarketplaceUtils::Helpers
6
6
  module GovUkHelper
7
7
  module Field
8
8
  # = GOV.UK Checkboxes
@@ -19,60 +19,35 @@ module CrownMarketplaceUtils
19
19
  #
20
20
  # @param attribute [String, Symbol] the attribute of the raido buttons
21
21
  # @param items [Array] array of checkbox items, see {_govuk_checkboxes_fields}
22
- # @param error_message [String] the error message to be displayed
23
22
  # @param govuk_checkboxes_options [Hash] options that will be used for the parts of the fieldset, form group, hint and checkbox buttons
24
23
  #
24
+ # @option govuk_checkboxes_options [String] :error_message (nil) the error message to be displayed
25
+ # @option govuk_checkboxes_options [ActiveModel] :model (nil) optional model that can be used to find an error message
26
+ # @option govuk_checkboxes_options [ActionView::Helpers::FormBuilder] :form (nil) optional form builder used to create
27
+ # the checkbox tags and find the error message
25
28
  # @option govuk_checkboxes_options [Hash] :form_group see {govuk_fields}
26
29
  # @option govuk_checkboxes_options [Hash] :fieldset see {govuk_fields}
27
30
  # @option govuk_checkboxes_options [Hash] :hint see {govuk_field}
28
31
  # @option govuk_checkboxes_options [Hash] :checkboxes ({}) the options that will be used when rendering the checkbox buttons.
29
- # See {govuk_checkboxes_fields} for more details.
32
+ # See {_govuk_checkboxes_fields} for more details.
30
33
  #
31
34
  # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Checkboxes
32
35
  # which can then be rendered on the page
33
36
 
34
- def govuk_checkboxes(attribute, items, error_message = nil, **govuk_checkboxes_options)
35
- govuk_fields(:checkboxes, attribute, error_message, **govuk_checkboxes_options) do |govuk_field_options|
36
- concat(govuk_checkboxes_fields(attribute, items, **govuk_field_options))
37
- end
38
- end
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
-
61
- # Generates the HTML for the GOV.UK Checkboxes component using an ActionView::Helpers::FormBuilder.
62
- # Unlike {govuk_checkboxes}, the method will be able to automatically determine if the error message needs to be shown.
63
- #
64
- # @param form [ActionView::Helpers::FormBuilder] the form builder used to create the checkbox buttons
65
- # @param attribute [String, Symbol] the attribute of the raido buttons
66
- # @param items [Array] array of checkbox items, see {_govuk_checkboxes_fields}
67
- # @param govuk_checkboxes_options [Hash] options that will be used for the parts of the fieldset, form group, hint and checkbox buttons
68
- #
69
- # @option (see govuk_checkboxes)
70
- #
71
- # @return (see govuk_checkboxes)
37
+ def govuk_checkboxes(attribute, items, **govuk_checkboxes_options)
38
+ govuk_fields(:checkboxes, attribute, **govuk_checkboxes_options) do |govuk_field_options|
39
+ if govuk_checkboxes_options[:model] || govuk_checkboxes_options[:form]
40
+ values = (govuk_checkboxes_options[:model] || govuk_checkboxes_options[:form].object).send(attribute) || []
41
+ items.each { |item| item[:checked] = values.include?(item[:value]) }
42
+ end
72
43
 
73
- def govuk_checkboxes_with_form(form, attribute, items, **govuk_checkboxes_options)
74
- govuk_fields_with_form(:checkboxes, form, attribute, **govuk_checkboxes_options) do |govuk_field_options|
75
- concat(govuk_checkboxes_fields_with_form(form, attribute, items, **govuk_field_options))
44
+ concat(
45
+ if govuk_checkboxes_options[:form]
46
+ govuk_checkbox_field_form(govuk_checkboxes_options[:form], attribute, items, **govuk_field_options)
47
+ else
48
+ govuk_checkbox_field_tag(attribute, items, **govuk_field_options)
49
+ end
50
+ )
76
51
  end
77
52
  end
78
53
 
@@ -88,30 +63,28 @@ module CrownMarketplaceUtils
88
63
  #
89
64
  # @return [ActiveSupport::SafeBuffer] the HTML for the checkbox buttons which is used in {govuk_checkboxes}
90
65
 
91
- def govuk_checkboxes_fields(attribute, items, **govuk_checkboxes_options)
66
+ def govuk_checkbox_field_tag(attribute, items, **govuk_checkboxes_options)
92
67
  _govuk_checkboxes_fields(items, **govuk_checkboxes_options) do |checkbox_item|
93
- govuk_checkbox_item(attribute, checkbox_item)
68
+ govuk_checkbox_item_tag(attribute, checkbox_item)
94
69
  end
95
70
  end
96
71
 
97
- # Generates the checkboxes HTML for {govuk_checkboxes_with_form}
72
+ # Generates the checkboxes HTML for {govuk_checkboxes} when there is a ActionView::Helpers::FormBuilder
98
73
  #
99
74
  # @param form [ActionView::Helpers::FormBuilder] the form builder used to create the checkbox buttons
100
- # @param attribute [String, Symbol] the attribute of the raido buttons
101
- # @param items [Array] array of checkbox items, see {_govuk_checkboxes_fields}
102
- # @param govuk_checkboxes_options [Hash] options that will be used in customising the HTML
75
+ # @param (see govuk_checkbox_field_tag)
103
76
  #
104
77
  # @option (see _govuk_checkboxes_fields)
105
78
  #
106
- # @return [ActiveSupport::SafeBuffer] the HTML for the checkbox buttons which is used in {govuk_checkboxes_with_form}
79
+ # @return (see govuk_checkbox_field_tag)
107
80
 
108
- def govuk_checkboxes_fields_with_form(form, attribute, items, **govuk_checkboxes_options)
81
+ def govuk_checkbox_field_form(form, attribute, items, **govuk_checkboxes_options)
109
82
  _govuk_checkboxes_fields(items, **govuk_checkboxes_options) do |checkbox_item|
110
- govuk_checkbox_item_with_form(form, attribute, checkbox_item)
83
+ govuk_checkbox_item_form(form, attribute, checkbox_item)
111
84
  end
112
85
  end
113
86
 
114
- # Wrapper method used by {govuk_checkboxes_fields} and {govuk_checkboxes_fields_with_form} to generate the checkboxes HTML
87
+ # Wrapper method used by {govuk_checkbox_field_tag} and {govuk_checkbox_field_form} to generate the checkboxes HTML
115
88
  #
116
89
  # @param items [Array] array of checkbox items.
117
90
  # Each item is a hash which can be:
@@ -122,18 +95,18 @@ module CrownMarketplaceUtils
122
95
  # @option govuk_checkboxes_options [String] :classes additional CSS classes for the checkboxes HTML
123
96
  # @option govuk_checkboxes_options [Hash] :attributes ({ module: 'govuk-checkboxes' }) any additional attributes that will added as part of the HTML
124
97
  #
125
- # @yield the checkbox item HTML generated by {govuk_checkboxes_fields} or {govuk_checkboxes_fields_with_form}
98
+ # @yield the checkbox item HTML generated by {govuk_checkbox_field_tag} or {govuk_checkbox_field_form}
126
99
  #
127
100
  # @yieldparam checkbox_item [Hash] the current checkbox item to be rendered
128
101
  #
129
- # @return [ActiveSupport::SafeBuffer] the HTML for the checkbox buttons which is used in {govuk_checkboxes_fields} or {govuk_checkboxes_fields_with_form}
102
+ # @return [ActiveSupport::SafeBuffer] the HTML for the checkbox buttons which is used in {govuk_checkbox_field_tag} or {govuk_checkbox_field_form}
130
103
 
131
104
  def _govuk_checkboxes_fields(items, **govuk_checkboxes_options)
132
- govuk_checkboxes_classes = ['govuk-checkboxes']
133
- govuk_checkboxes_classes << govuk_checkboxes_options[:classes]
134
- ((govuk_checkboxes_options[:attributes] ||= {})[:data] ||= {}).merge!({ module: 'govuk-checkboxes' })
105
+ govuk_checkboxes_options[:attributes] ||= {}
106
+ govuk_checkboxes_options[:attributes][:class] = "govuk-checkboxes #{govuk_checkboxes_options[:classes]}".rstrip
107
+ (govuk_checkboxes_options[:attributes][:data] ||= {}).merge!({ module: 'govuk-checkboxes' })
135
108
 
136
- tag.div(class: govuk_checkboxes_classes, **govuk_checkboxes_options[:attributes]) do
109
+ tag.div(**govuk_checkboxes_options[:attributes]) do
137
110
  capture do
138
111
  items.each do |checkbox_item|
139
112
  concat(
@@ -150,7 +123,7 @@ module CrownMarketplaceUtils
150
123
  end
151
124
  end
152
125
 
153
- # Generates the HTML for a checkbox button for {govuk_checkboxes_fields_with_form}
126
+ # Generates the HTML for a checkbox button for {govuk_checkbox_field_form}
154
127
  #
155
128
  # @param (see _govuk_checkbox_item)
156
129
  #
@@ -158,7 +131,7 @@ module CrownMarketplaceUtils
158
131
  #
159
132
  # @return (see _govuk_checkbox_item)
160
133
 
161
- def govuk_checkbox_item(attribute, checkbox_item)
134
+ def govuk_checkbox_item_tag(attribute, checkbox_item)
162
135
  _govuk_checkbox_item(attribute, checkbox_item) do
163
136
  checkbox_item[:attributes][:id] ||= "#{sanitize_to_id(attribute)}_#{sanitize_to_id(checkbox_item[:value])}"
164
137
 
@@ -167,7 +140,7 @@ module CrownMarketplaceUtils
167
140
  end
168
141
  end
169
142
 
170
- # Generates the HTML for a checkbox button for {govuk_checkboxes_fields}
143
+ # Generates the HTML for a checkbox button for {govuk_checkbox_field_tag}
171
144
  #
172
145
  # @param (see _govuk_checkbox_item)
173
146
  # @param form [ActionView::Helpers::FormBuilder] the form builder used to create the checkbox buttons
@@ -176,17 +149,19 @@ module CrownMarketplaceUtils
176
149
  #
177
150
  # @return (see _govuk_checkbox_item)
178
151
 
179
- def govuk_checkbox_item_with_form(form, attribute, checkbox_item)
152
+ def govuk_checkbox_item_form(form, attribute, checkbox_item)
180
153
  _govuk_checkbox_item(attribute, checkbox_item) do
181
154
  (checkbox_item[:label][:attributes] ||= {})[:value] = checkbox_item[:value]
182
155
  checkbox_item[:label][:attributes][:for] = checkbox_item[:attributes][:id] if checkbox_item[:attributes][:id]
183
156
 
184
157
  concat(form.check_box(attribute, checkbox_item[:attributes].merge({ class: 'govuk-checkboxes__input', multiple: true, include_hidden: false }), checkbox_item[:value]))
185
- concat(govuk_label_with_form(form, attribute, checkbox_item[:label][:text], **checkbox_item[:label]))
158
+ concat(govuk_label(attribute, checkbox_item[:label][:text], form: form, **checkbox_item[:label]))
186
159
  end
187
160
  end
188
161
 
189
- # Wrapper method used by {govuk_checkbox_item} and {govuk_checkbox_item_with_form} to generate the checkboxes HTML
162
+ # rubocop:disable Metrics/AbcSize
163
+
164
+ # Wrapper method used by {govuk_checkbox_item_tag} and {govuk_checkbox_item_form} to generate the checkboxes HTML
190
165
  # including the label and hint, if there is one.
191
166
  #
192
167
  # @param attribute [String, Symbol] the attribute of the raido buttons
@@ -196,23 +171,32 @@ module CrownMarketplaceUtils
196
171
  # @option checkbox_item [Hash] :label the parameters that will be used to create the label for the checkbox button, see {govuk_label}
197
172
  # @option checkbox_item [Hash] :hint (nil) the parameters that will be used to create the hint for the checkbox button, see {govuk_hint}.
198
173
  # If no hint is given then no hint will be rendered
174
+ # @option checkbox_item [Hash] :conditional (nil) content that will appear when the checkbox input 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
199
179
  # @option checkbox_item [Hash] :attributes ({}) any additional attributes that will be added as part of the checkbox button HTML
200
180
  #
201
181
  # @return [ActiveSupport::SafeBuffer] the HTML for the checkbox buttons, label and hint
202
- # which is used in {govuk_checkbox_item} and {govuk_checkbox_item_with_form}
182
+ # which is used in {govuk_checkbox_item_tag} and {govuk_checkbox_item_form}
203
183
 
204
184
  def _govuk_checkbox_item(attribute, checkbox_item)
205
185
  checkbox_item[:attributes] ||= {}
206
186
  checkbox_item[:label] ||= {}
207
187
  checkbox_item[:label][:classes] = "govuk-checkboxes__label #{checkbox_item[:label][:classes]}".rstrip
208
188
 
209
- set_item_options_for_hint('checkboxes', attribute, checkbox_item) if checkbox_item[:hint]
189
+ set_item_options_for_hint('checkboxes', attribute, checkbox_item)
190
+ set_conditional_item_options('checkboxes', attribute, checkbox_item)
210
191
 
211
192
  capture do
212
193
  yield
213
194
  concat(govuk_hint(checkbox_item[:hint][:text], **checkbox_item[:hint])) if checkbox_item[:hint]
195
+ concat(tag.div(checkbox_item[:conditional][:content], class: checkbox_item[:conditional][:attributes][:class], id: checkbox_item[:conditional][:attributes][:id])) if checkbox_item[:conditional]
214
196
  end
215
197
  end
198
+
199
+ # rubocop:enable Metrics/AbcSize
216
200
  end
217
201
  end
218
202
  end
@@ -3,7 +3,7 @@
3
3
  require_relative '../field'
4
4
  require_relative 'input'
5
5
 
6
- module CrownMarketplaceUtils
6
+ module CrownMarketplaceUtils::Helpers
7
7
  module GovUkHelper
8
8
  module Field
9
9
  # = GOV.UK Date Input
@@ -20,121 +20,38 @@ module CrownMarketplaceUtils
20
20
  # Generates the HTML for the GOV.UK date input component
21
21
  #
22
22
  # @param attribute [String, Symbol] the attribute of the date input
23
- # @param error_message [String] the error message to be displayed
24
23
  # @param govuk_date_input_options [Hash] options that will be used for the parts of the fieldset, form group, hint and date input
25
24
  #
25
+ # @option govuk_date_input_options [String] :error_message (nil) the error message to be displayed
26
+ # @option govuk_date_input_options [ActiveModel] :model (nil) optional model that can be used to find an error message
27
+ # @option govuk_date_input_options [ActionView::Helpers::FormBuilder] :form (nil) optional form builder used to create
28
+ # the date tag and find the error message
26
29
  # @option govuk_date_input_options [Hash] :form_group see {govuk_fields}
27
30
  # @option govuk_date_input_options [Hash] :fieldset see {govuk_fields}
28
31
  # @option govuk_date_input_options [Hash] :hint see {govuk_field}
29
32
  # @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.
33
+ # See {_govuk_date_input_fields} for more details.
31
34
  #
32
35
  # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Date Input
33
36
  # which can then be rendered on the page
34
37
 
35
- def govuk_date_input(attribute, error_message = nil, **govuk_date_input_options)
38
+ def govuk_date_input(attribute, **govuk_date_input_options)
36
39
  set_govuk_date_input_fieldset_options(**govuk_date_input_options)
37
40
 
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))
41
+ govuk_fields(:date_input, attribute, **govuk_date_input_options) do |govuk_field_options, error_message|
42
+ concat(_govuk_date_input_fields(attribute, error_message, **govuk_field_options) do |date_item_attribute, date_item_input_options|
43
+ govuk_input(
44
+ date_item_attribute,
45
+ model: govuk_date_input_options[:model],
46
+ form: govuk_date_input_options[:form],
47
+ **date_item_input_options
48
+ )
49
+ end)
78
50
  end
79
51
  end
80
52
 
81
53
  private
82
54
 
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
55
  # Sets the role for the fieldset to group
139
56
  #
140
57
  # @param govuk_date_input_options [Hash] options that will be used for the parts of the fieldset, form group, hint and date input
@@ -143,8 +60,7 @@ module CrownMarketplaceUtils
143
60
  (govuk_date_input_options[:fieldset][:attributes] ||= {})[:role] = 'group'
144
61
  end
145
62
 
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
63
+ # Wrapper method used by {govuk_date_input} to generate the date inputs HTML
148
64
  #
149
65
  # @param attribute [String, Symbol] the attribute of the date input
150
66
  # @param any_errors [Boolean] flag to indicate if the inputs need the error class
@@ -161,36 +77,14 @@ module CrownMarketplaceUtils
161
77
  # @yieldparam date_item_attribute [String] the attribute for individual date input
162
78
  # @yieldparam date_item_input_options [Hash] the HTML options used for the date input item
163
79
  #
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}
80
+ # @return [ActiveSupport::SafeBuffer] the HTML for the date inputs which is used in {govuk_date_input}
166
81
 
167
82
  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
83
  govuk_date_input_options[:attributes] ||= {}
84
+ govuk_date_input_options[:attributes][:class] = "govuk-date-input #{govuk_date_input_options[:classes]}".rstrip
85
+ date_items = govuk_date_input_options[:date_items] || govuk_default_date_items
192
86
 
193
- tag.div(class: govuk_date_input_classes, **govuk_date_input_options[:attributes]) do
87
+ tag.div(**govuk_date_input_options[:attributes]) do
194
88
  capture do
195
89
  enumerate_and_set_date_input_options(date_items, any_errors) do |date_item_input_options|
196
90
  concat(tag.div(class: 'govuk-date-input__item') do
@@ -228,6 +122,31 @@ module CrownMarketplaceUtils
228
122
  end
229
123
  end
230
124
  # rubocop:enable Metrics/CyclomaticComplexity
125
+
126
+ # The default date items used in {_govuk_date_input_fields} if no date items are provided
127
+
128
+ def govuk_default_date_items
129
+ [
130
+ {
131
+ name: 'day',
132
+ input: {
133
+ classes: 'govuk-input--width-2'
134
+ }
135
+ },
136
+ {
137
+ name: 'month',
138
+ input: {
139
+ classes: 'govuk-input--width-2'
140
+ }
141
+ },
142
+ {
143
+ name: 'year',
144
+ input: {
145
+ classes: 'govuk-input--width-4'
146
+ }
147
+ }
148
+ ]
149
+ end
231
150
  end
232
151
  end
233
152
  end
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../field'
4
+
5
+ module CrownMarketplaceUtils::Helpers
6
+ module GovUkHelper
7
+ module Field
8
+ # = GOV.UK File Upload
9
+ #
10
+ # This helper is used for generating the file upload component from the
11
+ # {https://design-system.service.gov.uk/components/file-upload GDS - Components - File Upload}
12
+ #
13
+ # This is considered a Field module and so makes use of the methods in {CrownMarketplaceUtils::GovUkHelper::Field}
14
+
15
+ module FileUpload
16
+ include Field
17
+
18
+ # Generates the HTML for the GOV.UK file upload component
19
+ #
20
+ # @param attribute [String, Symbol] the attribute of the file upload
21
+ # @param govuk_file_upload_options [Hash] options that will be used for the parts of the form group, label, hint and file upload
22
+ #
23
+ # @option govuk_file_upload_options [String] :error_message (nil) the error message to be displayed
24
+ # @option govuk_file_upload_options [ActiveModel] :model (nil) optional model that can be used to find an error message
25
+ # @option govuk_file_upload_options [ActionView::Helpers::FormBuilder] :form (nil) optional form builder used to create
26
+ # the file upload input and find the error message
27
+ # @option govuk_file_upload_options [Hash] :form_group see {govuk_field}
28
+ # @option govuk_file_upload_options [Hash] :label see {govuk_field}
29
+ # @option govuk_file_upload_options [Hash] :hint see {govuk_field}
30
+ # @option govuk_file_upload_options [Hash] :file_upload ({}) the options that will be used when rendering the file upload.
31
+ # See {set_file_upload_options} for more details.
32
+ #
33
+ # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK File Upload
34
+ # which can then be rendered on the page
35
+
36
+ def govuk_file_upload(attribute, **govuk_file_upload_options)
37
+ govuk_field(:file_upload, attribute, **govuk_file_upload_options) do |govuk_field_options, error_message|
38
+ set_file_upload_options(error_message, govuk_field_options)
39
+
40
+ concat(
41
+ if govuk_file_upload_options[:form]
42
+ govuk_file_upload_form(govuk_file_upload_options[:form], attribute, **govuk_field_options)
43
+ else
44
+ govuk_file_upload_tag(attribute, **govuk_field_options)
45
+ end
46
+ )
47
+ end
48
+ end
49
+
50
+ private
51
+
52
+ # Generates the file upload HTML for {govuk_file_upload}
53
+ #
54
+ # @param attribute [String, Symbol] the attribute of the file upload
55
+ # @param govuk_file_upload_options [Hash] options that will be used in customising the HTML
56
+ #
57
+ # @option (see set_file_upload_options)
58
+ #
59
+ # @return [ActiveSupport::SafeBuffer] the HTML for the file upload field which is used in {govuk_file_upload}
60
+
61
+ def govuk_file_upload_tag(attribute, **govuk_file_upload_options)
62
+ file_field_tag(attribute, **govuk_file_upload_options[:attributes])
63
+ end
64
+
65
+ # Generates the file upload HTML for {govuk_file_upload} when there is a ActionView::Helpers::FormBuilder
66
+ #
67
+ # @param form [ActionView::Helpers::FormBuilder] the form builder used to create the file upload input
68
+ # @param (see govuk_file_upload_tag)
69
+ #
70
+ # @option (see set_file_upload_options)
71
+ #
72
+ # @return (see govuk_file_upload_tag)
73
+
74
+ def govuk_file_upload_form(form, attribute, **govuk_file_upload_options)
75
+ form.file_field(attribute, **govuk_file_upload_options[:attributes])
76
+ end
77
+
78
+ # Initialises the attributes for the file upload input
79
+ #
80
+ # @param error_message [String] used to indicate if there is an error which will add extra classes
81
+ # @param govuk_file_upload_options [Hash] options that will be used in customising the HTML
82
+ #
83
+ # @option govuk_file_upload_options [String] :classes additional CSS classes for the file iupload HTML
84
+ # @option govuk_file_upload_options [Hash] :attributes ({}) any additional attributes that will added as part of the HTML
85
+
86
+ def set_file_upload_options(error_message, govuk_file_upload_options)
87
+ govuk_file_upload_options[:attributes] ||= {}
88
+ govuk_file_upload_options[:attributes][:class] = "govuk-file-upload #{govuk_file_upload_options[:classes]}".rstrip
89
+ govuk_file_upload_options[:attributes][:class] << ' govuk-file-upload--error' if error_message
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end