govuk_design_system_formbuilder 1.2.3 → 2.0.0b1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -2
  3. data/lib/govuk_design_system_formbuilder.rb +1 -1
  4. data/lib/govuk_design_system_formbuilder/base.rb +1 -1
  5. data/lib/govuk_design_system_formbuilder/builder.rb +155 -60
  6. data/lib/govuk_design_system_formbuilder/containers/character_count.rb +8 -7
  7. data/lib/govuk_design_system_formbuilder/containers/check_boxes.rb +18 -10
  8. data/lib/govuk_design_system_formbuilder/containers/check_boxes_fieldset.rb +13 -4
  9. data/lib/govuk_design_system_formbuilder/containers/fieldset.rb +15 -55
  10. data/lib/govuk_design_system_formbuilder/containers/form_group.rb +19 -10
  11. data/lib/govuk_design_system_formbuilder/containers/radio_buttons_fieldset.rb +13 -4
  12. data/lib/govuk_design_system_formbuilder/containers/radios.rb +22 -11
  13. data/lib/govuk_design_system_formbuilder/containers/supplemental.rb +3 -3
  14. data/lib/govuk_design_system_formbuilder/elements/caption.rb +6 -4
  15. data/lib/govuk_design_system_formbuilder/elements/check_boxes/collection.rb +21 -12
  16. data/lib/govuk_design_system_formbuilder/elements/check_boxes/collection_check_box.rb +9 -7
  17. data/lib/govuk_design_system_formbuilder/elements/check_boxes/fieldset_check_box.rb +24 -16
  18. data/lib/govuk_design_system_formbuilder/elements/date.rb +47 -40
  19. data/lib/govuk_design_system_formbuilder/elements/error_message.rb +4 -4
  20. data/lib/govuk_design_system_formbuilder/elements/error_summary.rb +15 -15
  21. data/lib/govuk_design_system_formbuilder/elements/file.rb +8 -7
  22. data/lib/govuk_design_system_formbuilder/elements/hint.rb +43 -16
  23. data/lib/govuk_design_system_formbuilder/elements/inputs/email.rb +2 -2
  24. data/lib/govuk_design_system_formbuilder/elements/inputs/number.rb +0 -2
  25. data/lib/govuk_design_system_formbuilder/elements/inputs/password.rb +0 -2
  26. data/lib/govuk_design_system_formbuilder/elements/inputs/phone.rb +0 -2
  27. data/lib/govuk_design_system_formbuilder/elements/inputs/text.rb +0 -2
  28. data/lib/govuk_design_system_formbuilder/elements/inputs/url.rb +0 -2
  29. data/lib/govuk_design_system_formbuilder/elements/label.rb +35 -27
  30. data/lib/govuk_design_system_formbuilder/elements/legend.rb +79 -0
  31. data/lib/govuk_design_system_formbuilder/elements/radios/collection.rb +23 -14
  32. data/lib/govuk_design_system_formbuilder/elements/radios/collection_radio_button.rb +24 -8
  33. data/lib/govuk_design_system_formbuilder/elements/radios/fieldset_radio_button.rb +19 -7
  34. data/lib/govuk_design_system_formbuilder/elements/select.rb +24 -19
  35. data/lib/govuk_design_system_formbuilder/elements/submit.rb +11 -6
  36. data/lib/govuk_design_system_formbuilder/elements/text_area.rb +31 -25
  37. data/lib/govuk_design_system_formbuilder/traits/collection_item.rb +1 -1
  38. data/lib/govuk_design_system_formbuilder/traits/conditional.rb +1 -1
  39. data/lib/govuk_design_system_formbuilder/traits/hint.rb +15 -2
  40. data/lib/govuk_design_system_formbuilder/traits/input.rb +9 -8
  41. data/lib/govuk_design_system_formbuilder/traits/label.rb +2 -2
  42. data/lib/govuk_design_system_formbuilder/version.rb +1 -1
  43. metadata +11 -10
@@ -24,17 +24,18 @@ module GOVUKDesignSystemFormBuilder
24
24
  private
25
25
 
26
26
  def submit
27
- @builder.submit(@text, class: submit_classes, **submit_options)
27
+ @builder.submit(@text, class: classes, **options)
28
28
  end
29
29
 
30
- def submit_classes
30
+ def classes
31
31
  %w(button)
32
32
  .prefix(brand)
33
- .push(warning_class, secondary_class, disabled_class, @classes, padding_class(@block_content.present?))
33
+ .push(warning_class, secondary_class, disabled_class, padding_class, custom_classes)
34
+ .flatten
34
35
  .compact
35
36
  end
36
37
 
37
- def submit_options
38
+ def options
38
39
  {
39
40
  formnovalidate: !@validate,
40
41
  disabled: @disabled,
@@ -53,13 +54,17 @@ module GOVUKDesignSystemFormBuilder
53
54
  %(#{brand}-button--secondary) if @secondary
54
55
  end
55
56
 
56
- def padding_class(content_present)
57
- %(#{brand}-!-margin-right-1) if content_present
57
+ def padding_class
58
+ %(#{brand}-!-margin-right-1) if @block_content
58
59
  end
59
60
 
60
61
  def disabled_class
61
62
  %(#{brand}-button--disabled) if @disabled
62
63
  end
64
+
65
+ def custom_classes
66
+ Array.wrap(@classes)
67
+ end
63
68
  end
64
69
  end
65
70
  end
@@ -8,22 +8,23 @@ module GOVUKDesignSystemFormBuilder
8
8
  include Traits::Label
9
9
  include Traits::Supplemental
10
10
 
11
- def initialize(builder, object_name, attribute_name, hint_text:, label:, caption:, rows:, max_words:, max_chars:, threshold:, **kwargs, &block)
11
+ def initialize(builder, object_name, attribute_name, hint:, label:, caption:, rows:, max_words:, max_chars:, threshold:, form_group:, **kwargs, &block)
12
12
  super(builder, object_name, attribute_name, &block)
13
13
 
14
- @label = label
15
- @caption = caption
16
- @hint_text = hint_text
17
- @attributes = kwargs
18
- @max_words = max_words
19
- @max_chars = max_chars
20
- @threshold = threshold
21
- @rows = rows
14
+ @label = label
15
+ @caption = caption
16
+ @hint = hint
17
+ @max_words = max_words
18
+ @max_chars = max_chars
19
+ @threshold = threshold
20
+ @rows = rows
21
+ @form_group = form_group
22
+ @html_attributes = kwargs
22
23
  end
23
24
 
24
25
  def html
25
- Containers::CharacterCount.new(@builder, max_words: @max_words, max_chars: @max_chars, threshold: @threshold).html do
26
- Containers::FormGroup.new(@builder, @object_name, @attribute_name).html do
26
+ Containers::CharacterCount.new(@builder, **character_count_options).html do
27
+ Containers::FormGroup.new(@builder, @object_name, @attribute_name, **@form_group).html do
27
28
  safe_join([label_element, supplemental_content, hint_element, error_element, text_area, limit_description])
28
29
  end
29
30
  end
@@ -31,21 +32,26 @@ module GOVUKDesignSystemFormBuilder
31
32
 
32
33
  private
33
34
 
35
+ def character_count_options
36
+ { max_words: @max_words, max_chars: @max_chars, threshold: @threshold }
37
+ end
38
+
34
39
  def text_area
35
- @builder.text_area(@attribute_name, **text_area_options, **@attributes.merge(rows: @rows))
40
+ @builder.text_area(@attribute_name, **options, **@html_attributes)
36
41
  end
37
42
 
38
- def text_area_classes
43
+ def classes
39
44
  %w(textarea).prefix(brand).tap do |classes|
40
- classes.push(%(#{brand}-textarea--error)) if has_errors?
45
+ classes.push(%(#{brand}-textarea--error)) if has_errors?
41
46
  classes.push(%(#{brand}-js-character-count)) if limit?
42
47
  end
43
48
  end
44
49
 
45
- def text_area_options
50
+ def options
46
51
  {
47
52
  id: field_id(link_errors: true),
48
- class: text_area_classes,
53
+ class: classes,
54
+ rows: @rows,
49
55
  aria: { describedby: described_by(hint_id, error_id, supplemental_id, limit_description_id) },
50
56
  }
51
57
  end
@@ -59,13 +65,21 @@ module GOVUKDesignSystemFormBuilder
59
65
  end
60
66
 
61
67
  def limit?
68
+ limit_quantity.present?
69
+ end
70
+
71
+ def limit_quantity
62
72
  @max_words || @max_chars
63
73
  end
64
74
 
75
+ def limit_type
76
+ @max_words.present? ? 'words' : 'characters'
77
+ end
78
+
65
79
  def limit_description
66
80
  return nil unless limit?
67
81
 
68
- content_tag('span', id: limit_id, class: limit_description_classes, aria: { live: 'polite' }) do
82
+ tag.span(id: limit_id, class: limit_description_classes, aria: { live: 'polite' }) do
69
83
  "You can enter up to #{limit_quantity} #{limit_type}"
70
84
  end
71
85
  end
@@ -74,14 +88,6 @@ module GOVUKDesignSystemFormBuilder
74
88
  %w(hint character-count__message).prefix(brand)
75
89
  end
76
90
 
77
- def limit_quantity
78
- @max_words || @max_chars
79
- end
80
-
81
- def limit_type
82
- @max_words.present? ? 'words' : 'characters'
83
- end
84
-
85
91
  def limit_description_id
86
92
  return nil unless limit?
87
93
 
@@ -8,7 +8,7 @@ module GOVUKDesignSystemFormBuilder
8
8
  when Symbol, String
9
9
  item.send(method)
10
10
  when Proc
11
- method.call(item)
11
+ capture { method.call(item) }
12
12
  end
13
13
  end
14
14
  end
@@ -8,7 +8,7 @@ module GOVUKDesignSystemFormBuilder
8
8
  end
9
9
 
10
10
  def wrap_conditional(block)
11
- content_tag('div', class: conditional_classes, id: conditional_id) do
11
+ tag.div(class: conditional_classes, id: conditional_id) do
12
12
  capture { block.call }
13
13
  end
14
14
  end
@@ -2,7 +2,7 @@ module GOVUKDesignSystemFormBuilder
2
2
  module Traits
3
3
  module Hint
4
4
  def hint_id
5
- return nil if @hint_text.blank?
5
+ return nil unless hint_element.active?
6
6
 
7
7
  build_id('hint')
8
8
  end
@@ -10,7 +10,20 @@ module GOVUKDesignSystemFormBuilder
10
10
  private
11
11
 
12
12
  def hint_element
13
- @hint_element ||= Elements::Hint.new(@builder, @object_name, @attribute_name, @hint_text)
13
+ @hint_element ||= Elements::Hint.new(@builder, @object_name, @attribute_name, **hint_content)
14
+ end
15
+
16
+ def hint_content
17
+ case @hint
18
+ when NilClass
19
+ {}
20
+ when Hash
21
+ @hint
22
+ when Proc
23
+ { content: @hint }
24
+ else
25
+ fail(ArgumentError, %(hint must be a Proc or Hash))
26
+ end
14
27
  end
15
28
  end
16
29
  end
@@ -1,18 +1,19 @@
1
1
  module GOVUKDesignSystemFormBuilder
2
2
  module Traits
3
3
  module Input
4
- def initialize(builder, object_name, attribute_name, hint_text:, label:, caption:, width:, **kwargs, &block)
4
+ def initialize(builder, object_name, attribute_name, hint:, label:, caption:, width:, form_group:, **kwargs, &block)
5
5
  super(builder, object_name, attribute_name, &block)
6
6
 
7
- @width = width
8
- @attributes = kwargs
9
- @label = label
10
- @caption = caption
11
- @hint_text = hint_text
7
+ @width = width
8
+ @label = label
9
+ @caption = caption
10
+ @hint = hint
11
+ @html_attributes = kwargs
12
+ @form_group = form_group
12
13
  end
13
14
 
14
15
  def html
15
- Containers::FormGroup.new(@builder, @object_name, @attribute_name).html do
16
+ Containers::FormGroup.new(@builder, @object_name, @attribute_name, **@form_group).html do
16
17
  safe_join([label_element, supplemental_content, hint_element, error_element, input])
17
18
  end
18
19
  end
@@ -20,7 +21,7 @@ module GOVUKDesignSystemFormBuilder
20
21
  private
21
22
 
22
23
  def input
23
- @builder.send(builder_method, @attribute_name, **input_options.merge(@attributes))
24
+ @builder.send(builder_method, @attribute_name, **input_options, **@html_attributes)
24
25
  end
25
26
 
26
27
  def input_options
@@ -4,10 +4,10 @@ module GOVUKDesignSystemFormBuilder
4
4
  private
5
5
 
6
6
  def label_element
7
- @label_element ||= Elements::Label.new(@builder, @object_name, @attribute_name, caption: @caption, **label_options)
7
+ @label_element ||= Elements::Label.new(@builder, @object_name, @attribute_name, caption: @caption, **label_content)
8
8
  end
9
9
 
10
- def label_options
10
+ def label_content
11
11
  case @label
12
12
  when Hash
13
13
  @label
@@ -1,3 +1,3 @@
1
1
  module GOVUKDesignSystemFormBuilder
2
- VERSION = '1.2.3'.freeze
2
+ VERSION = '2.0.0b1'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: govuk_design_system_formbuilder
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.3
4
+ version: 2.0.0b1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Yates
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-27 00:00:00.000000000 Z
11
+ date: 2020-08-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionview
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - '='
60
60
  - !ruby/object:Gem::Version
61
- version: 3.14.0
61
+ version: 3.17.0
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - '='
67
67
  - !ruby/object:Gem::Version
68
- version: 3.14.0
68
+ version: 3.17.0
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: pry
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -176,14 +176,14 @@ dependencies:
176
176
  requirements:
177
177
  - - "~>"
178
178
  - !ruby/object:Gem::Version
179
- version: 3.19.0
179
+ version: 3.21.0
180
180
  type: :development
181
181
  prerelease: false
182
182
  version_requirements: !ruby/object:Gem::Requirement
183
183
  requirements:
184
184
  - - "~>"
185
185
  - !ruby/object:Gem::Version
186
- version: 3.19.0
186
+ version: 3.21.0
187
187
  - !ruby/object:Gem::Dependency
188
188
  name: rubypants
189
189
  requirement: !ruby/object:Gem::Requirement
@@ -204,14 +204,14 @@ dependencies:
204
204
  requirements:
205
205
  - - "~>"
206
206
  - !ruby/object:Gem::Version
207
- version: 2.3.0
207
+ version: 2.4.0
208
208
  type: :development
209
209
  prerelease: false
210
210
  version_requirements: !ruby/object:Gem::Requirement
211
211
  requirements:
212
212
  - - "~>"
213
213
  - !ruby/object:Gem::Version
214
- version: 2.3.0
214
+ version: 2.4.0
215
215
  - !ruby/object:Gem::Dependency
216
216
  name: sass
217
217
  requirement: !ruby/object:Gem::Requirement
@@ -277,6 +277,7 @@ files:
277
277
  - lib/govuk_design_system_formbuilder/elements/inputs/text.rb
278
278
  - lib/govuk_design_system_formbuilder/elements/inputs/url.rb
279
279
  - lib/govuk_design_system_formbuilder/elements/label.rb
280
+ - lib/govuk_design_system_formbuilder/elements/legend.rb
280
281
  - lib/govuk_design_system_formbuilder/elements/radios/collection.rb
281
282
  - lib/govuk_design_system_formbuilder/elements/radios/collection_radio_button.rb
282
283
  - lib/govuk_design_system_formbuilder/elements/radios/fieldset_radio_button.rb
@@ -313,9 +314,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
313
314
  version: '0'
314
315
  required_rubygems_version: !ruby/object:Gem::Requirement
315
316
  requirements:
316
- - - ">="
317
+ - - ">"
317
318
  - !ruby/object:Gem::Version
318
- version: '0'
319
+ version: 1.3.1
319
320
  requirements: []
320
321
  rubygems_version: 3.1.2
321
322
  signing_key: