crown_marketplace_utils 0.1.0.beta.3 → 0.1.0.beta.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +1 -1
  3. data/Gemfile.lock +5 -5
  4. data/README.md +3 -4
  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 +13 -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 +40 -68
  12. data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/field/checkboxes.rb +65 -71
  13. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/field/date_input.rb +153 -0
  14. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/field/file_upload.rb +94 -0
  15. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/field/input.rb +156 -0
  16. data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/field/radios.rb +65 -67
  17. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/field/select.rb +134 -0
  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 +2 -2
  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 +3 -3
  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} +11 -1
  31. data/lib/crown_marketplace_utils/version.rb +1 -1
  32. data/lib/crown_marketplace_utils.rb +1 -1
  33. metadata +28 -23
  34. data/lib/crown_marketplace_utils/gov_uk_helper/field/input.rb +0 -160
  35. data/lib/crown_marketplace_utils/gov_uk_helper/field/select.rb +0 -166
  36. data/lib/crown_marketplace_utils/gov_uk_helper/field/textarea.rb +0 -127
  37. data/lib/crown_marketplace_utils/gov_uk_helper/field.rb +0 -263
  38. data/lib/crown_marketplace_utils/gov_uk_helper/label.rb +0 -97
  39. data/lib/crown_marketplace_utils/gov_uk_helper/pagination.rb +0 -214
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9bded690841ee7e1cbd50580542cbebe6ec6f1522dfa1ee4ef3da56028ada558
4
- data.tar.gz: 6cddd1647d22f33db11fc6120bd2f789b908d686b0c16e5d344a7ccbf188b3c7
3
+ metadata.gz: efd7605edd0d81a5dc2a899b1196c235a2f2265898f979855cd3e2b995e7c6c2
4
+ data.tar.gz: 1bef91763ce32d1c96e3e98494f2ea544142dfdf1eecb7141d40ebbd16fe2227
5
5
  SHA512:
6
- metadata.gz: 051dee5a663d6355aba6d171307b7ea9965413e15c7926efa164c4ff651b9f25fa8946c66cbaa2f0be4491facfbb8dbbf366363d33d348deb8eaa5ae431aa8db
7
- data.tar.gz: 7d201bfd9d0a383bf3e5bf71402f428f6eda75bd88d17c669a1f1d0649ad5029d2400ed5626b26b9bf3ffffdadbf3ddeba9b6a65fdb8c09edc26cc0683952b5e
6
+ metadata.gz: cd54c38fddaf0e2e012eb3440476de012cee407787407a35e8eea925e20b30a81bb37416e33a66e4218f1752e3203a6a61dc2df31d24b91e6acde7718ce35bf3
7
+ data.tar.gz: 21de5f82595c05403b6b4c81cdef316cb4a84610604e5de45aaf69df13b17733567e7ccce0298fbb675c79dee297786e5c3fbedb5ee8cef516256951ac3c3d17
data/.rubocop.yml CHANGED
@@ -114,7 +114,7 @@ RSpec/NestedGroups:
114
114
 
115
115
  RSpec/ExampleLength:
116
116
  Exclude:
117
- - 'spec/crown_marketplace_utils/gov_uk_helper/**/*'
117
+ - 'spec/crown_marketplace_utils/helpers/gov_uk_helper/**/*'
118
118
 
119
119
  Rails/DynamicFindBy:
120
120
  Whitelist:
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- crown_marketplace_utils (0.1.0.beta.3)
4
+ crown_marketplace_utils (0.1.0.beta.5)
5
5
  actionview (>= 6.0)
6
6
 
7
7
  GEM
@@ -93,7 +93,7 @@ GEM
93
93
  activesupport (>= 5.0)
94
94
  i18n (1.12.0)
95
95
  concurrent-ruby (~> 1.0)
96
- json (2.6.2)
96
+ json (2.6.3)
97
97
  loofah (2.19.0)
98
98
  crass (~> 1.0.2)
99
99
  nokogiri (>= 1.5.9)
@@ -118,7 +118,7 @@ GEM
118
118
  nokogiri (1.13.9-x86_64-linux)
119
119
  racc (~> 1.4)
120
120
  parallel (1.22.1)
121
- parser (3.1.2.1)
121
+ parser (3.1.3.0)
122
122
  ast (~> 2.4.1)
123
123
  public_suffix (5.0.0)
124
124
  racc (1.6.0)
@@ -168,7 +168,7 @@ GEM
168
168
  diff-lcs (>= 1.2.0, < 2.0)
169
169
  rspec-support (~> 3.12.0)
170
170
  rspec-support (3.12.0)
171
- rubocop (1.39.0)
171
+ rubocop (1.40.0)
172
172
  json (~> 2.3)
173
173
  parallel (~> 1.10)
174
174
  parser (>= 3.1.2.1)
@@ -178,7 +178,7 @@ GEM
178
178
  rubocop-ast (>= 1.23.0, < 2.0)
179
179
  ruby-progressbar (~> 1.7)
180
180
  unicode-display_width (>= 1.4.0, < 3.0)
181
- rubocop-ast (1.23.0)
181
+ rubocop-ast (1.24.0)
182
182
  parser (>= 3.1.1.0)
183
183
  rubocop-rails (2.17.3)
184
184
  activesupport (>= 4.2.0)
data/README.md CHANGED
@@ -48,14 +48,16 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
48
48
  At the moment this Gem only contains view helpers. It would be good in the future to add other kinds of modules to this gem so that they can be shared and managed in one place.
49
49
 
50
50
  ### Helpers to create
51
+ - ~~Accordion~~
51
52
  - ~~Breadcrumbs~~
52
53
  - ~~Button~~
53
54
  - ~~Checkboxes~~
54
55
  - ~~CharacterCount~~
55
- - DateInput
56
+ - ~~DateInput~~
56
57
  - ~~Details~~
57
58
  - ~~ErrorMessage~~
58
59
  - ~~Fieldset~~
60
+ - ~~File Upload~~
59
61
  - ~~FormGroup~~
60
62
  - ~~Header~~
61
63
  - ~~Hint~~
@@ -70,11 +72,8 @@ At the moment this Gem only contains view helpers. It would be good in the futur
70
72
  - ~~Tag~~
71
73
 
72
74
  ### CCS Helpers to look at after first release
73
- - Accordion
74
75
  - CCS Panel
75
76
  - CCS Header
76
- - Conditioanl Checkboxes
77
- - Conditioanl Radios
78
77
 
79
78
  ## Contributing
80
79
 
@@ -0,0 +1,116 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'action_view'
4
+
5
+ module CrownMarketplaceUtils::Helpers
6
+ module GovUkHelper
7
+ # = GOV.UK Accordion
8
+ #
9
+ # This helper is used for generating the accordion component from the
10
+ # {https://design-system.service.gov.uk/accordion/back-link GDS - Components - Accordion}
11
+
12
+ module Accordion
13
+ include ActionView::Context
14
+ include ActionView::Helpers::TagHelper
15
+ include ActionView::Helpers::TextHelper
16
+
17
+ # Generates the HTML for the GOV.UK accordion component
18
+ #
19
+ # @param accordion_id [String] used as an id in the HTML for the accordion as a whole,
20
+ # and also as a prefix for the ids of the section contents
21
+ # and the buttons that open them
22
+ # @param accordion_items [Array<Hash>] an array of accordion items.
23
+ # See {#govuk_accordion_section} for details of the items in the array.
24
+ # @param govuk_accordion_options [Hash] options that will be used in customising the HTML
25
+ #
26
+ # @option govuk_accordion_options [String] :classes additional CSS classes for the accordion HTML
27
+ # @option govuk_accordion_options [Integer] :heading_level (2) heading level, from 1 to 6
28
+ # @option govuk_accordion_options [Hash] :attributes ({ data: { module: 'govuk-accordion' } }) any additional
29
+ # attributes that will added as part of the HTML
30
+ #
31
+ # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Accordion
32
+ # which can then be rendered on the page
33
+
34
+ def govuk_accordion(accordion_id, accordion_items, **govuk_accordion_options)
35
+ (govuk_accordion_options[:attributes] ||= {})[:data] ||= {}
36
+ govuk_accordion_options[:attributes][:class] = "govuk-accordion #{govuk_accordion_options[:classes]}".rstrip
37
+ govuk_accordion_options[:attributes][:id] = accordion_id
38
+ govuk_accordion_options[:attributes][:data][:module] = 'govuk-accordion'
39
+ govuk_accordion_options[:heading_level] ||= 2
40
+
41
+ tag.div(**govuk_accordion_options[:attributes]) do
42
+ capture do
43
+ accordion_items.each.with_index(1) { |accordion_item, index| concat(govuk_accordion_section(accordion_id, accordion_item, index, govuk_accordion_options[:heading_level])) }
44
+ end
45
+ end
46
+ end
47
+
48
+ private
49
+
50
+ # Generates the HTML for an accordion section, used by {govuk_accordion}
51
+ #
52
+ # @param accordion_id [String] used as an id in the HTML for the accordion
53
+ # @param index [Integer] the index of the accordion item
54
+ # @param heading_level [Integer] heading level, from 1 to 6
55
+ #
56
+ # @option accordion_item [Boolean] :expanded sets whether the section should be expanded
57
+ # when the page loads for the first time.
58
+ # @option accordion_item [String] :heading_level (2) Heading level, from 1 to 6
59
+ # @option accordion_item [String] :heading the heading text for the accordion
60
+ # @option accordion_item [String] :summary (nil) optional summary text for the accordion header
61
+ # @option accordion_item [String, ActiveSupport::SafeBuffer] the content within the accordion section
62
+ #
63
+ # @return [ActiveSupport::SafeBuffer] the HTML for an accordion section
64
+ # which is used in {govuk_accordion}
65
+
66
+ def govuk_accordion_section(accordion_id, accordion_item, index, heading_level)
67
+ tag.div(class: "govuk-accordion__section #{'govuk-accordion__section--expanded' if accordion_item[:expanded]}".rstrip) do
68
+ capture do
69
+ concat(govuk_accordion_section_header(accordion_id, accordion_item, index, heading_level))
70
+ concat(govuk_accordion_section_content(accordion_id, accordion_item, index))
71
+ end
72
+ end
73
+ end
74
+
75
+ # Generates the HTML for an accordion section heading, used by {govuk_accordion_section}
76
+ #
77
+ # @param (see govuk_accordion_section)
78
+ #
79
+ # @option (see govuk_accordion_section)
80
+ #
81
+ # @return [ActiveSupport::SafeBuffer] the HTML for an accordion section heading
82
+ # which is used in {govuk_accordion_section}
83
+
84
+ def govuk_accordion_section_header(accordion_id, accordion_item, index, heading_level)
85
+ tag.div(class: 'govuk-accordion__section-header') do
86
+ capture do
87
+ concat(tag.send("h#{heading_level}", class: 'govuk-accordion__section-heading') do
88
+ tag.span(accordion_item[:heading], class: 'govuk-accordion__section-button', id: "#{accordion_id}-heading-#{index}")
89
+ end)
90
+ concat(tag.div(accordion_item[:summary], class: 'govuk-accordion__section-summary govuk-body', id: "#{accordion_id}-summary-#{index}")) if accordion_item[:summary]
91
+ end
92
+ end
93
+ end
94
+
95
+ # Generates the HTML for an accordion sections content, used by {govuk_accordion_section}
96
+ #
97
+ # @param accordion_id [String] used as an id in the HTML for the accordion
98
+ # @param index [Integer] the index of the accordion item
99
+ #
100
+ # @option (see govuk_accordion_section)
101
+ #
102
+ # @return [ActiveSupport::SafeBuffer] the HTML for an accordion sections content
103
+ # which is used in {govuk_accordion_section}
104
+
105
+ def govuk_accordion_section_content(accordion_id, accordion_item, index)
106
+ tag.div(class: 'govuk-accordion__section-content', id: "#{accordion_id}-content-#{index}", aria: { labelledby: "#{accordion_id}-heading-#{index}" }) do
107
+ if accordion_item[:content].is_a? ActiveSupport::SafeBuffer
108
+ accordion_item[:content]
109
+ else
110
+ tag.p(accordion_item[:content], class: 'govuk-body')
111
+ end
112
+ end
113
+ end
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'action_view'
4
+
5
+ module CrownMarketplaceUtils::Helpers
6
+ module GovUkHelper
7
+ # = GOV.UK Back Link
8
+ #
9
+ # This helper is used for generating the back link component from the
10
+ # {https://design-system.service.gov.uk/components/back-link GDS - Components - Back link}
11
+
12
+ module BackLink
13
+ include ActionView::Helpers::UrlHelper
14
+
15
+ # Generates the HTML for the GOV.UK Back link component
16
+ #
17
+ # @param text [String] the text for the back link
18
+ # @param href [String] the href for the back link
19
+ # @param govuk_back_link_options [Hash] options that will be used in customising the HTML
20
+ #
21
+ # @option govuk_back_link_options [String] :classes additional CSS classes for the tag HTML
22
+ # @option govuk_back_link_options [Hash] :attributes ({}) any additional attributes that will added as part of the HTML
23
+ #
24
+ # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Back link
25
+ # which can then be rendered on the page
26
+
27
+ def govuk_back_link(text, href, **govuk_back_link_options)
28
+ govuk_back_link_options[:class] = "govuk-back-link #{govuk_back_link_options[:classes]}".rstrip
29
+
30
+ link_to(text, href, class: govuk_back_link_options[:class], **(govuk_back_link_options[:attributes] || {}))
31
+ end
32
+ end
33
+ end
34
+ end
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'action_view'
4
4
 
5
- module CrownMarketplaceUtils
5
+ module CrownMarketplaceUtils::Helpers
6
6
  module GovUkHelper
7
7
  # = GOV.UK Breadcrumbs
8
8
  #
@@ -20,7 +20,7 @@ module CrownMarketplaceUtils
20
20
  # @param govuk_breadcrumb_items [Array<Hash>] An array of links for the breadcrumbs list. See {#govuk_breadcrumb_link} for details of the items in the array.
21
21
  # @param govuk_breadcrumbs_options [Hash] options that will be used in customising the HTML
22
22
  #
23
- # @option govuk_breadcrumbs_options [String] :classes additional CSS classes for the breadcrums HTML
23
+ # @option govuk_breadcrumbs_options [String] :classes additional CSS classes for the breadcrumbs HTML
24
24
  # @option govuk_breadcrumbs_options [Boolean] :collapse_on_mobile indicates if it is to colapse breadcrumbs on mobile
25
25
  # @option govuk_breadcrumbs_options [Hash] :attributes any additional attributes that will added as part of the HTML
26
26
  #
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'action_view'
4
4
 
5
- module CrownMarketplaceUtils
5
+ module CrownMarketplaceUtils::Helpers
6
6
  module GovUkHelper
7
7
  # = GOV.UK Button
8
8
  #
@@ -19,38 +19,35 @@ module CrownMarketplaceUtils
19
19
  # Generates the HTML for the GOV.UK button component
20
20
  #
21
21
  # @param text [String] the text that will be shown in the button
22
- # @param type [Symbol] the type of button that will be created.
23
- # - :a will create a anchor tag
24
- # - :submit will create a input with type submit
25
- # - all other values will create a button with the type specified
26
22
  # @param govuk_button_options [Hash] options that will be used in customising the HTML
27
23
  #
28
24
  # @option govuk_button_options [String] :classes additional CSS classes for the button HTML
29
25
  # @option govuk_button_options [Boolean] :is_start_button indicates if it is a start button
30
- # @option govuk_button_options [String] :href the URI that will be used in anchor tag (only used if type is +:a+)
31
- # @option govuk_button_options [ActionView::Helpers::FormBuilder] :form the form builder used to create the submit button (only used if type is +:submit+)
26
+ # @option govuk_button_options [String] :href the URI that will be used in anchor tag
27
+ # @option govuk_button_options [ActionView::Helpers::FormBuilder] :form the form builder used to create the submit button
32
28
  # @option govuk_button_options [Hash] :attributes ({ data: { module: 'govuk-button' } }) any additional attributes that will added as part of the HTML
33
29
  #
34
30
  # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Button
35
31
  # which can then be rendered on the page
36
32
 
37
- def govuk_button(text, type = :a, **govuk_button_options)
33
+ def govuk_button(text, **govuk_button_options)
38
34
  govuk_button_classes = ['govuk-button']
39
35
  govuk_button_classes << govuk_button_options[:classes]
40
36
  govuk_button_options[:attributes] ||= {}
41
37
  govuk_button_classes << 'govuk-button--disabled' if govuk_button_options[:attributes][:disabled]
38
+ govuk_button_classes << 'govuk-button--start' if govuk_button_options[:is_start_button]
39
+
42
40
  (govuk_button_options[:attributes][:data] ||= {}).merge!({ module: 'govuk-button' })
43
41
 
44
- button_method = case type
45
- when :a
42
+ button_method = if govuk_button_options[:href]
46
43
  :govuk_button_link
47
- when :submit
44
+ elsif govuk_button_options[:form]
48
45
  :govuk_button_submit
49
46
  else
50
47
  :govuk_button_button
51
48
  end
52
49
 
53
- send(button_method, text, type, govuk_button_classes, **govuk_button_options)
50
+ send(button_method, text, govuk_button_classes, **govuk_button_options)
54
51
  end
55
52
 
56
53
  private
@@ -59,7 +56,6 @@ module CrownMarketplaceUtils
59
56
  # It is called by {#govuk_button} which will pass in the parameters, including any defaults.
60
57
  #
61
58
  # @param text [String] the text that will be shown in the button
62
- # @param _type [Symbol] unused as type has already been determined
63
59
  # @param classes [String] additional CSS classes for the button HTML
64
60
  # @param govuk_button_options [Hash] options that will be used in customising the HTML
65
61
  #
@@ -70,7 +66,7 @@ module CrownMarketplaceUtils
70
66
  # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Button as an anchor element
71
67
  # which can then be rendered on the page
72
68
 
73
- def govuk_button_link(text, _type, classes, **govuk_button_options)
69
+ def govuk_button_link(text, classes, **govuk_button_options)
74
70
  link_to(govuk_button_options[:href], class: classes, role: :button, draggable: false, **govuk_button_options[:attributes]) do
75
71
  concat(text)
76
72
  concat(govuk_start_button_icon) if govuk_button_options[:is_start_button]
@@ -81,7 +77,6 @@ module CrownMarketplaceUtils
81
77
  # It is called by {#govuk_button} which will pass in the parameters, including any defaults.
82
78
  #
83
79
  # @param text [String] the text that will be shown in the button
84
- # @param type [Symbol] the type of attribute of the button
85
80
  # @param classes [String] additional CSS classes for the button HTML
86
81
  # @param govuk_button_options [Hash] options that will be used in customising the HTML
87
82
  #
@@ -91,8 +86,8 @@ module CrownMarketplaceUtils
91
86
  # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Button as a button element
92
87
  # which can then be rendered on the page
93
88
 
94
- def govuk_button_button(text, type, classes, **govuk_button_options)
95
- button_tag(class: classes, type: type, **govuk_button_options[:attributes]) do
89
+ def govuk_button_button(text, classes, **govuk_button_options)
90
+ button_tag(class: classes, **govuk_button_options[:attributes]) do
96
91
  concat(text)
97
92
  concat(govuk_start_button_icon) if govuk_button_options[:is_start_button]
98
93
  end
@@ -102,7 +97,6 @@ module CrownMarketplaceUtils
102
97
  # It is called by {#govuk_button} which will pass in the parameters, including any defaults.
103
98
  #
104
99
  # @param text [String] the text that will be shown in the input
105
- # @param _type [Symbol] unused as type has already been determined
106
100
  # @param classes [String] additional CSS classes for the button HTML
107
101
  # @param govuk_button_options [Hash] options that will be used in customising the HTML
108
102
  #
@@ -112,7 +106,7 @@ module CrownMarketplaceUtils
112
106
  # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Button as an input element
113
107
  # which can then be rendered on the page
114
108
 
115
- def govuk_button_submit(text, _type, classes, **govuk_button_options)
109
+ def govuk_button_submit(text, classes, **govuk_button_options)
116
110
  govuk_button_options[:form].submit(text, class: classes, **govuk_button_options[:attributes])
117
111
  end
118
112
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'action_view'
4
4
 
5
- module CrownMarketplaceUtils
5
+ module CrownMarketplaceUtils::Helpers
6
6
  module GovUkHelper
7
7
  # = GOV.UK Details
8
8
  #
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'action_view'
4
4
 
5
- module CrownMarketplaceUtils
5
+ module CrownMarketplaceUtils::Helpers
6
6
  module GovUkHelper
7
7
  # = GOV.UK Error Message
8
8
  #
@@ -2,7 +2,7 @@
2
2
 
3
3
  require_relative 'textarea'
4
4
 
5
- module CrownMarketplaceUtils
5
+ module CrownMarketplaceUtils::Helpers
6
6
  module GovUkHelper
7
7
  module Field
8
8
  # = GOV.UK Character count
@@ -19,72 +19,44 @@ module CrownMarketplaceUtils
19
19
  # It works by warpping the govuk_textarea in HTML which will trigger the JavaScript to do the character count.
20
20
  #
21
21
  # @param attribute [String, Symbol] the attribute of the character count text area
22
- # @param error_message [String] the error message to be displayed
23
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
24
23
  #
25
- # @option govuk_character_count_options [Hash] :form_group_options see {govuk_field}
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}
26
29
  # @option govuk_character_count_options [Hash] :label see {govuk_field}
27
30
  # @option govuk_character_count_options [Hash] :hint see {govuk_field}
28
- # @option govuk_character_count_options [Hash] :textarea_options see {govuk_textarea}
29
- # @option govuk_character_count_options [Hash] :character_count_options ({}) the options that will be used when rendering the textarea.
30
- # See {_govuk_character_count} for more details.
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.
31
34
  #
32
35
  # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK chracter count
33
36
  # which can then be rendered on the page
34
37
 
35
- def govuk_character_count(attribute, error_message = nil, **govuk_character_count_options)
36
- _govuk_character_count(attribute, **govuk_character_count_options) do |govuk_text_textarea_options|
37
- govuk_textarea(attribute, error_message, **govuk_text_textarea_options)
38
- end
39
- end
40
-
41
- # Generates the HTML for the GOV.UK character count component.
42
- # It works by warpping the govuk_textarea in HTML which will trigger the JavaScript to do the character count.
43
- #
44
- # @param attribute [String, Symbol] the attribute of the character count
45
- # @param error_message [String] the error message to be displayed
46
- # @param govuk_character_count_options [Hash] options that will be used for the parts of the form group, label, hint and textarea
47
- #
48
- # @option govuk_character_count_options [Hash] :form_group_options see {govuk_field}
49
- # @option govuk_character_count_options [Hash] :label see {govuk_field}
50
- # @option govuk_character_count_options [Hash] :hint see {govuk_field}
51
- # @option govuk_character_count_options [Hash] :textarea_options see {govuk_textarea}
52
- # @option govuk_character_count_options [Hash] :character_count_options ({}) the options that will be used when rendering the textarea.
53
- # See {_govuk_character_count} for more details.
54
- #
55
- # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK chracter count
56
- # which can then be rendered on the page
57
-
58
- # Generates the HTML for the GOV.UK textarea component using an ActionView::Helpers::FormBuilder.
59
- # Unlike {#govuk_textarea}, the method will be able to automatically determine if the error message needs to be shown.
60
- #
61
- # @param form [ActionView::Helpers::FormBuilder] :form the form builder used to create the character count textarea
62
- # @param attribute [String, Symbol] the attribute of the character count text area
63
- # @param govuk_character_count_options [Hash] options that will be used for the parts of the form group, label, hint, textarea and the character count
64
- #
65
- # @option govuk_character_count_options [Hash] :form_group_options see {govuk_field_with_form}
66
- # @option govuk_character_count_options [Hash] :label see {govuk_field_with_form}
67
- # @option govuk_character_count_options [Hash] :hint see {govuk_field_with_form}
68
- # @option govuk_character_count_options [Hash] :textarea_options see {govuk_textarea_with_form}
69
- # @option govuk_character_count_options [Hash] :character_count_options ({}) the options that will be used when rendering the textarea.
70
- # See {_govuk_character_count} for more details.
71
- #
72
- # @return (see govuk_character_count)
73
-
74
- def govuk_character_count_with_form(form, attribute, **govuk_character_count_options)
75
- _govuk_character_count(attribute, **govuk_character_count_options) do |govuk_text_textarea_options|
76
- govuk_textarea_with_form(form, attribute, **govuk_text_textarea_options)
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
+ )
77
49
  end
78
50
  end
79
51
 
80
52
  private
81
53
 
82
- # Wrapper method used by {govuk_character_count} and {govuk_character_count_with_form} to generate the character count HTML
54
+ # Wrapper method used by {govuk_character_count} to generate the character count HTML
83
55
  #
84
56
  # @param attribute [String, Symbol] the attribute of the character count
85
57
  # @param govuk_character_count_options [Hash] options that will be used in customising the HTML.
86
- # This includes everything described in {govuk_character_count} and {govuk_character_count_with_form}
87
- # with the addition of the +:character_count_options+ which are described below
58
+ # This includes everything described in {govuk_character_count}
59
+ # with the addition of the +:character_count+ which are described below
88
60
  #
89
61
  # @option govuk_character_count_options [String] :maxlength (required) - if +maxwords+ is set, this is not required.
90
62
  # The maximum number of characters.
@@ -94,22 +66,22 @@ module CrownMarketplaceUtils
94
66
  # If +maxwords+ is provided, the +maxlength+ option will be ignored.
95
67
  # @option govuk_character_count_options [String] :threshold the percentage value of the limit at which point the count message is displayed.
96
68
  # If this attribute is set, the count message will be hidden by default.
97
- # @option govuk_text_textarea_options [Hash] :fallback_hint ({}) additional parameters that will be used to create the hint containing the character count text.
69
+ # @option govuk_textarea_options [Hash] :fallback_hint ({}) additional parameters that will be used to create the hint containing the character count text.
98
70
  # This includes all the options in {govuk_hint} plus +:count_message+.
99
71
  # This will replace the default text for the count message.
100
72
  # If you want the count number to appear, put %<count>s in the string and it will be replaced with the number
101
73
  #
102
- # @yield the textarea HTML generated by {govuk_character_count} or {govuk_character_count_with_form}
74
+ # @yield the textarea HTML generated by {govuk_character_count}
103
75
  #
104
- # @yieldparam govuk_text_textarea_options [Hash] the options used in the textarea called by {govuk_character_count} or {govuk_character_count_with_form}
76
+ # @yieldparam govuk_textarea_options [Hash] the options used in the textarea called by {govuk_character_count}
105
77
  #
106
- # @return [ActiveSupport::SafeBuffer] the HTML for the chracter count which wrpas arround {govuk_character_count} or {govuk_character_count_with_form}
78
+ # @return [ActiveSupport::SafeBuffer] the HTML for the chracter count which wrpas arround {govuk_character_count}
107
79
 
108
80
  def _govuk_character_count(attribute, **govuk_character_count_options)
109
- deep_init_hash(govuk_character_count_options, :textarea_options, :attributes, :aria)
110
- govuk_character_count_options[:textarea_options][:attributes][:aria][:describedby] = [govuk_character_count_options.dig(:textarea_options, :attributes, :aria, :describedby), "#{attribute}-hint-info"].compact.join(' ')
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(' ')
111
83
 
112
- govuk_character_count_options[:textarea_options][:classes] = [govuk_character_count_options[:textarea_options][:classes], 'govuk-js-character-count'].compact
84
+ govuk_character_count_options[:textarea][:classes] = "#{govuk_character_count_options[:textarea][:classes]} govuk-js-character-count".lstrip
113
85
 
114
86
  tag.div(class: 'govuk-character-count', **get_character_count_attributes(**govuk_character_count_options)) do
115
87
  capture do
@@ -128,24 +100,24 @@ module CrownMarketplaceUtils
128
100
  # @return [ActiveSupport::SafeBuffer] the HTML for the fullback hint used in {_govuk_character_count}
129
101
 
130
102
  def character_count_hint(attribute, **govuk_character_count_options)
131
- fallback_hint_length = govuk_character_count_options[:character_count_options][:maxwords] || govuk_character_count_options[:character_count_options][:maxlength]
132
- fallback_hint_default = "You can enter up to %<count>s #{govuk_character_count_options[:character_count_options][:maxwords] ? 'words' : 'characters'}"
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'}"
133
105
 
134
- deep_init_hash(govuk_character_count_options, :character_count_options, :fallback_hint, :attributes)
106
+ deep_init_hash(govuk_character_count_options, :character_count, :fallback_hint, :attributes)
135
107
 
136
- fallback_hint_text = format(govuk_character_count_options[:character_count_options][:fallback_hint][:count_message] || fallback_hint_default, count: fallback_hint_length)
108
+ fallback_hint_text = format(govuk_character_count_options[:character_count][:fallback_hint][:count_message] || fallback_hint_default, count: fallback_hint_length)
137
109
 
138
- govuk_character_count_options[:character_count_options][:fallback_hint][:classes] = [govuk_character_count_options.dig(:character_count_options, :fallback_hint, :classes), 'govuk-character-count__message']
139
- govuk_character_count_options[:character_count_options][:fallback_hint][:attributes].merge!(id: "#{attribute}-hint-info")
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")
140
112
 
141
- govuk_hint(fallback_hint_text, **govuk_character_count_options[:character_count_options][:fallback_hint])
113
+ govuk_hint(fallback_hint_text, **govuk_character_count_options[:character_count][:fallback_hint])
142
114
  end
143
115
 
144
116
  # Generates a hash with the character count attributes used in {_govuk_character_count}
145
117
  #
146
118
  # @param govuk_character_count_options [Hash] options that will be used in customising the HTML.
147
- # This includes everything described in {govuk_character_count} and {govuk_character_count_with_form}
148
- # with the addition of the +:character_count_options+ which are described below
119
+ # This includes everything described in {govuk_character_count}
120
+ # with the addition of the +:character_count+ which are described below
149
121
  #
150
122
  # @option (see _govuk_character_count)
151
123
  #
@@ -155,7 +127,7 @@ module CrownMarketplaceUtils
155
127
  govuk_character_count_attributes = { data: { module: 'govuk-character-count' } }
156
128
 
157
129
  %i[maxlength threshold maxwords].each do |data_attribute|
158
- govuk_character_count_attributes[:data][data_attribute] = govuk_character_count_options[:character_count_options][data_attribute].to_s if govuk_character_count_options[:character_count_options][data_attribute]
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]
159
131
  end
160
132
 
161
133
  govuk_character_count_attributes