crown_marketplace_utils 0.1.0.beta.6 → 0.1.0.beta.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (30) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +1 -1
  3. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/accordion.rb +91 -89
  4. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/back_link.rb +24 -22
  5. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/breadcrumbs.rb +56 -54
  6. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/button.rb +109 -107
  7. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/details.rb +33 -31
  8. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/error_message.rb +53 -46
  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 +2 -2
@@ -2,162 +2,164 @@
2
2
 
3
3
  require_relative 'textarea'
4
4
 
5
- module CrownMarketplaceUtils::Helpers
6
- module GovUkHelper
7
- module Field
8
- # = GOV.UK Character count
9
- #
10
- # This helper is used for generating the character count component from the
11
- # {https://design-system.service.gov.uk/components/character-count GDS - Components - Character count}
12
- #
13
- # This is a wrapper around a Textarea module and so makes use of the methods in {Textarea}
14
-
15
- module CharacterCount
16
- include Textarea
17
-
18
- # Generates the HTML for the GOV.UK character count component.
19
- # It works by warpping the govuk_textarea in HTML which will trigger the JavaScript to do the character count.
20
- #
21
- # @param attribute [String, Symbol] the attribute of the character count text area
22
- # @param govuk_character_count_options [Hash] options that will be used for the parts of the form group, label, hint, textarea and the character count
23
- #
24
- # @option govuk_character_count_options [String] :error_message (nil) the error message to be displayed
25
- # @option govuk_character_count_options [ActiveModel] :model (nil) optional model that can be used to find an error message
26
- # @option govuk_character_count_options [ActionView::Helpers::FormBuilder] :form (nil) optional form builder used to create
27
- # the textarea tag and find the error message
28
- # @option govuk_character_count_options [Hash] :form_group see {govuk_field}
29
- # @option govuk_character_count_options [Hash] :label see {govuk_field}
30
- # @option govuk_character_count_options [Hash] :hint see {govuk_field}
31
- # @option govuk_character_count_options [Hash] :textarea see {govuk_textarea}
32
- # @option govuk_character_count_options [Hash] :character_count ({}) the options that will be used when rendering the textarea.
33
- # See {_govuk_character_count} for more details.
34
- #
35
- # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK chracter count
36
- # which can then be rendered on the page
37
-
38
- def govuk_character_count(attribute, **govuk_character_count_options)
39
- character_count_attribute = govuk_character_count_options[:form] ? "#{form.object_name}_#{attribute}" : attribute
40
-
41
- _govuk_character_count(character_count_attribute, **govuk_character_count_options) do |govuk_textarea_options|
42
- govuk_textarea(
43
- attribute,
44
- error_message: govuk_character_count_options[:error_message],
45
- model: govuk_character_count_options[:model],
46
- form: govuk_character_count_options[:form],
47
- **govuk_textarea_options
48
- )
5
+ module CrownMarketplaceUtils
6
+ module Helpers
7
+ module GovUkHelper
8
+ module Field
9
+ # = GOV.UK Character count
10
+ #
11
+ # This helper is used for generating the character count component from the
12
+ # {https://design-system.service.gov.uk/components/character-count GDS - Components - Character count}
13
+ #
14
+ # This is a wrapper around a Textarea module and so makes use of the methods in {Textarea}
15
+
16
+ module CharacterCount
17
+ include Textarea
18
+
19
+ # Generates the HTML for the GOV.UK character count component.
20
+ # It works by warpping the govuk_textarea in HTML which will trigger the JavaScript to do the character count.
21
+ #
22
+ # @param attribute [String, Symbol] the attribute of the character count text area
23
+ # @param govuk_character_count_options [Hash] options that will be used for the parts of the form group, label, hint, textarea and the character count
24
+ #
25
+ # @option govuk_character_count_options [String] :error_message (nil) the error message to be displayed
26
+ # @option govuk_character_count_options [ActiveModel] :model (nil) optional model that can be used to find an error message
27
+ # @option govuk_character_count_options [ActionView::Helpers::FormBuilder] :form (nil) optional form builder used to create
28
+ # the textarea tag and find the error message
29
+ # @option govuk_character_count_options [Hash] :form_group see {govuk_field}
30
+ # @option govuk_character_count_options [Hash] :label see {govuk_field}
31
+ # @option govuk_character_count_options [Hash] :hint see {govuk_field}
32
+ # @option govuk_character_count_options [Hash] :textarea see {govuk_textarea}
33
+ # @option govuk_character_count_options [Hash] :character_count ({}) the options that will be used when rendering the textarea.
34
+ # See {_govuk_character_count} for more details.
35
+ #
36
+ # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK chracter count
37
+ # which can then be rendered on the page
38
+
39
+ def govuk_character_count(attribute, **govuk_character_count_options)
40
+ character_count_attribute = govuk_character_count_options[:form] ? "#{form.object_name}_#{attribute}" : attribute
41
+
42
+ _govuk_character_count(character_count_attribute, **govuk_character_count_options) do |govuk_textarea_options|
43
+ govuk_textarea(
44
+ attribute,
45
+ error_message: govuk_character_count_options[:error_message],
46
+ model: govuk_character_count_options[:model],
47
+ form: govuk_character_count_options[:form],
48
+ **govuk_textarea_options
49
+ )
50
+ end
49
51
  end
50
- end
51
52
 
52
- private
53
-
54
- # Wrapper method used by {govuk_character_count} to generate the character count HTML
55
- #
56
- # @param attribute [String, Symbol] the attribute of the character count
57
- # @param govuk_character_count_options [Hash] options that will be used in customising the HTML.
58
- # This includes everything described in {govuk_character_count}
59
- # with the addition of the +:character_count+ which are described below
60
- #
61
- # @option govuk_character_count_options [String] :maxlength (required) - if +maxwords+ is set, this is not required.
62
- # The maximum number of characters.
63
- # If +maxwords+ is provided, the +maxlength+ option will be ignored.
64
- # @option govuk_character_count_options [String] :maxwords (required) - if maxlength is set, this is not required.
65
- # The maximum number of words.
66
- # If +maxwords+ is provided, the +maxlength+ option will be ignored.
67
- # @option govuk_character_count_options [String] :threshold the percentage value of the limit at which point the count message is displayed.
68
- # If this attribute is set, the count message will be hidden by default.
69
- # @option govuk_textarea_options [Hash] :fallback_hint ({}) additional parameters that will be used to create the hint containing the character count text.
70
- # This includes all the options in {govuk_hint} plus +:count_message+.
71
- # This will replace the default text for the count message.
72
- # If you want the count number to appear, put %<count>s in the string and it will be replaced with the number
73
- #
74
- # @yield the textarea HTML generated by {govuk_character_count}
75
- #
76
- # @yieldparam govuk_textarea_options [Hash] the options used in the textarea called by {govuk_character_count}
77
- #
78
- # @return [ActiveSupport::SafeBuffer] the HTML for the chracter count which wrpas arround {govuk_character_count}
79
-
80
- def _govuk_character_count(attribute, **govuk_character_count_options)
81
- deep_init_hash(govuk_character_count_options, :textarea, :attributes, :aria)
82
- govuk_character_count_options[:textarea][:attributes][:aria][:describedby] = [govuk_character_count_options.dig(:textarea, :attributes, :aria, :describedby), "#{attribute}-info"].compact.join(' ')
83
-
84
- govuk_character_count_options[:textarea][:classes] = "#{govuk_character_count_options[:textarea][:classes]} govuk-js-character-count".lstrip
85
-
86
- tag.div(class: 'govuk-character-count', **get_character_count_attributes(**govuk_character_count_options)) do
87
- capture do
88
- concat(yield(govuk_character_count_options))
89
- concat(character_count_hint(attribute, **govuk_character_count_options))
53
+ private
54
+
55
+ # Wrapper method used by {govuk_character_count} to generate the character count HTML
56
+ #
57
+ # @param attribute [String, Symbol] the attribute of the character count
58
+ # @param govuk_character_count_options [Hash] options that will be used in customising the HTML.
59
+ # This includes everything described in {govuk_character_count}
60
+ # with the addition of the +:character_count+ which are described below
61
+ #
62
+ # @option govuk_character_count_options [String] :maxlength (required) - if +maxwords+ is set, this is not required.
63
+ # The maximum number of characters.
64
+ # If +maxwords+ is provided, the +maxlength+ option will be ignored.
65
+ # @option govuk_character_count_options [String] :maxwords (required) - if maxlength is set, this is not required.
66
+ # The maximum number of words.
67
+ # If +maxwords+ is provided, the +maxlength+ option will be ignored.
68
+ # @option govuk_character_count_options [String] :threshold the percentage value of the limit at which point the count message is displayed.
69
+ # If this attribute is set, the count message will be hidden by default.
70
+ # @option govuk_textarea_options [Hash] :fallback_hint ({}) additional parameters that will be used to create the hint containing the character count text.
71
+ # This includes all the options in {govuk_hint} plus +:count_message+.
72
+ # This will replace the default text for the count message.
73
+ # If you want the count number to appear, put %<count>s in the string and it will be replaced with the number
74
+ #
75
+ # @yield the textarea HTML generated by {govuk_character_count}
76
+ #
77
+ # @yieldparam govuk_textarea_options [Hash] the options used in the textarea called by {govuk_character_count}
78
+ #
79
+ # @return [ActiveSupport::SafeBuffer] the HTML for the chracter count which wrpas arround {govuk_character_count}
80
+
81
+ def _govuk_character_count(attribute, **govuk_character_count_options)
82
+ deep_init_hash(govuk_character_count_options, :textarea, :attributes, :aria)
83
+ govuk_character_count_options[:textarea][:attributes][:aria][:describedby] = [govuk_character_count_options.dig(:textarea, :attributes, :aria, :describedby), "#{attribute}-info"].compact.join(' ')
84
+
85
+ govuk_character_count_options[:textarea][:classes] = "#{govuk_character_count_options[:textarea][:classes]} govuk-js-character-count".lstrip
86
+
87
+ tag.div(class: 'govuk-character-count', **get_character_count_attributes(**govuk_character_count_options)) do
88
+ capture do
89
+ concat(yield(govuk_character_count_options))
90
+ concat(character_count_hint(attribute, **govuk_character_count_options))
91
+ end
90
92
  end
91
93
  end
92
- end
93
94
 
94
- # Generates the fallback hint HTML for {_govuk_character_count}
95
- #
96
- # @param (see _govuk_character_count)
97
- #
98
- # @option (see _govuk_character_count)
99
- #
100
- # @return [ActiveSupport::SafeBuffer] the HTML for the fullback hint used in {_govuk_character_count}
95
+ # Generates the fallback hint HTML for {_govuk_character_count}
96
+ #
97
+ # @param (see _govuk_character_count)
98
+ #
99
+ # @option (see _govuk_character_count)
100
+ #
101
+ # @return [ActiveSupport::SafeBuffer] the HTML for the fullback hint used in {_govuk_character_count}
101
102
 
102
- def character_count_hint(attribute, **govuk_character_count_options)
103
- fallback_hint_length = govuk_character_count_options[:character_count][:maxwords] || govuk_character_count_options[:character_count][:maxlength]
104
- fallback_hint_default = "You can enter up to %<count>s #{govuk_character_count_options[:character_count][:maxwords] ? 'words' : 'characters'}"
103
+ def character_count_hint(attribute, **govuk_character_count_options)
104
+ fallback_hint_length = govuk_character_count_options[:character_count][:maxwords] || govuk_character_count_options[:character_count][:maxlength]
105
+ fallback_hint_default = "You can enter up to %<count>s #{govuk_character_count_options[:character_count][:maxwords] ? 'words' : 'characters'}"
105
106
 
106
- deep_init_hash(govuk_character_count_options, :character_count, :fallback_hint, :attributes)
107
+ deep_init_hash(govuk_character_count_options, :character_count, :fallback_hint, :attributes)
107
108
 
108
- fallback_hint_text = format(govuk_character_count_options[:character_count][:fallback_hint][:count_message] || fallback_hint_default, count: fallback_hint_length)
109
+ fallback_hint_text = format(govuk_character_count_options[:character_count][:fallback_hint][:count_message] || fallback_hint_default, count: fallback_hint_length)
109
110
 
110
- govuk_character_count_options[:character_count][:fallback_hint][:classes] = "#{govuk_character_count_options.dig(:character_count, :fallback_hint, :classes)} govuk-character-count__message".lstrip
111
- govuk_character_count_options[:character_count][:fallback_hint][:attributes].merge!(id: "#{attribute}-info")
111
+ govuk_character_count_options[:character_count][:fallback_hint][:classes] = "#{govuk_character_count_options.dig(:character_count, :fallback_hint, :classes)} govuk-character-count__message".lstrip
112
+ govuk_character_count_options[:character_count][:fallback_hint][:attributes].merge!(id: "#{attribute}-info")
112
113
 
113
- govuk_hint(fallback_hint_text, **govuk_character_count_options[:character_count][:fallback_hint])
114
- end
115
-
116
- # Generates a hash with the character count attributes used in {_govuk_character_count}
117
- #
118
- # @param govuk_character_count_options [Hash] options that will be used in customising the HTML.
119
- # This includes everything described in {govuk_character_count}
120
- # with the addition of the +:character_count+ which are described below
121
- #
122
- # @option (see _govuk_character_count)
123
- #
124
- # @return [Hash] contains the HTMl attributes used in {_govuk_character_count}
125
-
126
- def get_character_count_attributes(**govuk_character_count_options)
127
- govuk_character_count_attributes = { data: { module: 'govuk-character-count' } }
128
-
129
- %i[maxlength threshold maxwords].each do |data_attribute|
130
- govuk_character_count_attributes[:data][data_attribute] = govuk_character_count_options[:character_count][data_attribute].to_s if govuk_character_count_options[:character_count][data_attribute]
114
+ govuk_hint(fallback_hint_text, **govuk_character_count_options[:character_count][:fallback_hint])
131
115
  end
132
116
 
133
- govuk_character_count_attributes
134
- end
135
-
136
- # Method to initialise hashes within hashes if it is not already initialised
137
- #
138
- # @param hash [Hash] the hash that is going to be initialised
139
- # @param keys [Array] the keys that are going to be initialised in the hash
140
- #
141
- # @example When the hash is completely empty
142
- # hash = { }
143
- # keys = [:a, :b, :c]
144
- #
145
- # deep_init_hash(hash, *keys)
146
- #
147
- # hash = { a: { b: { c: { } } } }
148
- #
149
- # @example When the hash has already been initialised
150
- # hash = { a: { b: { d: 'hello' } } }
151
- # keys = [:a, :b, :c]
152
- #
153
- # deep_init_hash(hash, *keys)
154
- #
155
- # hash = { a: { b: { d: 'hello', c: { } } } }
117
+ # Generates a hash with the character count attributes used in {_govuk_character_count}
118
+ #
119
+ # @param govuk_character_count_options [Hash] options that will be used in customising the HTML.
120
+ # This includes everything described in {govuk_character_count}
121
+ # with the addition of the +:character_count+ which are described below
122
+ #
123
+ # @option (see _govuk_character_count)
124
+ #
125
+ # @return [Hash] contains the HTMl attributes used in {_govuk_character_count}
126
+
127
+ def get_character_count_attributes(**govuk_character_count_options)
128
+ govuk_character_count_attributes = { data: { module: 'govuk-character-count' } }
129
+
130
+ %i[maxlength threshold maxwords].each do |data_attribute|
131
+ govuk_character_count_attributes[:data][data_attribute] = govuk_character_count_options[:character_count][data_attribute].to_s if govuk_character_count_options[:character_count][data_attribute]
132
+ end
156
133
 
157
- def deep_init_hash(hash, *keys)
158
- current_hash = hash
134
+ govuk_character_count_attributes
135
+ end
159
136
 
160
- keys.each { |key| current_hash = (current_hash[key] ||= {}) }
137
+ # Method to initialise hashes within hashes if it is not already initialised
138
+ #
139
+ # @param hash [Hash] the hash that is going to be initialised
140
+ # @param keys [Array] the keys that are going to be initialised in the hash
141
+ #
142
+ # @example When the hash is completely empty
143
+ # hash = { }
144
+ # keys = [:a, :b, :c]
145
+ #
146
+ # deep_init_hash(hash, *keys)
147
+ #
148
+ # hash = { a: { b: { c: { } } } }
149
+ #
150
+ # @example When the hash has already been initialised
151
+ # hash = { a: { b: { d: 'hello' } } }
152
+ # keys = [:a, :b, :c]
153
+ #
154
+ # deep_init_hash(hash, *keys)
155
+ #
156
+ # hash = { a: { b: { d: 'hello', c: { } } } }
157
+
158
+ def deep_init_hash(hash, *keys)
159
+ current_hash = hash
160
+
161
+ keys.each { |key| current_hash = (current_hash[key] ||= {}) }
162
+ end
161
163
  end
162
164
  end
163
165
  end