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

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +1 -1
  3. data/Gemfile.lock +5 -5
  4. data/README.md +1 -3
  5. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/accordion.rb +116 -0
  6. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/back_link.rb +34 -0
  7. data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/breadcrumbs.rb +2 -2
  8. data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/button.rb +11 -19
  9. data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/details.rb +1 -1
  10. data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/error_message.rb +1 -1
  11. data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/field/character_count.rb +25 -50
  12. data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/field/checkboxes.rb +52 -68
  13. data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/field/date_input.rb +46 -127
  14. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/field/file_upload.rb +94 -0
  15. data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/field/input.rb +34 -72
  16. data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/field/radios.rb +52 -68
  17. data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/field/select.rb +28 -64
  18. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/field/textarea.rb +105 -0
  19. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/field.rb +215 -0
  20. data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/fieldset.rb +1 -1
  21. data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/form_group.rb +10 -21
  22. data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/header.rb +1 -1
  23. data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/hint.rb +1 -1
  24. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/label.rb +88 -0
  25. data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/notification_banner.rb +1 -1
  26. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/pagination.rb +338 -0
  27. data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/step_by_step_navigation.rb +1 -1
  28. data/lib/crown_marketplace_utils/{gov_uk_helper → helpers/gov_uk_helper}/tag.rb +1 -1
  29. data/lib/crown_marketplace_utils/helpers/gov_uk_helper/warning_text.rb +52 -0
  30. data/lib/crown_marketplace_utils/{gov_uk_helper.rb → helpers/gov_uk_helper.rb} +7 -1
  31. data/lib/crown_marketplace_utils/version.rb +1 -1
  32. data/lib/crown_marketplace_utils.rb +1 -1
  33. metadata +28 -25
  34. data/lib/crown_marketplace_utils/gov_uk_helper/field/file_upload.rb +0 -125
  35. data/lib/crown_marketplace_utils/gov_uk_helper/field/textarea.rb +0 -140
  36. data/lib/crown_marketplace_utils/gov_uk_helper/field.rb +0 -305
  37. data/lib/crown_marketplace_utils/gov_uk_helper/label.rb +0 -97
  38. data/lib/crown_marketplace_utils/gov_uk_helper/pagination.rb +0 -214
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9379a4394bcdd0b8a3f1721e917bbcfc3a30ac2848bd1d49e71b06724eebe753
4
- data.tar.gz: 846cf0d310cd55fba57a1de24d91d10eef2743adc9c3c7869e466a31f647694b
3
+ metadata.gz: efd7605edd0d81a5dc2a899b1196c235a2f2265898f979855cd3e2b995e7c6c2
4
+ data.tar.gz: 1bef91763ce32d1c96e3e98494f2ea544142dfdf1eecb7141d40ebbd16fe2227
5
5
  SHA512:
6
- metadata.gz: 5056a707b0f224e7d8891b95f295cbe44c42d1141779f131f9c8228f5ff297c276b46e52a87cc7d61b8e973819b00cd78da0d0f5e24a16fb4819459399777513
7
- data.tar.gz: ef97d1119c64536641a9bbd78f3cdb486d29f0db49c175c417aaf80311417cebe29f504701996ed2766f00d3c322f6a4ffc20b49f70d12180309eac8b5d9ef94
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.4)
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,6 +48,7 @@ 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~~
@@ -71,11 +72,8 @@ At the moment this Gem only contains view helpers. It would be good in the futur
71
72
  - ~~Tag~~
72
73
 
73
74
  ### CCS Helpers to look at after first release
74
- - Accordion
75
75
  - CCS Panel
76
76
  - CCS Header
77
- - Conditioanl Checkboxes
78
- - Conditioanl Radios
79
77
 
80
78
  ## Contributing
81
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,22 +19,18 @@ 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] ||= {}
@@ -43,16 +39,15 @@ module CrownMarketplaceUtils
43
39
 
44
40
  (govuk_button_options[:attributes][:data] ||= {}).merge!({ module: 'govuk-button' })
45
41
 
46
- button_method = case type
47
- when :a
42
+ button_method = if govuk_button_options[:href]
48
43
  :govuk_button_link
49
- when :submit
44
+ elsif govuk_button_options[:form]
50
45
  :govuk_button_submit
51
46
  else
52
47
  :govuk_button_button
53
48
  end
54
49
 
55
- send(button_method, text, type, govuk_button_classes, **govuk_button_options)
50
+ send(button_method, text, govuk_button_classes, **govuk_button_options)
56
51
  end
57
52
 
58
53
  private
@@ -61,7 +56,6 @@ module CrownMarketplaceUtils
61
56
  # It is called by {#govuk_button} which will pass in the parameters, including any defaults.
62
57
  #
63
58
  # @param text [String] the text that will be shown in the button
64
- # @param _type [Symbol] unused as type has already been determined
65
59
  # @param classes [String] additional CSS classes for the button HTML
66
60
  # @param govuk_button_options [Hash] options that will be used in customising the HTML
67
61
  #
@@ -72,7 +66,7 @@ module CrownMarketplaceUtils
72
66
  # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Button as an anchor element
73
67
  # which can then be rendered on the page
74
68
 
75
- def govuk_button_link(text, _type, classes, **govuk_button_options)
69
+ def govuk_button_link(text, classes, **govuk_button_options)
76
70
  link_to(govuk_button_options[:href], class: classes, role: :button, draggable: false, **govuk_button_options[:attributes]) do
77
71
  concat(text)
78
72
  concat(govuk_start_button_icon) if govuk_button_options[:is_start_button]
@@ -83,7 +77,6 @@ module CrownMarketplaceUtils
83
77
  # It is called by {#govuk_button} which will pass in the parameters, including any defaults.
84
78
  #
85
79
  # @param text [String] the text that will be shown in the button
86
- # @param type [Symbol] the type of attribute of the button
87
80
  # @param classes [String] additional CSS classes for the button HTML
88
81
  # @param govuk_button_options [Hash] options that will be used in customising the HTML
89
82
  #
@@ -93,8 +86,8 @@ module CrownMarketplaceUtils
93
86
  # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Button as a button element
94
87
  # which can then be rendered on the page
95
88
 
96
- def govuk_button_button(text, type, classes, **govuk_button_options)
97
- 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
98
91
  concat(text)
99
92
  concat(govuk_start_button_icon) if govuk_button_options[:is_start_button]
100
93
  end
@@ -104,7 +97,6 @@ module CrownMarketplaceUtils
104
97
  # It is called by {#govuk_button} which will pass in the parameters, including any defaults.
105
98
  #
106
99
  # @param text [String] the text that will be shown in the input
107
- # @param _type [Symbol] unused as type has already been determined
108
100
  # @param classes [String] additional CSS classes for the button HTML
109
101
  # @param govuk_button_options [Hash] options that will be used in customising the HTML
110
102
  #
@@ -114,7 +106,7 @@ module CrownMarketplaceUtils
114
106
  # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Button as an input element
115
107
  # which can then be rendered on the page
116
108
 
117
- def govuk_button_submit(text, _type, classes, **govuk_button_options)
109
+ def govuk_button_submit(text, classes, **govuk_button_options)
118
110
  govuk_button_options[:form].submit(text, class: classes, **govuk_button_options[:attributes])
119
111
  end
120
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,9 +19,12 @@ 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
  #
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
25
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}
@@ -32,55 +35,27 @@ module CrownMarketplaceUtils
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 using anActiveModel.
42
- # Unlike {#govuk_character_count}, the method will be able to automatically determine if the error message needs to be shown.
43
- # It works by warpping the govuk_textarea in HTML which will trigger the JavaScript to do the character count.
44
- #
45
- # @param model [ActiveModel] model that will be used to find an error message and content of the textarea
46
- # @param attribute [String, Symbol] the attribute of the character count text area
47
- # @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
48
- #
49
- # @option (see govuk_character_count)
50
- #
51
- # @return (see govuk_character_count)
52
-
53
- def govuk_character_count_with_model(model, attribute, **govuk_character_count_options)
54
- _govuk_character_count(attribute, **govuk_character_count_options) do |govuk_text_textarea_options|
55
- govuk_textarea_with_model(model, attribute, **govuk_text_textarea_options)
56
- end
57
- end
58
-
59
- # Generates the HTML for the GOV.UK character count component using an ActionView::Helpers::FormBuilder.
60
- # Unlike {#govuk_character_count}, the method will be able to automatically determine if the error message needs to be shown.
61
- # It works by warpping the govuk_textarea in HTML which will trigger the JavaScript to do the character count.
62
- #
63
- # @param form [ActionView::Helpers::FormBuilder] the form builder used to create the character count textarea
64
- # @param attribute [String, Symbol] the attribute of the character count text area
65
- # @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
66
- #
67
- # @option (see govuk_character_count)
68
- #
69
- # @return (see govuk_character_count)
70
-
71
- def govuk_character_count_with_form(form, attribute, **govuk_character_count_options)
72
- _govuk_character_count(attribute, **govuk_character_count_options) do |govuk_text_textarea_options|
73
- 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
+ )
74
49
  end
75
50
  end
76
51
 
77
52
  private
78
53
 
79
- # 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
80
55
  #
81
56
  # @param attribute [String, Symbol] the attribute of the character count
82
57
  # @param govuk_character_count_options [Hash] options that will be used in customising the HTML.
83
- # This includes everything described in {govuk_character_count} and {govuk_character_count_with_form}
58
+ # This includes everything described in {govuk_character_count}
84
59
  # with the addition of the +:character_count+ which are described below
85
60
  #
86
61
  # @option govuk_character_count_options [String] :maxlength (required) - if +maxwords+ is set, this is not required.
@@ -91,20 +66,20 @@ module CrownMarketplaceUtils
91
66
  # If +maxwords+ is provided, the +maxlength+ option will be ignored.
92
67
  # @option govuk_character_count_options [String] :threshold the percentage value of the limit at which point the count message is displayed.
93
68
  # If this attribute is set, the count message will be hidden by default.
94
- # @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.
95
70
  # This includes all the options in {govuk_hint} plus +:count_message+.
96
71
  # This will replace the default text for the count message.
97
72
  # If you want the count number to appear, put %<count>s in the string and it will be replaced with the number
98
73
  #
99
- # @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}
100
75
  #
101
- # @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}
102
77
  #
103
- # @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}
104
79
 
105
80
  def _govuk_character_count(attribute, **govuk_character_count_options)
106
81
  deep_init_hash(govuk_character_count_options, :textarea, :attributes, :aria)
107
- govuk_character_count_options[:textarea][:attributes][:aria][:describedby] = [govuk_character_count_options.dig(:textarea, :attributes, :aria, :describedby), "#{attribute}-hint-info"].compact.join(' ')
82
+ govuk_character_count_options[:textarea][:attributes][:aria][:describedby] = [govuk_character_count_options.dig(:textarea, :attributes, :aria, :describedby), "#{attribute}-info"].compact.join(' ')
108
83
 
109
84
  govuk_character_count_options[:textarea][:classes] = "#{govuk_character_count_options[:textarea][:classes]} govuk-js-character-count".lstrip
110
85
 
@@ -133,7 +108,7 @@ module CrownMarketplaceUtils
133
108
  fallback_hint_text = format(govuk_character_count_options[:character_count][:fallback_hint][:count_message] || fallback_hint_default, count: fallback_hint_length)
134
109
 
135
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
136
- govuk_character_count_options[:character_count][:fallback_hint][:attributes].merge!(id: "#{attribute}-hint-info")
111
+ govuk_character_count_options[:character_count][:fallback_hint][:attributes].merge!(id: "#{attribute}-info")
137
112
 
138
113
  govuk_hint(fallback_hint_text, **govuk_character_count_options[:character_count][:fallback_hint])
139
114
  end
@@ -141,7 +116,7 @@ module CrownMarketplaceUtils
141
116
  # Generates a hash with the character count attributes used in {_govuk_character_count}
142
117
  #
143
118
  # @param govuk_character_count_options [Hash] options that will be used in customising the HTML.
144
- # This includes everything described in {govuk_character_count} and {govuk_character_count_with_form}
119
+ # This includes everything described in {govuk_character_count}
145
120
  # with the addition of the +:character_count+ which are described below
146
121
  #
147
122
  # @option (see _govuk_character_count)