crown_marketplace_utils 0.1.0.beta.4 → 0.1.0.beta.6

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.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +1 -1
  3. data/Gemfile.lock +11 -11
  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 +5 -5
  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 Input
@@ -18,100 +18,66 @@ module CrownMarketplaceUtils
18
18
  # Generates the HTML for the GOV.UK input component
19
19
  #
20
20
  # @param attribute [String, Symbol] the attribute of the input
21
- # @param error_message [String] the error message to be displayed
22
21
  # @param govuk_input_options [Hash] options that will be used for the parts of the form group, label, hint and input
23
22
  #
23
+ # @option govuk_input_options [String] :error_message (nil) the error message to be displayed
24
+ # @option govuk_input_options [ActiveModel] :model (nil) optional model that can be used to find an error message
25
+ # @option govuk_label_options [ActionView::Helpers::FormBuilder] :form (nil) optional form builder used to create
26
+ # the input tag and find the error message
24
27
  # @option govuk_input_options [Hash] :form_group see {govuk_field}
25
28
  # @option govuk_input_options [Hash] :label see {govuk_field}
26
29
  # @option govuk_input_options [Hash] :hint see {govuk_field}
27
30
  # @option govuk_input_options [Hash] :input ({}) the options that will be used when rendering the input.
28
- # See {govuk_input_field} for more details.
31
+ # See {_govuk_input_field} for more details.
29
32
  #
30
33
  # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Input
31
34
  # which can then be rendered on the page
32
35
 
33
- def govuk_input(attribute, error_message = nil, **govuk_input_options)
34
- govuk_field(:input, attribute, error_message, **govuk_input_options) do |govuk_field_options|
35
- concat(govuk_input_field(attribute, error_message, **govuk_field_options))
36
- end
37
- end
38
-
39
- # Generates the HTML for the GOV.UK input component using an ActiveModel.
40
- # Unlike {govuk_input}, the method will be able to automatically determine if the error message needs to be shown.
41
- #
42
- # @param model [ActiveModel] model that will be used to find an error message and the value of the input
43
- # @param attribute [String, Symbol] the attribute of the input
44
- # @param govuk_input_options [Hash] options that will be used for the parts of the form group, label, hint and input
45
- #
46
- # @option (see govuk_input)
47
- #
48
- # @return (see govuk_input)
49
-
50
- def govuk_input_with_model(model, attribute, **govuk_input_options)
51
- govuk_field_with_model(:input, model, attribute, **govuk_input_options) do |govuk_field_options, error_message|
52
- govuk_field_options[:value] = model.send(attribute)
53
-
54
- concat(govuk_input_field(attribute, error_message, **govuk_field_options))
55
- end
56
- end
57
-
58
- # Generates the HTML for the GOV.UK input component using an ActionView::Helpers::FormBuilder.
59
- # Unlike {govuk_input}, the method will be able to automatically determine if the error message needs to be shown.
60
- #
61
- # @param form [ActionView::Helpers::FormBuilder] the form builder used to create the input
62
- # @param attribute [String, Symbol] the attribute of the input
63
- # @param govuk_input_options [Hash] options that will be used for the parts of the form group, label, hint and input
64
- #
65
- # @option (see govuk_input)
66
- #
67
- # @return (see govuk_input)
36
+ def govuk_input(attribute, **govuk_input_options)
37
+ govuk_field(:input, attribute, **govuk_input_options) do |govuk_field_options, error_message|
38
+ concat(_govuk_input_field(error_message, **govuk_field_options) do
39
+ field_type = get_field_type(govuk_field_options[:field_type])
40
+ govuk_field_options[:value] = govuk_input_options[:model].send(attribute) if govuk_input_options[:model]
68
41
 
69
- def govuk_input_with_form(form, attribute, **govuk_input_options)
70
- govuk_field_with_form(:input, form, attribute, **govuk_input_options) do |govuk_field_options, error_message|
71
- concat(govuk_input_field_with_form(form, attribute, error_message, **govuk_field_options))
42
+ if govuk_input_options[:form]
43
+ govuk_input_form(govuk_input_options[:form], field_type, attribute, **govuk_field_options)
44
+ else
45
+ govuk_input_tag(field_type, attribute, **govuk_field_options)
46
+ end
47
+ end)
72
48
  end
73
49
  end
74
50
 
75
51
  private
76
52
 
77
- # Generates the input HTML for {govuk_input} and {govuk_input_with_model}
53
+ # Generates the input HTML for {govuk_input}
78
54
  #
55
+ # @param field_type [String] the inout field type
79
56
  # @param attribute [String, Symbol] the attribute of the input
80
- # @param error_message [String] used to indicate if there is an error which will add extra classes
81
57
  # @param govuk_text_input_options [Hash] options that will be used in customising the HTML
82
58
  #
83
59
  # @option (see _govuk_input_field)
84
60
  #
85
61
  # @return [ActiveSupport::SafeBuffer] the HTML for the input field which is used in {govuk_input}
86
62
 
87
- def govuk_input_field(attribute, error_message, **govuk_text_input_options)
88
- _govuk_input_field(error_message, **govuk_text_input_options) do |govuk_text_input_classes, govuk_text_input_attributes|
89
- field_type = :"#{get_field_type(govuk_text_input_options[:field_type])}_tag"
90
-
91
- send(field_type, attribute, govuk_text_input_options[:value], class: govuk_text_input_classes, **govuk_text_input_attributes)
92
- end
63
+ def govuk_input_tag(field_type, attribute, **govuk_text_input_options)
64
+ send("#{field_type}_tag", attribute, govuk_text_input_options[:value], **govuk_text_input_options[:attributes])
93
65
  end
94
66
 
95
- # Generates the input HTML for {govuk_input_with_form}
67
+ # Generates the input HTML for {govuk_input} when there is a ActionView::Helpers::FormBuilder
96
68
  #
97
- # @param form [ActionView::Helpers::FormBuilder] the form builder used to create the input
98
- # @param attribute [String, Symbol] the attribute of the input
99
- # @param error_message [String] used to indicate if there is an error which will add an extra classes
100
- # @param govuk_text_input_options [Hash] options that will be used in customising the HTML
69
+ # @param form [ActionView::Helpers::FormBuilder] the form builder used to create the input tag
70
+ # @param (see govuk_input_tag)
101
71
  #
102
72
  # @option (see _govuk_input_field)
103
73
  #
104
- # @return [ActiveSupport::SafeBuffer] the HTML for the input field which is used in {govuk_input_with_form}
105
-
106
- def govuk_input_field_with_form(form, attribute, error_message, **govuk_text_input_options)
107
- _govuk_input_field(error_message, **govuk_text_input_options) do |govuk_text_input_classes, govuk_text_input_attributes|
108
- field_type = get_field_type(govuk_text_input_options[:field_type])
74
+ # @return (see govuk_input_tag)
109
75
 
110
- form.send(field_type, attribute, class: govuk_text_input_classes, **govuk_text_input_attributes)
111
- end
76
+ def govuk_input_form(form, field_type, attribute, **govuk_text_input_options)
77
+ form.send(field_type, attribute, **govuk_text_input_options[:attributes])
112
78
  end
113
79
 
114
- # Wrapper method used by {govuk_input_field} and {govuk_input_field_with_form} to generate the Input HTML
80
+ # Wrapper method used by {govuk_input} to generate the Input HTML
115
81
  #
116
82
  # @param error_message [String] used to indicate if there is an error which will add extra classes
117
83
  # @param govuk_text_input_options [Hash] options that will be used in customising the HTML
@@ -123,20 +89,16 @@ module CrownMarketplaceUtils
123
89
  # @option govuk_text_input_options [Hash] :suffix (nil) optional suffix for the input field. See {govuk_fix} for more details.
124
90
  # @option govuk_text_input_options [Hash] :attributes ({}) any additional attributes that will added as part of the HTML
125
91
  #
126
- # @yield the input HTML generated by {govuk_input_field} or {govuk_input_field_with_form}
127
- #
128
- # @yieldparam govuk_text_input_classes [Array] the classes for the input HTML
129
- # @yieldparam govuk_text_input_attributes [Hash] additional attributes that will added as part of the HTML
92
+ # @yield the input HTML generated by {govuk_input_tag} or {govuk_input_form}
130
93
  #
131
- # @return [ActiveSupport::SafeBuffer] the HTML for the input field which is used in {govuk_input_field} or {govuk_input_field_with_form}
94
+ # @return [ActiveSupport::SafeBuffer] the HTML for the input field which is used by {govuk_input}
132
95
 
133
96
  def _govuk_input_field(error_message, **govuk_text_input_options)
134
- govuk_text_input_classes = "govuk-input #{govuk_text_input_options[:classes]}".rstrip
135
- govuk_text_input_classes << ' govuk-input--error' if error_message
136
-
137
97
  govuk_text_input_options[:attributes] ||= {}
98
+ govuk_text_input_options[:attributes][:class] = "govuk-input #{govuk_text_input_options[:classes]}".rstrip
99
+ govuk_text_input_options[:attributes][:class] << ' govuk-input--error' if error_message
138
100
 
139
- input_html = yield(govuk_text_input_classes, govuk_text_input_options[:attributes])
101
+ input_html = yield
140
102
 
141
103
  prefix = govuk_text_input_options[:prefix]
142
104
  suffix = govuk_text_input_options[:suffix]
@@ -162,7 +124,7 @@ module CrownMarketplaceUtils
162
124
  # @option govuk_fix_options [String] :classes additional CSS classes for the input HTML
163
125
  # @option govuk_text_input_options [Hash] :attributes ({ aria: { hidden: true } }) any additional attributes that will added as part of the HTML
164
126
  #
165
- # @return [ActiveSupport::SafeBuffer] the HTML for the input field which is used in {govuk_input_field} or {govuk_input_field_with_form}
127
+ # @return [ActiveSupport::SafeBuffer] the HTML for the input field which is used in {_govuk_input_field}
166
128
 
167
129
  def govuk_fix(fix, govuk_fix_options)
168
130
  govuk_fix_classes = ["govuk-input__#{fix}fix"]
@@ -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 Radios
@@ -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 radio items, see {_govuk_radios_fields}
22
- # @param error_message [String] the error message to be displayed
23
22
  # @param govuk_radios_options [Hash] options that will be used for the parts of the fieldset, form group, hint and radio buttons
24
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
25
28
  # @option govuk_radios_options [Hash] :form_group see {govuk_fields}
26
29
  # @option govuk_radios_options [Hash] :fieldset see {govuk_fields}
27
30
  # @option govuk_radios_options [Hash] :hint see {govuk_field}
28
31
  # @option govuk_radios_options [Hash] :radios ({}) the options that will be used when rendering the radio buttons.
29
- # See {govuk_radios_fields} for more details.
32
+ # See {govuk_radio_fields_tag} for more details.
30
33
  #
31
34
  # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Radios
32
35
  # which can then be rendered on the page
33
36
 
34
- def govuk_radios(attribute, items, error_message = nil, **govuk_radios_options)
35
- govuk_fields(:radios, attribute, error_message, **govuk_radios_options) do |govuk_field_options|
36
- concat(govuk_radios_fields(attribute, items, **govuk_field_options))
37
- end
38
- end
39
-
40
- # Generates the HTML for the GOV.UK Radios component using an ActiveModel.
41
- # Unlike {govuk_radios}, 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 selected radio button
44
- # @param attribute [String, Symbol] the attribute of the raido buttons
45
- # @param items [Array] array of radio items, see {_govuk_radios_fields}
46
- # @param govuk_radios_options [Hash] options that will be used for the parts of the fieldset, form group, hint and radio buttons
47
- #
48
- # @option (see govuk_radios)
49
- #
50
- # @return (see govuk_radios)
51
-
52
- def govuk_radios_with_model(model, attribute, items, **govuk_radios_options)
53
- value = model.send(attribute)
54
- items.each { |item| item[:checked] = item[:value] == value }
55
-
56
- govuk_fields_with_model(:radios, model, attribute, **govuk_radios_options) do |govuk_field_options|
57
- concat(govuk_radios_fields(attribute, items, **govuk_field_options))
58
- end
59
- end
60
-
61
- # Generates the HTML for the GOV.UK Radios component using an ActionView::Helpers::FormBuilder.
62
- # Unlike {govuk_radios}, 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 radio buttons
65
- # @param attribute [String, Symbol] the attribute of the raido buttons
66
- # @param items [Array] array of radio items, see {_govuk_radios_fields}
67
- # @param govuk_radios_options [Hash] options that will be used for the parts of the fieldset, form group, hint and radio buttons
68
- #
69
- # @option (see govuk_radios)
70
- #
71
- # @return (see govuk_radios)
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
72
43
 
73
- def govuk_radios_with_form(form, attribute, items, **govuk_radios_options)
74
- govuk_fields_with_form(:radios, form, attribute, **govuk_radios_options) do |govuk_field_options|
75
- concat(govuk_radios_fields_with_form(form, attribute, items, **govuk_field_options))
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)
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 radio buttons which is used in {govuk_radios}
90
65
 
91
- def govuk_radios_fields(attribute, items, **govuk_radios_options)
66
+ def govuk_radio_fields_tag(attribute, items, **govuk_radios_options)
92
67
  _govuk_radios_fields(items, **govuk_radios_options) do |radio_item|
93
- govuk_radio_item(attribute, radio_item)
68
+ govuk_radio_item_tag(attribute, radio_item)
94
69
  end
95
70
  end
96
71
 
97
- # Generates the radios HTML for {govuk_radios_with_form}
72
+ # Generates the radios HTML for {govuk_radios} when there is a ActionView::Helpers::FormBuilder
98
73
  #
99
74
  # @param form [ActionView::Helpers::FormBuilder] the form builder used to create the radio buttons
100
- # @param attribute [String, Symbol] the attribute of the raido buttons
101
- # @param items [Array] array of radio items, see {_govuk_radios_fields}
102
- # @param govuk_radios_options [Hash] options that will be used in customising the HTML
75
+ # @param (see govuk_radio_fields_tag)
103
76
  #
104
77
  # @option (see _govuk_radios_fields)
105
78
  #
106
- # @return [ActiveSupport::SafeBuffer] the HTML for the radio buttons which is used in {govuk_radios_with_form}
79
+ # @return (see govuk_radio_fields_tag)
107
80
 
108
- def govuk_radios_fields_with_form(form, attribute, items, **govuk_radios_options)
81
+ def govuk_radio_fields_form(form, attribute, items, **govuk_radios_options)
109
82
  _govuk_radios_fields(items, **govuk_radios_options) do |radio_item|
110
- govuk_radio_item_with_form(form, attribute, radio_item)
83
+ govuk_radio_item_form(form, attribute, radio_item)
111
84
  end
112
85
  end
113
86
 
114
- # Wrapper method used by {govuk_radios_fields} and {govuk_radios_fields_with_form} to generate the radios HTML
87
+ # Wrapper method used by {govuk_radio_fields_tag} and {govuk_radio_fields_form} to generate the radios HTML
115
88
  #
116
89
  # @param items [Array] array of radio items.
117
90
  # Each item is a hash which can be:
@@ -122,18 +95,18 @@ module CrownMarketplaceUtils
122
95
  # @option govuk_radios_options [String] :classes additional CSS classes for the radios HTML
123
96
  # @option govuk_radios_options [Hash] :attributes ({ module: 'govuk-radios' }) any additional attributes that will added as part of the HTML
124
97
  #
125
- # @yield the radio item HTML generated by {govuk_radios_fields} or {govuk_radios_fields_with_form}
98
+ # @yield the radio item HTML generated by {govuk_radio_fields_tag} or {govuk_radio_fields_form}
126
99
  #
127
100
  # @yieldparam radio_item [Hash] the current radio item to be rendered
128
101
  #
129
- # @return [ActiveSupport::SafeBuffer] the HTML for the radio buttons which is used in {govuk_radios_fields} or {govuk_radios_fields_with_form}
102
+ # @return [ActiveSupport::SafeBuffer] the HTML for the radio buttons which is used in {govuk_radio_fields_tag} or {govuk_radio_fields_form}
130
103
 
131
104
  def _govuk_radios_fields(items, **govuk_radios_options)
132
- govuk_radios_classes = ['govuk-radios']
133
- govuk_radios_classes << govuk_radios_options[:classes]
134
- ((govuk_radios_options[:attributes] ||= {})[:data] ||= {}).merge!({ module: 'govuk-radios' })
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' })
135
108
 
136
- tag.div(class: govuk_radios_classes, **govuk_radios_options[:attributes]) do
109
+ tag.div(**govuk_radios_options[:attributes]) do
137
110
  capture do
138
111
  items.each do |radio_item|
139
112
  concat(
@@ -150,7 +123,7 @@ module CrownMarketplaceUtils
150
123
  end
151
124
  end
152
125
 
153
- # Generates the HTML for a radio button for {govuk_radios_fields_with_form}
126
+ # Generates the HTML for a radio button for {govuk_radio_fields_form}
154
127
  #
155
128
  # @param (see _govuk_radio_item)
156
129
  #
@@ -158,7 +131,7 @@ module CrownMarketplaceUtils
158
131
  #
159
132
  # @return (see _govuk_radio_item)
160
133
 
161
- def govuk_radio_item(attribute, radio_item)
134
+ def govuk_radio_item_tag(attribute, radio_item)
162
135
  _govuk_radio_item(attribute, radio_item) do
163
136
  label_attribute = radio_item[:attributes][:id] || "#{attribute}[#{radio_item[:value]}]"
164
137
 
@@ -167,7 +140,7 @@ module CrownMarketplaceUtils
167
140
  end
168
141
  end
169
142
 
170
- # Generates the HTML for a radio button for {govuk_radios_fields}
143
+ # Generates the HTML for a radio button for {govuk_radio_fields_tag}
171
144
  #
172
145
  # @param (see _govuk_radio_item)
173
146
  # @param form [ActionView::Helpers::FormBuilder] the form builder used to create the radio buttons
@@ -176,17 +149,19 @@ module CrownMarketplaceUtils
176
149
  #
177
150
  # @return (see _govuk_radio_item)
178
151
 
179
- def govuk_radio_item_with_form(form, attribute, radio_item)
152
+ def govuk_radio_item_form(form, attribute, radio_item)
180
153
  _govuk_radio_item(attribute, radio_item) do
181
154
  (radio_item[:label][:attributes] ||= {})[:value] = radio_item[:value]
182
155
  radio_item[:label][:attributes][:for] = radio_item[:attributes][:id] if radio_item[:attributes][:id]
183
156
 
184
157
  concat(form.radio_button(attribute, radio_item[:value], class: 'govuk-radios__input', **radio_item[:attributes]))
185
- concat(govuk_label_with_form(form, attribute, radio_item[:label][:text], **radio_item[:label]))
158
+ concat(govuk_label(attribute, radio_item[:label][:text], form: form, **radio_item[:label]))
186
159
  end
187
160
  end
188
161
 
189
- # Wrapper method used by {govuk_radio_item} and {govuk_radio_item_with_form} to generate the radios HTML
162
+ # rubocop:disable Metrics/AbcSize
163
+
164
+ # Wrapper method used by {govuk_radio_item_tag} and {govuk_radio_item_form} to generate the radios 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 radio_item [Hash] :label the parameters that will be used to create the label for the radio button, see {govuk_label}
197
172
  # @option radio_item [Hash] :hint (nil) the parameters that will be used to create the hint for the radio button, see {govuk_hint}.
198
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
199
179
  # @option radio_item [Hash] :attributes ({}) any additional attributes that will be added as part of the radio button HTML
200
180
  #
201
181
  # @return [ActiveSupport::SafeBuffer] the HTML for the radio buttons, label and hint
202
- # which is used in {govuk_radio_item} and {govuk_radio_item_with_form}
182
+ # which is used in {govuk_radio_item_tag} and {govuk_radio_item_form}
203
183
 
204
184
  def _govuk_radio_item(attribute, radio_item)
205
185
  radio_item[:attributes] ||= {}
206
186
  radio_item[:label] ||= {}
207
187
  radio_item[:label][:classes] = "govuk-radios__label #{radio_item[:label][:classes]}".rstrip
208
188
 
209
- set_item_options_for_hint('radios', attribute, radio_item) if radio_item[:hint]
189
+ set_item_options_for_hint('radios', attribute, radio_item)
190
+ set_conditional_item_options('radios', attribute, radio_item)
210
191
 
211
192
  capture do
212
193
  yield
213
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]
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 'action_view'
4
4
  require_relative '../field'
5
5
 
6
- module CrownMarketplaceUtils
6
+ module CrownMarketplaceUtils::Helpers
7
7
  module GovUkHelper
8
8
  module Field
9
9
  # = GOV.UK Select
@@ -21,59 +21,33 @@ module CrownMarketplaceUtils
21
21
  #
22
22
  # @param attribute [String, Symbol] the attribute of the select field
23
23
  # @param items [Array] array of option items for the select, see {govuk_map_select_items}
24
- # @param error_message [String] the error message to be displayed
25
24
  # @param govuk_select_options [Hash] options that will be used for the parts of the form group, label, hint and select
26
25
  #
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
27
30
  # @option govuk_select_options [Hash] :form_group see {govuk_field}
28
31
  # @option govuk_select_options [Hash] :label see {govuk_field}
29
32
  # @option govuk_select_options [Hash] :hint see {govuk_field}
30
33
  # @option govuk_select_options [Hash] :select ({}) the options that will be used when rendering the select field.
31
- # See {govuk_select_field} for more details.
34
+ # See {set_govuk_select_field_options} for more details.
32
35
  #
33
36
  # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Select
34
37
  # which can then be rendered on the page
35
38
 
36
- def govuk_select(attribute, items, error_message = nil, **govuk_select_options)
37
- govuk_field(:select, attribute, error_message, **govuk_select_options) do |govuk_field_options|
38
- concat(govuk_select_field(attribute, items, error_message, **govuk_field_options))
39
- end
40
- end
41
-
42
- # Generates the HTML for the GOV.UK Select component using an ActiveModel.
43
- # Unlike {govuk_select}, the method will be able to automatically determine if the error message needs to be shown.
44
- #
45
- # @param model [ActiveModel] model that will be used to find an error message and the selected options
46
- # @param attribute [String, Symbol] the attribute of the select field
47
- # @param items [Array] array of option items for the select, see {govuk_map_select_items}
48
- # @param govuk_select_options [Hash] options that will be used for the parts of the form group, label, hint and select
49
- #
50
- # @option see govuk_select)
51
- #
52
- # @return (see govuk_select)
53
-
54
- def govuk_select_with_model(model, attribute, items, **govuk_select_options)
55
- (govuk_select_options[:select] ||= {})[:selected] = model.send(attribute)
56
-
57
- govuk_field_with_model(:select, model, attribute, **govuk_select_options) do |govuk_field_options, error_message|
58
- concat(govuk_select_field(attribute, items, error_message, **govuk_field_options))
59
- end
60
- end
61
-
62
- # Generates the HTML for the GOV.UK Select component using an ActionView::Helpers::FormBuilder.
63
- # Unlike {govuk_select}, 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 select field
66
- # @param attribute [String, Symbol] the attribute of the select field
67
- # @param items [Array] array of option items for the select, see {govuk_map_select_items}
68
- # @param govuk_select_options [Hash] options that will be used for the parts of the form group, label, hint and select
69
- #
70
- # @option see govuk_select)
71
- #
72
- # @return (see govuk_select)
73
-
74
- def govuk_select_with_form(form, attribute, items, **govuk_select_options)
75
- govuk_field_with_form(:select, form, attribute, **govuk_select_options) do |govuk_field_options, error_message|
76
- concat(govuk_select_field_with_form(form, attribute, items, error_message, **govuk_field_options))
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]
43
+
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
+ )
77
51
  end
78
52
  end
79
53
 
@@ -83,47 +57,37 @@ module CrownMarketplaceUtils
83
57
  #
84
58
  # @param attribute [String, Symbol] the attribute of the select field
85
59
  # @param items [Array] array of option items for the select, see {govuk_map_select_items}
86
- # @param error_message [String] used to indicate if there is an error which will add extra classes
87
60
  # @param govuk_select_options [Hash] options that will be used in customising the HTML
88
61
  #
89
62
  # @option (see set_govuk_select_field_options)
90
63
  #
91
64
  # @return [ActiveSupport::SafeBuffer] the HTML for the select field which is used in {govuk_select}
92
65
 
93
- def govuk_select_field(attribute, items, error_message, **govuk_select_options)
94
- set_govuk_select_field_options(error_message, govuk_select_options)
95
-
66
+ def govuk_select_tag(attribute, items, **govuk_select_options)
96
67
  select_tag(
97
68
  attribute,
98
69
  options_for_select(
99
70
  govuk_map_select_items(items),
100
71
  govuk_select_options[:selected]
101
72
  ),
102
- class: govuk_select_options[:classes],
103
73
  **govuk_select_options[:attributes]
104
74
  )
105
75
  end
106
76
 
107
- # Generates the select HTML for {govuk_select_with_form}
77
+ # Generates the select HTML for {govuk_select} when there is a ActionView::Helpers::FormBuilder
108
78
  #
109
79
  # @param form [ActionView::Helpers::FormBuilder] the form builder used to create the select field
110
- # @param attribute [String, Symbol] the attribute of the select field
111
- # @param items [Array] array of option items for the select, see {govuk_map_select_items}
112
- # @param error_message [String] used to indicate if there is an error which will add extra classes
113
- # @param govuk_select_options [Hash] options that will be used in customising the HTML
80
+ # @param (see govuk_select_tag)
114
81
  #
115
82
  # @option (see set_govuk_select_field_options)
116
83
  #
117
- # @return [ActiveSupport::SafeBuffer] the HTML for the select field which is used in {govuk_select_with_form}
118
-
119
- def govuk_select_field_with_form(form, attribute, items, error_message, **govuk_select_options)
120
- set_govuk_select_field_options(error_message, govuk_select_options)
84
+ # @return (see govuk_select_tag)
121
85
 
86
+ def govuk_select_form(form, attribute, items, **govuk_select_options)
122
87
  form.select(
123
88
  attribute,
124
89
  govuk_map_select_items(items),
125
- govuk_select_options[:select] || {},
126
- class: govuk_select_options[:classes],
90
+ govuk_select_options[:select_options] || {},
127
91
  **govuk_select_options[:attributes]
128
92
  )
129
93
  end
@@ -138,13 +102,13 @@ module CrownMarketplaceUtils
138
102
  # @option govuk_select_options [Hash] :attributes ({}) any additional attributes that will added as part of the HTML
139
103
 
140
104
  def set_govuk_select_field_options(error_message, govuk_select_options)
141
- govuk_select_options[:classes] = "govuk-select #{govuk_select_options[:classes]}".rstrip
142
- govuk_select_options[:classes] << ' govuk-select--error' if error_message
143
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
144
108
  end
145
109
 
146
110
  # Maps the items into an array that can be used to generate the options for
147
- # {govuk_select_field} and {govuk_select_field_with_form}
111
+ # {govuk_select_tag} and {govuk_select_form}
148
112
  #
149
113
  # @param items [Array] array of option items for the select
150
114
  #
@@ -153,7 +117,7 @@ module CrownMarketplaceUtils
153
117
  # @option items [String] :value the value of the option item
154
118
  # @option items [Hash] :attributes ({}) any additional attributes that will added as part of the option HTML
155
119
  #
156
- # @return [Array] array of option params that are used in {govuk_select_field} and {govuk_select_field_with_form}
120
+ # @return [Array] array of option params that are used in {govuk_select_tag} and {govuk_select_form}
157
121
 
158
122
  def govuk_map_select_items(items)
159
123
  items.map do |item|