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

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 (30) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +1 -1
  3. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/accordion.rb +91 -89
  4. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/back_link.rb +24 -22
  5. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/breadcrumbs.rb +56 -54
  6. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/button.rb +109 -107
  7. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/details.rb +33 -31
  8. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/error_message.rb +49 -47
  9. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/field/character_count.rb +144 -142
  10. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/field/checkboxes.rb +176 -174
  11. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/field/date_input.rb +126 -124
  12. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/field/file_upload.rb +75 -73
  13. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/field/input.rb +137 -135
  14. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/field/radios.rb +176 -174
  15. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/field/select.rb +111 -109
  16. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/field/textarea.rb +86 -84
  17. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/field.rb +181 -179
  18. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/fieldset.rb +56 -54
  19. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/form_group.rb +37 -35
  20. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/header.rb +142 -140
  21. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/hint.rb +25 -23
  22. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/label.rb +69 -67
  23. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/notification_banner.rb +121 -119
  24. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/pagination.rb +290 -288
  25. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/step_by_step_navigation.rb +187 -185
  26. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/tag.rb +29 -27
  27. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/warning_text.rb +39 -37
  28. data/lib/crown_marketplace_utils/helpers/gov_uk_helper.rb +31 -29
  29. data/lib/crown_marketplace_utils/version.rb +1 -1
  30. metadata +1 -1
@@ -2,102 +2,104 @@
2
2
 
3
3
  require_relative '../field'
4
4
 
5
- module CrownMarketplaceUtils::Helpers
6
- module GovUkHelper
7
- module Field
8
- # = GOV.UK Textarea
9
- #
10
- # This helper is used for generating the textarea component from the
11
- # {https://design-system.service.gov.uk/components/textarea GDS - Components - Textarea}
12
- #
13
- # This is considered a Field module and so makes use of the methods in {CrownMarketplaceUtils::GovUkHelper::Field}
14
-
15
- module Textarea
16
- include Field
17
-
18
- # Generates the HTML for the GOV.UK textarea component
19
- #
20
- # @param attribute [String, Symbol] the attribute of the textarea
21
- # @param govuk_textarea_options [Hash] options that will be used for the parts of the form group, label, hint and textarea
5
+ module CrownMarketplaceUtils
6
+ module Helpers
7
+ module GovUkHelper
8
+ module Field
9
+ # = GOV.UK Textarea
22
10
  #
23
- # @option govuk_textarea_options [String] :error_message (nil) the error message to be displayed
24
- # @option govuk_textarea_options [ActiveModel] :model (nil) optional model that can be used to find an error message
25
- # @option govuk_textarea_options [ActionView::Helpers::FormBuilder] :form (nil) optional form builder used to create
26
- # the textarea tag and find the error message
27
- # @option govuk_textarea_options [Hash] :form_group see {govuk_field}
28
- # @option govuk_textarea_options [Hash] :label see {govuk_field}
29
- # @option govuk_textarea_options [Hash] :hint see {govuk_field}
30
- # @option govuk_textarea_options [Hash] :textarea ({}) the options that will be used when rendering the textarea.
31
- # See {set_govuk_textarea_field_options} for more details.
11
+ # This helper is used for generating the textarea component from the
12
+ # {https://design-system.service.gov.uk/components/textarea GDS - Components - Textarea}
32
13
  #
33
- # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK textarea
34
- # which can then be rendered on the page
14
+ # This is considered a Field module and so makes use of the methods in {CrownMarketplaceUtils::GovUkHelper::Field}
35
15
 
36
- def govuk_textarea(attribute, **govuk_textarea_options)
37
- govuk_field(:textarea, attribute, **govuk_textarea_options) do |govuk_field_options, error_message|
38
- set_govuk_textarea_field_options(error_message, govuk_field_options)
39
- (govuk_textarea_options[:textarea] ||= {})[:content] = govuk_textarea_options[:model].send(attribute) if govuk_textarea_options[:model]
16
+ module Textarea
17
+ include Field
40
18
 
41
- concat(
42
- if govuk_textarea_options[:form]
43
- govuk_textarea_form(govuk_textarea_options[:form], attribute, **govuk_field_options)
44
- else
45
- govuk_textarea_tag(attribute, **govuk_field_options)
46
- end
47
- )
19
+ # Generates the HTML for the GOV.UK textarea component
20
+ #
21
+ # @param attribute [String, Symbol] the attribute of the textarea
22
+ # @param govuk_textarea_options [Hash] options that will be used for the parts of the form group, label, hint and textarea
23
+ #
24
+ # @option govuk_textarea_options [String] :error_message (nil) the error message to be displayed
25
+ # @option govuk_textarea_options [ActiveModel] :model (nil) optional model that can be used to find an error message
26
+ # @option govuk_textarea_options [ActionView::Helpers::FormBuilder] :form (nil) optional form builder used to create
27
+ # the textarea tag and find the error message
28
+ # @option govuk_textarea_options [Hash] :form_group see {govuk_field}
29
+ # @option govuk_textarea_options [Hash] :label see {govuk_field}
30
+ # @option govuk_textarea_options [Hash] :hint see {govuk_field}
31
+ # @option govuk_textarea_options [Hash] :textarea ({}) the options that will be used when rendering the textarea.
32
+ # See {set_govuk_textarea_field_options} for more details.
33
+ #
34
+ # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK textarea
35
+ # which can then be rendered on the page
36
+
37
+ def govuk_textarea(attribute, **govuk_textarea_options)
38
+ govuk_field(:textarea, attribute, **govuk_textarea_options) do |govuk_field_options, error_message|
39
+ set_govuk_textarea_field_options(error_message, govuk_field_options)
40
+ (govuk_textarea_options[:textarea] ||= {})[:content] = govuk_textarea_options[:model].send(attribute) if govuk_textarea_options[:model]
41
+
42
+ concat(
43
+ if govuk_textarea_options[:form]
44
+ govuk_textarea_form(govuk_textarea_options[:form], attribute, **govuk_field_options)
45
+ else
46
+ govuk_textarea_tag(attribute, **govuk_field_options)
47
+ end
48
+ )
49
+ end
48
50
  end
49
- end
50
51
 
51
- private
52
+ private
52
53
 
53
- # Generates the textarea HTML for {govuk_textarea}
54
- #
55
- # @param attribute [String, Symbol] the attribute of the textarea
56
- # @param govuk_text_textarea_options [Hash] options that will be used in customising the HTML
57
- #
58
- # @option (see set_govuk_textarea_field_options)
59
- #
60
- # @return [ActiveSupport::SafeBuffer] the HTML for the textarea field which is used in {govuk_textarea}
54
+ # Generates the textarea HTML for {govuk_textarea}
55
+ #
56
+ # @param attribute [String, Symbol] the attribute of the textarea
57
+ # @param govuk_text_textarea_options [Hash] options that will be used in customising the HTML
58
+ #
59
+ # @option (see set_govuk_textarea_field_options)
60
+ #
61
+ # @return [ActiveSupport::SafeBuffer] the HTML for the textarea field which is used in {govuk_textarea}
61
62
 
62
- def govuk_textarea_tag(attribute, **govuk_text_textarea_options)
63
- text_area_tag(
64
- attribute,
65
- govuk_text_textarea_options[:content],
66
- **govuk_text_textarea_options[:attributes]
67
- )
68
- end
63
+ def govuk_textarea_tag(attribute, **govuk_text_textarea_options)
64
+ text_area_tag(
65
+ attribute,
66
+ govuk_text_textarea_options[:content],
67
+ **govuk_text_textarea_options[:attributes]
68
+ )
69
+ end
69
70
 
70
- # Generates the textarea HTML for {govuk_textarea} when there is a ActionView::Helpers::FormBuilder
71
- #
72
- # @param form [ActionView::Helpers::FormBuilder] the form builder used to create the textarea
73
- # @param (see govuk_textarea_tag)
74
- #
75
- # @option (see set_govuk_textarea_field_options)
76
- #
77
- # @return (see govuk_textarea_tag)
71
+ # Generates the textarea HTML for {govuk_textarea} when there is a ActionView::Helpers::FormBuilder
72
+ #
73
+ # @param form [ActionView::Helpers::FormBuilder] the form builder used to create the textarea
74
+ # @param (see govuk_textarea_tag)
75
+ #
76
+ # @option (see set_govuk_textarea_field_options)
77
+ #
78
+ # @return (see govuk_textarea_tag)
78
79
 
79
- def govuk_textarea_form(form, attribute, **govuk_text_textarea_options)
80
- form.text_area(
81
- attribute,
82
- **govuk_text_textarea_options[:attributes]
83
- )
84
- end
80
+ def govuk_textarea_form(form, attribute, **govuk_text_textarea_options)
81
+ form.text_area(
82
+ attribute,
83
+ **govuk_text_textarea_options[:attributes]
84
+ )
85
+ end
85
86
 
86
- # Initialises the attributes for the textarea input
87
- #
88
- # @param error_message [String] used to indicate if there is an error which will add extra classes
89
- # @param govuk_text_textarea_options [Hash] options that will be used in customising the HTML
90
- #
91
- # @option govuk_text_textarea_options [String] :classes additional CSS classes for the textarea HTML
92
- # @option govuk_text_textarea_options [String] :content (nil) the content of the textarea
93
- # @option govuk_text_textarea_options [String] :rows (5) the number of rows for the text area
94
- # @option govuk_text_textarea_options [Hash] :attributes ({}) any additional attributes that will added as part of the HTML
87
+ # Initialises the attributes for the textarea input
88
+ #
89
+ # @param error_message [String] used to indicate if there is an error which will add extra classes
90
+ # @param govuk_text_textarea_options [Hash] options that will be used in customising the HTML
91
+ #
92
+ # @option govuk_text_textarea_options [String] :classes additional CSS classes for the textarea HTML
93
+ # @option govuk_text_textarea_options [String] :content (nil) the content of the textarea
94
+ # @option govuk_text_textarea_options [String] :rows (5) the number of rows for the text area
95
+ # @option govuk_text_textarea_options [Hash] :attributes ({}) any additional attributes that will added as part of the HTML
95
96
 
96
- def set_govuk_textarea_field_options(error_message, govuk_text_textarea_options)
97
- govuk_text_textarea_options[:attributes] ||= {}
98
- govuk_text_textarea_options[:attributes][:class] = "govuk-textarea #{govuk_text_textarea_options[:classes]}".rstrip
99
- govuk_text_textarea_options[:attributes][:class] << ' govuk-textarea--error' if error_message
100
- govuk_text_textarea_options[:attributes][:rows] ||= govuk_text_textarea_options[:rows] || 5
97
+ def set_govuk_textarea_field_options(error_message, govuk_text_textarea_options)
98
+ govuk_text_textarea_options[:attributes] ||= {}
99
+ govuk_text_textarea_options[:attributes][:class] = "govuk-textarea #{govuk_text_textarea_options[:classes]}".rstrip
100
+ govuk_text_textarea_options[:attributes][:class] << ' govuk-textarea--error' if error_message
101
+ govuk_text_textarea_options[:attributes][:rows] ||= govuk_text_textarea_options[:rows] || 5
102
+ end
101
103
  end
102
104
  end
103
105
  end
@@ -5,211 +5,213 @@ require_relative 'form_group'
5
5
  require_relative 'hint'
6
6
  require_relative 'label'
7
7
 
8
- module CrownMarketplaceUtils::Helpers
9
- module GovUkHelper
10
- # = GOV.UK Field
11
- #
12
- # This module contains methods to wrap the:
13
- # - fieldset
14
- # - form group
15
- # - label
16
- # - hint
17
- # - error message
18
- # around some kind of input.
19
- #
20
- # The wrapper functions in this module are used
21
- # to create the fields using the structure of a GDS input field component.
22
-
23
- module Field
24
- include Fieldset
25
- include FormGroup
26
- include Hint
27
- include Label
28
-
29
- # Generates the HTML to wrap arround a GDS input field component.
30
- #
31
- # @param field [Symbol] the type of the field
32
- # @param attribute [String, Symbol] the attribute of the field
33
- # @param govuk_field_options [Hash] options that will be used for the parts of the form group, label, hint and field
34
- #
35
- # @option govuk_field_options [String] :error_message (nil) the error message to be displayed
36
- # @option govuk_field_options [ActiveModel] :model (nil) optional model that can be used to find an error message
37
- # @option govuk_field_options [ActionView::Helpers::FormBuilder] :form (nil) optional form builder used to create
38
- # the field and find the error message
39
- # @option govuk_field_options [Hash] :form_group ({}) the options for govuk_form_group {govuk_form_group}
40
- # @option govuk_field_options [Hash] :label the parameters that will be used to create the label for the field, see {govuk_label}
41
- # @option govuk_field_options [Hash] :hint (nil) the parameters that will be used to create the hint for the field, see {govuk_hint}.
42
- # If no hint is given then no hint will be rendered
43
- # @option govuk_field_options [Hash] :field_options ({}) the options that will be used when rendering the field.
44
- # For more details look at the specific module that uses +Field+.
45
- #
46
- # @yield the field HTML
47
- #
48
- # @yieldparam govuk_field_options [Hash] the HTML options used when rendering the field
49
- # @yieldparam error_message [String] flag to indicate if there are any erros
50
- #
51
- # @return [ActiveSupport::SafeBuffer] the HTML that wraps arround the field
52
-
53
- def govuk_field(field, attribute, **govuk_field_options)
54
- set_label_for_if_custom_id(field, govuk_field_options)
55
- set_hint_id(attribute, govuk_field_options)
56
- error_message = find_field_error_message(attribute, govuk_field_options)
57
-
58
- govuk_form_group(attribute, error_message: error_message, **(govuk_field_options[:form_group] || {})) do |display_error_message|
59
- set_field_described_by(field, attribute, error_message, govuk_field_options)
60
-
61
- capture do
62
- concat(govuk_label(attribute, govuk_field_options[:label][:text], form: govuk_field_options[:form], **govuk_field_options[:label]))
63
- concat(govuk_hint(govuk_field_options[:hint][:text], **govuk_field_options[:hint])) if govuk_field_options[:hint]
64
- concat(display_error_message)
65
- yield(govuk_field_options[field], error_message)
8
+ module CrownMarketplaceUtils
9
+ module Helpers
10
+ module GovUkHelper
11
+ # = GOV.UK Field
12
+ #
13
+ # This module contains methods to wrap the:
14
+ # - fieldset
15
+ # - form group
16
+ # - label
17
+ # - hint
18
+ # - error message
19
+ # around some kind of input.
20
+ #
21
+ # The wrapper functions in this module are used
22
+ # to create the fields using the structure of a GDS input field component.
23
+
24
+ module Field
25
+ include Fieldset
26
+ include FormGroup
27
+ include Hint
28
+ include Label
29
+
30
+ # Generates the HTML to wrap arround a GDS input field component.
31
+ #
32
+ # @param field [Symbol] the type of the field
33
+ # @param attribute [String, Symbol] the attribute of the field
34
+ # @param govuk_field_options [Hash] options that will be used for the parts of the form group, label, hint and field
35
+ #
36
+ # @option govuk_field_options [String] :error_message (nil) the error message to be displayed
37
+ # @option govuk_field_options [ActiveModel] :model (nil) optional model that can be used to find an error message
38
+ # @option govuk_field_options [ActionView::Helpers::FormBuilder] :form (nil) optional form builder used to create
39
+ # the field and find the error message
40
+ # @option govuk_field_options [Hash] :form_group ({}) the options for govuk_form_group {govuk_form_group}
41
+ # @option govuk_field_options [Hash] :label the parameters that will be used to create the label for the field, see {govuk_label}
42
+ # @option govuk_field_options [Hash] :hint (nil) the parameters that will be used to create the hint for the field, see {govuk_hint}.
43
+ # If no hint is given then no hint will be rendered
44
+ # @option govuk_field_options [Hash] :field_options ({}) the options that will be used when rendering the field.
45
+ # For more details look at the specific module that uses +Field+.
46
+ #
47
+ # @yield the field HTML
48
+ #
49
+ # @yieldparam govuk_field_options [Hash] the HTML options used when rendering the field
50
+ # @yieldparam error_message [String] flag to indicate if there are any erros
51
+ #
52
+ # @return [ActiveSupport::SafeBuffer] the HTML that wraps arround the field
53
+
54
+ def govuk_field(field, attribute, **govuk_field_options)
55
+ set_label_for_if_custom_id(field, govuk_field_options)
56
+ set_hint_id(attribute, govuk_field_options)
57
+ error_message = find_field_error_message(attribute, govuk_field_options)
58
+
59
+ govuk_form_group(attribute, error_message: error_message, **(govuk_field_options[:form_group] || {})) do |display_error_message|
60
+ set_field_described_by(field, attribute, error_message, govuk_field_options)
61
+
62
+ capture do
63
+ concat(govuk_label(attribute, govuk_field_options[:label][:text], form: govuk_field_options[:form], **govuk_field_options[:label]))
64
+ concat(govuk_hint(govuk_field_options[:hint][:text], **govuk_field_options[:hint])) if govuk_field_options[:hint]
65
+ concat(display_error_message)
66
+ yield(govuk_field_options[field], error_message)
67
+ end
66
68
  end
67
69
  end
68
- end
69
70
 
70
- # Generates the HTML to wrap arround a GDS input fields component.
71
- # These are inputs that require being wrapped in a fieldset, for example radio buttons.
72
- #
73
- # @param field [Symbol] the type of the fields
74
- # @param attribute [String, Symbol] the attribute of the fields
75
- # @param govuk_fields_options [Hash] options that will be used for the parts of the fieldset, form group, hint and fields
76
- #
77
- # @option govuk_fields_options [String] :error_message (nil) the error message to be displayed
78
- # @option govuk_fields_options [ActiveModel] :model (nil) optional model that can be used to find an error message
79
- # @option govuk_fields_options [ActionView::Helpers::FormBuilder] :form (nil) optional form builder used to create
80
- # the field and find the error message
81
- # @option govuk_fields_options [Hash] :form_group ({}) the options for govuk_form_group {govuk_form_group}
82
- # @option govuk_fields_options [Hash] :fieldset ({}) the options for govuk_fieldset {govuk_fieldset}
83
- # @option govuk_fields_options [Hash] :hint (nil) the parameters that will be used to create the hint for the field, see {govuk_hint}.
84
- # If no hint is given then no hint will be rendered
85
- # @option govuk_fields_options [Hash] :field_options ({}) the options that will be used when rendering the fields.
86
- # For more details look at a module that uses +Field+.
87
- #
88
- # @yield the fields HTML
89
- #
90
- # @yieldparam govuk_fields_options [Hash] the HTML options used when rendering the fields
91
- # @yieldparam error_message [String] flag to indicate if there are any erros
92
- #
93
- # @return [ActiveSupport::SafeBuffer] the HTML that wraps arround the fields
94
-
95
- def govuk_fields(field, attribute, **govuk_fields_options)
96
- set_hint_id(attribute, govuk_fields_options)
97
- error_message = find_field_error_message(attribute, govuk_fields_options)
98
-
99
- govuk_form_group(attribute, error_message: error_message, **(govuk_fields_options[:form_group] || {})) do |display_error_message|
100
- set_field_described_by(:fieldset, attribute, error_message, govuk_fields_options)
101
- (govuk_fields_options[field] ||= {})[:attributes] ||= {}
102
-
103
- govuk_fieldset(**govuk_fields_options[:fieldset]) do
104
- concat(capture do
105
- concat(govuk_hint(govuk_fields_options[:hint][:text], **govuk_fields_options[:hint])) if govuk_fields_options[:hint]
106
- concat(display_error_message)
107
- yield(govuk_fields_options[field], error_message)
108
- end)
71
+ # Generates the HTML to wrap arround a GDS input fields component.
72
+ # These are inputs that require being wrapped in a fieldset, for example radio buttons.
73
+ #
74
+ # @param field [Symbol] the type of the fields
75
+ # @param attribute [String, Symbol] the attribute of the fields
76
+ # @param govuk_fields_options [Hash] options that will be used for the parts of the fieldset, form group, hint and fields
77
+ #
78
+ # @option govuk_fields_options [String] :error_message (nil) the error message to be displayed
79
+ # @option govuk_fields_options [ActiveModel] :model (nil) optional model that can be used to find an error message
80
+ # @option govuk_fields_options [ActionView::Helpers::FormBuilder] :form (nil) optional form builder used to create
81
+ # the field and find the error message
82
+ # @option govuk_fields_options [Hash] :form_group ({}) the options for govuk_form_group {govuk_form_group}
83
+ # @option govuk_fields_options [Hash] :fieldset ({}) the options for govuk_fieldset {govuk_fieldset}
84
+ # @option govuk_fields_options [Hash] :hint (nil) the parameters that will be used to create the hint for the field, see {govuk_hint}.
85
+ # If no hint is given then no hint will be rendered
86
+ # @option govuk_fields_options [Hash] :field_options ({}) the options that will be used when rendering the fields.
87
+ # For more details look at a module that uses +Field+.
88
+ #
89
+ # @yield the fields HTML
90
+ #
91
+ # @yieldparam govuk_fields_options [Hash] the HTML options used when rendering the fields
92
+ # @yieldparam error_message [String] flag to indicate if there are any erros
93
+ #
94
+ # @return [ActiveSupport::SafeBuffer] the HTML that wraps arround the fields
95
+
96
+ def govuk_fields(field, attribute, **govuk_fields_options)
97
+ set_hint_id(attribute, govuk_fields_options)
98
+ error_message = find_field_error_message(attribute, govuk_fields_options)
99
+
100
+ govuk_form_group(attribute, error_message: error_message, **(govuk_fields_options[:form_group] || {})) do |display_error_message|
101
+ set_field_described_by(:fieldset, attribute, error_message, govuk_fields_options)
102
+ (govuk_fields_options[field] ||= {})[:attributes] ||= {}
103
+
104
+ govuk_fieldset(**govuk_fields_options[:fieldset]) do
105
+ concat(capture do
106
+ concat(govuk_hint(govuk_fields_options[:hint][:text], **govuk_fields_options[:hint])) if govuk_fields_options[:hint]
107
+ concat(display_error_message)
108
+ yield(govuk_fields_options[field], error_message)
109
+ end)
110
+ end
109
111
  end
110
112
  end
111
- end
112
-
113
- private
114
113
 
115
- # If present, will find the error message for the field
116
- #
117
- # @param attribute [String, Symbol] the attribute of the fields
118
- #
119
- # @option govuk_field_options [String] :error_message (nil) the error message to be displayed
120
- # @option govuk_field_options [ActiveModel] :model (nil) optional model that can be used to find an error message
121
- # @option govuk_field_options [ActionView::Helpers::FormBuilder] :form (nil) optional form builder used to create
122
- # the field and find the error message
123
- #
124
- # @return [String] the error message
125
-
126
- def find_field_error_message(attribute, govuk_field_options)
127
- if govuk_field_options[:model]
128
- govuk_field_options[:model].errors[attribute].first
129
- elsif govuk_field_options[:form]
130
- govuk_field_options[:form].object.errors[attribute].first
131
- else
132
- govuk_field_options[:error_message]
114
+ private
115
+
116
+ # If present, will find the error message for the field
117
+ #
118
+ # @param attribute [String, Symbol] the attribute of the fields
119
+ #
120
+ # @option govuk_field_options [String] :error_message (nil) the error message to be displayed
121
+ # @option govuk_field_options [ActiveModel] :model (nil) optional model that can be used to find an error message
122
+ # @option govuk_field_options [ActionView::Helpers::FormBuilder] :form (nil) optional form builder used to create
123
+ # the field and find the error message
124
+ #
125
+ # @return [String] the error message
126
+
127
+ def find_field_error_message(attribute, govuk_field_options)
128
+ if govuk_field_options[:model]
129
+ govuk_field_options[:model].errors[attribute].first
130
+ elsif govuk_field_options[:form]
131
+ govuk_field_options[:form].object.errors[attribute].first
132
+ else
133
+ govuk_field_options[:error_message]
134
+ end
133
135
  end
134
- end
135
136
 
136
- # Sets the label +for+ if a custom ID has been given for the field.
137
- #
138
- # @param govuk_field_options [Hash] see {govuk_field} for details
137
+ # Sets the label +for+ if a custom ID has been given for the field.
138
+ #
139
+ # @param govuk_field_options [Hash] see {govuk_field} for details
139
140
 
140
- def set_label_for_if_custom_id(field, govuk_field_options)
141
- field_id = govuk_field_options.dig(field, :attributes, :id)
141
+ def set_label_for_if_custom_id(field, govuk_field_options)
142
+ field_id = govuk_field_options.dig(field, :attributes, :id)
142
143
 
143
- govuk_field_options[:label] ||= {}
144
- (govuk_field_options[:label][:attributes] ||= {}).merge!({ for: field_id }) if field_id
145
- end
144
+ govuk_field_options[:label] ||= {}
145
+ (govuk_field_options[:label][:attributes] ||= {}).merge!({ for: field_id }) if field_id
146
+ end
146
147
 
147
- # Sets the hint ID if there is a hint, and the ID for the hint has not been sent
148
- #
149
- # @param attribute [String, Symbol] the attribute of the field
150
- # @param govuk_field_options [Hash] see {govuk_field} for details
148
+ # Sets the hint ID if there is a hint, and the ID for the hint has not been sent
149
+ #
150
+ # @param attribute [String, Symbol] the attribute of the field
151
+ # @param govuk_field_options [Hash] see {govuk_field} for details
151
152
 
152
- def set_hint_id(attribute, govuk_field_options)
153
- return if !govuk_field_options[:hint] || govuk_field_options.dig(:hint, :attributes, :id)
153
+ def set_hint_id(attribute, govuk_field_options)
154
+ return if !govuk_field_options[:hint] || govuk_field_options.dig(:hint, :attributes, :id)
154
155
 
155
- govuk_field_options[:hint] ||= {}
156
- (govuk_field_options[:hint][:attributes] ||= {}).merge!({ id: "#{attribute}-hint" })
157
- end
156
+ govuk_field_options[:hint] ||= {}
157
+ (govuk_field_options[:hint][:attributes] ||= {}).merge!({ id: "#{attribute}-hint" })
158
+ end
158
159
 
159
- # Adds the aira-describedby attribute for the field
160
- # if there is a hint or an error message
161
- #
162
- # @param attribute [String, Symbol] the attribute of the input
163
- # @param error_message [String] used to indicate if there is an error
164
- # @param govuk_field_options [Hash] see {#govuk_field} for details
160
+ # Adds the aira-describedby attribute for the field
161
+ # if there is a hint or an error message
162
+ #
163
+ # @param attribute [String, Symbol] the attribute of the input
164
+ # @param error_message [String] used to indicate if there is an error
165
+ # @param govuk_field_options [Hash] see {#govuk_field} for details
165
166
 
166
- def set_field_described_by(field, attribute, error_message, govuk_field_options)
167
- aria_described_by = []
168
- aria_described_by << govuk_field_options.dig(field, :attributes, :aria, :describedby)
169
- aria_described_by << govuk_field_options[:hint][:attributes][:id] if govuk_field_options[:hint]
170
- aria_described_by << "#{attribute}-error" if error_message
171
- aria_described_by.compact!
167
+ def set_field_described_by(field, attribute, error_message, govuk_field_options)
168
+ aria_described_by = []
169
+ aria_described_by << govuk_field_options.dig(field, :attributes, :aria, :describedby)
170
+ aria_described_by << govuk_field_options[:hint][:attributes][:id] if govuk_field_options[:hint]
171
+ aria_described_by << "#{attribute}-error" if error_message
172
+ aria_described_by.compact!
172
173
 
173
- govuk_field_options[field] ||= {}
174
- govuk_field_options[field][:attributes] ||= {}
174
+ govuk_field_options[field] ||= {}
175
+ govuk_field_options[field][:attributes] ||= {}
175
176
 
176
- return unless aria_described_by.any?
177
+ return unless aria_described_by.any?
177
178
 
178
- (govuk_field_options[field][:attributes][:aria] ||= {}).merge!({ describedby: aria_described_by.join(' ') })
179
- end
179
+ (govuk_field_options[field][:attributes][:aria] ||= {}).merge!({ describedby: aria_described_by.join(' ') })
180
+ end
180
181
 
181
- # Sets the hint attributes and adds the aira-describedby attribute for a fields item
182
- #
183
- # @param type [String] the type of the item
184
- # @param attribute [String, Symbol] the attribute of the item
185
- # @param item [Hash] the options for that item
182
+ # Sets the hint attributes and adds the aira-describedby attribute for a fields item
183
+ #
184
+ # @param type [String] the type of the item
185
+ # @param attribute [String, Symbol] the attribute of the item
186
+ # @param item [Hash] the options for that item
186
187
 
187
- def set_item_options_for_hint(type, attribute, item)
188
- return unless item[:hint]
188
+ def set_item_options_for_hint(type, attribute, item)
189
+ return unless item[:hint]
189
190
 
190
- (item[:hint] ||= {})[:attributes] ||= {}
191
- item[:hint][:classes] = "govuk-#{type}__hint #{item[:hint][:classes]}".rstrip
192
- item[:hint][:attributes][:id] ||= "#{attribute}_#{item[:value]}-item-hint"
191
+ (item[:hint] ||= {})[:attributes] ||= {}
192
+ item[:hint][:classes] = "govuk-#{type}__hint #{item[:hint][:classes]}".rstrip
193
+ item[:hint][:attributes][:id] ||= "#{attribute}_#{item[:value]}-item-hint"
193
194
 
194
- (item[:attributes][:aria] ||= {})[:describedby] = item[:hint][:attributes][:id]
195
- end
195
+ (item[:attributes][:aria] ||= {})[:describedby] = item[:hint][:attributes][:id]
196
+ end
196
197
 
197
- # Sets the conditional attributes and adds the data-aira-controls attribute for a fields item
198
- #
199
- # @param type [String] the type of the item
200
- # @param attribute [String, Symbol] the attribute of the item
201
- # @param item [Hash] the options for that item
198
+ # Sets the conditional attributes and adds the data-aira-controls attribute for a fields item
199
+ #
200
+ # @param type [String] the type of the item
201
+ # @param attribute [String, Symbol] the attribute of the item
202
+ # @param item [Hash] the options for that item
202
203
 
203
- def set_conditional_item_options(type, attribute, item)
204
- return unless item[:conditional]
204
+ def set_conditional_item_options(type, attribute, item)
205
+ return unless item[:conditional]
205
206
 
206
- item[:conditional][:attributes] ||= {}
207
- item[:conditional][:attributes][:class] = "govuk-#{type}__conditional #{"govuk-#{type}__conditional--hidden" unless item[:checked]}".rstrip
208
- item[:conditional][:attributes][:id] ||= sanitize_to_id("#{attribute}_#{item[:value]}_conditional")
209
- (item[:attributes][:data] ||= {})[:'aria-controls'] = item[:conditional][:attributes][:id]
207
+ item[:conditional][:attributes] ||= {}
208
+ item[:conditional][:attributes][:class] = "govuk-#{type}__conditional #{"govuk-#{type}__conditional--hidden" unless item[:checked]}".rstrip
209
+ item[:conditional][:attributes][:id] ||= sanitize_to_id("#{attribute}_#{item[:value]}_conditional")
210
+ (item[:attributes][:data] ||= {})[:'aria-controls'] = item[:conditional][:attributes][:id]
211
+ end
210
212
  end
211
213
  end
212
214
  end
213
- end
214
215
 
215
- ActionView::Base.field_error_proc = proc { |html_tag, _instance| html_tag }
216
+ ActionView::Base.field_error_proc = proc { |html_tag, _instance| html_tag }
217
+ end