govuk_design_system_formbuilder 1.0.1 → 1.1.0.beta.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/lib/govuk_design_system_formbuilder.rb +47 -3
  3. data/lib/govuk_design_system_formbuilder/base.rb +7 -56
  4. data/lib/govuk_design_system_formbuilder/builder.rb +15 -10
  5. data/lib/govuk_design_system_formbuilder/containers/check_boxes_fieldset.rb +5 -2
  6. data/lib/govuk_design_system_formbuilder/containers/fieldset.rb +21 -8
  7. data/lib/govuk_design_system_formbuilder/containers/form_group.rb +1 -1
  8. data/lib/govuk_design_system_formbuilder/containers/radio_buttons_fieldset.rb +5 -2
  9. data/lib/govuk_design_system_formbuilder/containers/radios.rb +3 -1
  10. data/lib/govuk_design_system_formbuilder/containers/supplemental.rb +5 -6
  11. data/lib/govuk_design_system_formbuilder/elements/check_boxes/collection.rb +6 -2
  12. data/lib/govuk_design_system_formbuilder/elements/check_boxes/collection_check_box.rb +6 -2
  13. data/lib/govuk_design_system_formbuilder/elements/check_boxes/fieldset_check_box.rb +4 -1
  14. data/lib/govuk_design_system_formbuilder/elements/check_boxes/hint.rb +3 -1
  15. data/lib/govuk_design_system_formbuilder/elements/check_boxes/label.rb +1 -1
  16. data/lib/govuk_design_system_formbuilder/elements/date.rb +6 -2
  17. data/lib/govuk_design_system_formbuilder/elements/error_message.rb +3 -1
  18. data/lib/govuk_design_system_formbuilder/elements/error_summary.rb +3 -1
  19. data/lib/govuk_design_system_formbuilder/elements/file.rb +6 -1
  20. data/lib/govuk_design_system_formbuilder/elements/hint.rb +11 -2
  21. data/lib/govuk_design_system_formbuilder/elements/inputs/base.rb +5 -0
  22. data/lib/govuk_design_system_formbuilder/elements/label.rb +3 -3
  23. data/lib/govuk_design_system_formbuilder/elements/radios/collection.rb +6 -2
  24. data/lib/govuk_design_system_formbuilder/elements/radios/collection_radio_button.rb +8 -4
  25. data/lib/govuk_design_system_formbuilder/elements/radios/fieldset_radio_button.rb +3 -0
  26. data/lib/govuk_design_system_formbuilder/elements/select.rb +6 -1
  27. data/lib/govuk_design_system_formbuilder/elements/submit.rb +1 -1
  28. data/lib/govuk_design_system_formbuilder/elements/text_area.rb +14 -0
  29. data/lib/govuk_design_system_formbuilder/traits/collection_item.rb +16 -0
  30. data/lib/govuk_design_system_formbuilder/traits/conditional.rb +17 -0
  31. data/lib/govuk_design_system_formbuilder/traits/error.rb +17 -0
  32. data/lib/govuk_design_system_formbuilder/traits/hint.rb +17 -0
  33. data/lib/govuk_design_system_formbuilder/traits/label.rb +11 -0
  34. data/lib/govuk_design_system_formbuilder/traits/localisation.rb +21 -0
  35. data/lib/govuk_design_system_formbuilder/traits/supplemental.rb +17 -0
  36. data/lib/govuk_design_system_formbuilder/version.rb +1 -1
  37. metadata +30 -9
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a461a934a644c5132053494bb2f7226da713744089d2d2685c2288abb203fbc6
4
- data.tar.gz: 6015d09960437e4f7c3672738da64bc67ff7651135e0f8ce52f4e5c261ec1e27
3
+ metadata.gz: b9c9499b630e3696a4d327de2e886f683126009aaf04a40d415c52d03fd48fdf
4
+ data.tar.gz: ae9a0f7b5c079b689da3e719031632a4007ad51b7e7bf4800575d8ba8b529b61
5
5
  SHA512:
6
- metadata.gz: 50b788c0351f43d4919473f08cc4ba58da35a5e899fcdb1556a5ecf4edfbeb8ef327930a6a605fffee68b83d6db7cd90019ad525c86f88ce11a02eb0fd4284af
7
- data.tar.gz: 36a30c7e31d409ef926726c0b2c56fda14c2c2606310c948810f288db2ef2a64637b9c9104d43f368e32bb5ebff4a38719a8e38a9fd76367f97e762d96b92fb7
6
+ metadata.gz: 32d92bf557aec39313728bb51ac4e43ac4a0ba983749a9bce6b9f2b8a107062ecdbd413bb1fb1583844781230f6c8e33f5f3c4eac84d7cbf432bdf0137e6e89d
7
+ data.tar.gz: ef4303cf4ea3ee8082f2597f8cf7f5d84e2b3182dd566fc9c77ad9bc3e21867be362356eb3b94519d132bbed8471c473e9ad023ce38fb4aa97208ecf195001df
@@ -1,3 +1,13 @@
1
+ require 'active_support/configurable'
2
+
3
+ require 'govuk_design_system_formbuilder/traits/collection_item'
4
+ require 'govuk_design_system_formbuilder/traits/conditional'
5
+ require 'govuk_design_system_formbuilder/traits/error'
6
+ require 'govuk_design_system_formbuilder/traits/hint'
7
+ require 'govuk_design_system_formbuilder/traits/label'
8
+ require 'govuk_design_system_formbuilder/traits/localisation'
9
+ require 'govuk_design_system_formbuilder/traits/supplemental'
10
+
1
11
  require 'govuk_design_system_formbuilder/version'
2
12
  require 'govuk_design_system_formbuilder/builder'
3
13
  require 'govuk_design_system_formbuilder/base'
@@ -40,6 +50,42 @@ require 'govuk_design_system_formbuilder/containers/character_count'
40
50
  require 'govuk_design_system_formbuilder/containers/supplemental'
41
51
 
42
52
  module GOVUKDesignSystemFormBuilder
53
+ include ActiveSupport::Configurable
54
+
55
+ DEFAULTS = {
56
+ default_legend_size: 'm',
57
+ default_legend_tag: 'h1',
58
+ default_submit_button_text: 'Continue',
59
+ default_radio_divider_text: 'or',
60
+ default_error_summary_title: 'There is a problem'
61
+ }.freeze
62
+
63
+ DEFAULTS.keys.each { |k| config_accessor(k) { DEFAULTS[k] } }
64
+
65
+ class << self
66
+ # Configure the form builder in the usual manner. All of the
67
+ # keys in {DEFAULTS} can be configured as per the example below
68
+ #
69
+ # @example
70
+ # GOVUKDesignSystemFormBuilder.configure do |conf|
71
+ # conf.default_legend_size = 'xl'
72
+ # conf.default_error_summary_title = 'OMG'
73
+ # end
74
+ def configure
75
+ yield(config)
76
+ end
77
+
78
+ # Resets each of the configurable values to its default
79
+ #
80
+ # @note This method is only really intended for use to clean up
81
+ # during testing
82
+ def reset!
83
+ configure do |c|
84
+ DEFAULTS.each { |k, v| c.send("#{k}=", v) }
85
+ end
86
+ end
87
+ end
88
+
43
89
  class FormBuilder < ActionView::Helpers::FormBuilder
44
90
  delegate :content_tag, :tag, :safe_join, :safe_concat, :capture, :link_to, :raw, to: :@template
45
91
 
@@ -47,7 +93,5 @@ module GOVUKDesignSystemFormBuilder
47
93
  end
48
94
 
49
95
  # Disable Rails' div.field_with_error wrapper
50
- ActionView::Base.field_error_proc = Proc.new do |html_tag, _instance|
51
- html_tag.html_safe
52
- end
96
+ ActionView::Base.field_error_proc = ->(html_tag, _instance) { html_tag }
53
97
  end
@@ -1,29 +1,18 @@
1
1
  module GOVUKDesignSystemFormBuilder
2
2
  class Base
3
+ include Traits::Localisation
4
+
3
5
  delegate :capture, :content_tag, :safe_join, :tag, :raw, :link_to, to: :@builder
6
+ delegate :config, to: GOVUKDesignSystemFormBuilder
4
7
 
5
8
  def initialize(builder, object_name, attribute_name, &block)
6
- @builder = builder
7
- @object_name = object_name
9
+ @builder = builder
10
+ @object_name = object_name
8
11
  @attribute_name = attribute_name
9
- @block_content = capture { block.call } if block_given?
10
- end
11
-
12
- def hint_element
13
- @hint_element ||= Elements::Hint.new(@builder, @object_name, @attribute_name, @hint_text)
14
- end
15
-
16
- def error_element
17
- @error_element ||= Elements::ErrorMessage.new(@builder, @object_name, @attribute_name)
18
- end
19
-
20
- def label_element
21
- @label_element ||= Elements::Label.new(@builder, @object_name, @attribute_name, **@label)
12
+ @block_content = capture { block.call } if block_given?
22
13
  end
23
14
 
24
- def supplemental_content
25
- @supplemental_content ||= Containers::Supplemental.new(@builder, @object_name, @attribute_name, @block_content)
26
- end
15
+ private
27
16
 
28
17
  # returns the id value used for the input
29
18
  #
@@ -46,53 +35,15 @@ module GOVUKDesignSystemFormBuilder
46
35
  end
47
36
  end
48
37
 
49
- def hint_id
50
- return nil unless @hint_text.present?
51
-
52
- build_id('hint')
53
- end
54
-
55
- def error_id
56
- return nil unless has_errors?
57
-
58
- build_id('error')
59
- end
60
-
61
- def conditional_id
62
- build_id('conditional')
63
- end
64
-
65
- def supplemental_id
66
- return nil unless @block_content.present?
67
-
68
- build_id('supplemental')
69
- end
70
-
71
- # Provides an id for use by the textual description of character and word limits.
72
- #
73
- # @note In order for the GOV.UK Frontend JavaScript to pick up this associated field
74
- # it has to have the same id as the text area with the additional suffix of '-info'
75
- def limit_id
76
- [field_id(link_errors: true), 'info'].join('-')
77
- end
78
-
79
38
  def has_errors?
80
39
  @builder.object.errors.any? &&
81
40
  @builder.object.errors.messages.dig(@attribute_name).present?
82
41
  end
83
42
 
84
- def wrap_conditional(block)
85
- content_tag('div', class: conditional_classes, id: conditional_id) do
86
- capture { block.call }
87
- end
88
- end
89
-
90
43
  def described_by(*ids)
91
44
  ids.flatten.compact.join(' ').presence
92
45
  end
93
46
 
94
- private
95
-
96
47
  # Builds the values used for HTML id attributes throughout the builder
97
48
  #
98
49
  # @param id_type [String] a description of the id's type, eg +hint+, +error+
@@ -1,5 +1,6 @@
1
1
  module GOVUKDesignSystemFormBuilder
2
2
  module Builder
3
+ delegate :config, to: GOVUKDesignSystemFormBuilder
3
4
  # Generates a input of type +text+
4
5
  #
5
6
  # @param attribute_name [Symbol] The name of the attribute
@@ -201,9 +202,12 @@ module GOVUKDesignSystemFormBuilder
201
202
  #
202
203
  # @param attribute_name [Symbol] The name of the attribute
203
204
  # @param collection [Enumerable<Object>] Options to be added to the +select+ element
204
- # @param value_method [Symbol] The method called against each member of the collection to provide the value
205
- # @param text_method [Symbol] The method called against each member of the collection to provide the text
206
- # @param hint_method [Symbol] The method called against each member of the collection to provide the hint text
205
+ # @param value_method [Symbol, Proc] The method called against each member of the collection to provide the value.
206
+ # When a +Proc+ is provided it must take a single argument that is a single member of the collection
207
+ # @param text_method [Symbol, Proc] The method called against each member of the collection to provide the label text.
208
+ # When a +Proc+ is provided it must take a single argument that is a single member of the collection
209
+ # @param hint_method [Symbol, Proc] The method called against each member of the collection to provide the hint text.
210
+ # When a +Proc+ is provided it must take a single argument that is a single member of the collection
207
211
  # @param hint_text [String] The content of the fieldset hint. No hint will be injected if left +nil+
208
212
  # @param legend [Hash] options for configuring the legend
209
213
  # @param inline [Boolean] controls whether the radio buttons are displayed inline or not
@@ -214,7 +218,7 @@ module GOVUKDesignSystemFormBuilder
214
218
  # @option legend tag [Symbol,String] the tag used for the fieldset's header, defaults to +h1+, defaults to +h1+
215
219
  # @return [ActiveSupport::SafeBuffer] HTML output
216
220
  #
217
- # @example A collection of radio buttons for favourite colours
221
+ # @example A collection of radio buttons for favourite colours, labels capitalised via a proc
218
222
  #
219
223
  # @colours = [
220
224
  # OpenStruct.new(id: 'red', name: 'Red', description: 'Roses are red'),
@@ -224,7 +228,7 @@ module GOVUKDesignSystemFormBuilder
224
228
  # = f.govuk_collection_radio_buttons :favourite_colour,
225
229
  # @colours,
226
230
  # :id,
227
- # :name,
231
+ # ->(option) { option.name.upcase },
228
232
  # :description,
229
233
  # legend: { text: 'Pick your favourite colour', size: 'm' },
230
234
  # hint_text: 'If you cannot find the exact match choose something close',
@@ -307,7 +311,7 @@ module GOVUKDesignSystemFormBuilder
307
311
  # @return [ActiveSupport::SafeBuffer] HTML output
308
312
  # @example A custom divider
309
313
  # = govuk_radio_divider 'Alternatively'
310
- def govuk_radio_divider(text = 'or')
314
+ def govuk_radio_divider(text = config.default_radio_divider_text)
311
315
  tag.div(text, class: %w(govuk-radios__divider))
312
316
  end
313
317
 
@@ -316,8 +320,9 @@ module GOVUKDesignSystemFormBuilder
316
320
  # @param attribute_name [Symbol] The name of the attribute
317
321
  # @param collection [Enumerable<Object>] Options to be added to the +select+ element
318
322
  # @param value_method [Symbol] The method called against each member of the collection to provide the value
319
- # @param text_method [Symbol] The method called against each member of the collection to provide the text
320
- # @param hint_method [Symbol] The method called against each member of the collection to provide the hint text
323
+ # @param text_method [Symbol] The method called against each member of the collection to provide the label text
324
+ # @param hint_method [Symbol, Proc] The method called against each member of the collection to provide the hint text.
325
+ # When a +Proc+ is provided it must take a single argument that is a single member of the collection
321
326
  # @param hint_text [String] The content of the fieldset hint. No hint will be injected if left +nil+
322
327
  # @param small [Boolean] controls whether small check boxes are used instead of regular-sized ones
323
328
  # @param legend [Hash] options for configuring the legend
@@ -450,7 +455,7 @@ module GOVUKDesignSystemFormBuilder
450
455
  # = f.govuk_submit "Proceed", prevent_double_click: true do
451
456
  # = link_to 'Cancel', some_other_path, class: 'govuk-button__secondary'
452
457
  #
453
- def govuk_submit(text = 'Continue', warning: false, secondary: false, prevent_double_click: true, validate: false, &block)
458
+ def govuk_submit(text = config.default_submit_button_text, warning: false, secondary: false, prevent_double_click: true, validate: false, &block)
454
459
  Elements::Submit.new(self, text, warning: warning, secondary: secondary, prevent_double_click: prevent_double_click, validate: validate, &block).html
455
460
  end
456
461
 
@@ -496,7 +501,7 @@ module GOVUKDesignSystemFormBuilder
496
501
  # = f.govuk_error_summary 'Uh-oh, spaghettios'
497
502
  #
498
503
  # @see https://design-system.service.gov.uk/components/error-summary/ GOV.UK error summary
499
- def govuk_error_summary(title = 'There is a problem')
504
+ def govuk_error_summary(title = config.default_error_summary_title)
500
505
  Elements::ErrorSummary.new(self, object_name, title).html
501
506
  end
502
507
 
@@ -1,6 +1,9 @@
1
1
  module GOVUKDesignSystemFormBuilder
2
2
  module Containers
3
- class CheckBoxesFieldset < GOVUKDesignSystemFormBuilder::Base
3
+ class CheckBoxesFieldset < Base
4
+ include Traits::Error
5
+ include Traits::Hint
6
+
4
7
  def initialize(builder, object_name, attribute_name, hint_text:, legend:, small:, &block)
5
8
  super(builder, object_name, attribute_name, &block)
6
9
 
@@ -12,7 +15,7 @@ module GOVUKDesignSystemFormBuilder
12
15
 
13
16
  def html
14
17
  Containers::FormGroup.new(@builder, @object_name, @attribute_name).html do
15
- Containers::Fieldset.new(@builder, legend: @legend, described_by: [error_element.error_id, hint_element.hint_id]).html do
18
+ Containers::Fieldset.new(@builder, @object_name, @attribute_name, legend: @legend, described_by: [error_element.error_id, hint_element.hint_id]).html do
16
19
  safe_join(
17
20
  [
18
21
  hint_element.html,
@@ -1,13 +1,14 @@
1
1
  module GOVUKDesignSystemFormBuilder
2
2
  module Containers
3
- class Fieldset < GOVUKDesignSystemFormBuilder::Base
4
- LEGEND_DEFAULTS = { text: nil, tag: 'h1', size: 'm' }.freeze
3
+ class Fieldset < Base
5
4
  LEGEND_SIZES = %w(xl l m s).freeze
6
5
 
7
- def initialize(builder, legend: {}, described_by: nil)
8
- @builder = builder
9
- @legend = LEGEND_DEFAULTS.merge(legend)
10
- @described_by = described_by(described_by)
6
+ def initialize(builder, object_name = nil, attribute_name = nil, legend: {}, described_by: nil)
7
+ super(builder, object_name, attribute_name)
8
+
9
+ @legend = legend_defaults.merge(legend)
10
+ @described_by = described_by(described_by)
11
+ @attribute_name = attribute_name
11
12
  end
12
13
 
13
14
  def html
@@ -18,14 +19,26 @@ module GOVUKDesignSystemFormBuilder
18
19
 
19
20
  private
20
21
 
22
+ def legend_defaults
23
+ {
24
+ text: nil,
25
+ tag: config.default_legend_tag,
26
+ size: config.default_legend_size
27
+ }
28
+ end
29
+
21
30
  def build_legend
22
- if @legend.dig(:text).present?
31
+ if legend_text.present?
23
32
  content_tag('legend', class: legend_classes) do
24
- tag.send(@legend.dig(:tag), @legend.dig(:text), class: legend_heading_classes)
33
+ tag.send(@legend.dig(:tag), legend_text, class: legend_heading_classes)
25
34
  end
26
35
  end
27
36
  end
28
37
 
38
+ def legend_text
39
+ [@legend.dig(:text), localised_text('fieldset')].compact.first
40
+ end
41
+
29
42
  def fieldset_classes
30
43
  %w(govuk-fieldset)
31
44
  end
@@ -1,6 +1,6 @@
1
1
  module GOVUKDesignSystemFormBuilder
2
2
  module Containers
3
- class FormGroup < GOVUKDesignSystemFormBuilder::Base
3
+ class FormGroup < Base
4
4
  def initialize(builder, object_name, attribute_name)
5
5
  super(builder, object_name, attribute_name)
6
6
  end
@@ -1,6 +1,9 @@
1
1
  module GOVUKDesignSystemFormBuilder
2
2
  module Containers
3
- class RadioButtonsFieldset < GOVUKDesignSystemFormBuilder::Base
3
+ class RadioButtonsFieldset < Base
4
+ include Traits::Hint
5
+ include Traits::Error
6
+
4
7
  def initialize(builder, object_name, attribute_name, hint_text:, legend:, inline:, small:, &block)
5
8
  super(builder, object_name, attribute_name)
6
9
 
@@ -13,7 +16,7 @@ module GOVUKDesignSystemFormBuilder
13
16
 
14
17
  def html
15
18
  Containers::FormGroup.new(@builder, @object_name, @attribute_name).html do
16
- Containers::Fieldset.new(@builder, legend: @legend, described_by: [error_element.error_id, hint_element.hint_id]).html do
19
+ Containers::Fieldset.new(@builder, @object_name, @attribute_name, legend: @legend, described_by: [error_element.error_id, hint_element.hint_id]).html do
17
20
  safe_join(
18
21
  [
19
22
  hint_element.html,
@@ -1,6 +1,8 @@
1
1
  module GOVUKDesignSystemFormBuilder
2
2
  module Containers
3
- class Radios < GOVUKDesignSystemFormBuilder::Base
3
+ class Radios < Base
4
+ include Traits::Hint
5
+
4
6
  def initialize(builder, inline:, small:)
5
7
  @builder = builder
6
8
  @inline = inline
@@ -1,15 +1,14 @@
1
1
  module GOVUKDesignSystemFormBuilder
2
2
  module Containers
3
- class Supplemental < GOVUKDesignSystemFormBuilder::Base
3
+ class Supplemental < Base
4
4
  def initialize(builder, object_name, attribute_name, content)
5
- @builder = builder
6
- @object_name = object_name
7
- @attribute_name = attribute_name
8
- @content = content
5
+ super(builder, object_name, attribute_name)
6
+
7
+ @content = content
9
8
  end
10
9
 
11
10
  def html
12
- return nil unless @content.present?
11
+ return nil if @content.blank?
13
12
 
14
13
  content_tag('div', id: supplemental_id) do
15
14
  @content
@@ -1,7 +1,11 @@
1
1
  module GOVUKDesignSystemFormBuilder
2
2
  module Elements
3
3
  module CheckBoxes
4
- class Collection < GOVUKDesignSystemFormBuilder::Base
4
+ class Collection < Base
5
+ include Traits::Error
6
+ include Traits::Hint
7
+ include Traits::Supplemental
8
+
5
9
  def initialize(builder, object_name, attribute_name, collection, value_method:, text_method:, hint_method: nil, hint_text:, legend:, small:, &block)
6
10
  super(builder, object_name, attribute_name, &block)
7
11
 
@@ -16,7 +20,7 @@ module GOVUKDesignSystemFormBuilder
16
20
 
17
21
  def html
18
22
  Containers::FormGroup.new(@builder, @object_name, @attribute_name).html do
19
- Containers::Fieldset.new(@builder, legend: @legend, described_by: [error_id, hint_id, supplemental_id]).html do
23
+ Containers::Fieldset.new(@builder, @object_name, @attribute_name, legend: @legend, described_by: [error_id, hint_id, supplemental_id]).html do
20
24
  safe_join(
21
25
  [
22
26
  hint_element.html,
@@ -1,13 +1,17 @@
1
1
  module GOVUKDesignSystemFormBuilder
2
2
  module Elements
3
3
  module CheckBoxes
4
- class CollectionCheckBox < GOVUKDesignSystemFormBuilder::Base
4
+ class CollectionCheckBox < Base
5
+ include Traits::CollectionItem
6
+ include Traits::Hint
7
+
5
8
  def initialize(builder, object_name, attribute_name, checkbox, hint_method = nil, link_errors: false)
6
9
  super(builder, object_name, attribute_name)
10
+
7
11
  @checkbox = checkbox
8
12
  @item = checkbox.object
9
13
  @value = checkbox.value
10
- @hint_text = @item.send(hint_method) if hint_method.present?
14
+ @hint_text = retrieve(@item, hint_method)
11
15
  @link_errors = link_errors
12
16
  end
13
17
 
@@ -1,7 +1,10 @@
1
1
  module GOVUKDesignSystemFormBuilder
2
2
  module Elements
3
3
  module CheckBoxes
4
- class FieldsetCheckBox < GOVUKDesignSystemFormBuilder::Base
4
+ class FieldsetCheckBox < Base
5
+ include Traits::Hint
6
+ include Traits::Conditional
7
+
5
8
  def initialize(builder, object_name, attribute_name, value, label:, hint_text:, link_errors:, multiple:, &block)
6
9
  super(builder, object_name, attribute_name)
7
10
 
@@ -1,7 +1,9 @@
1
1
  module GOVUKDesignSystemFormBuilder
2
2
  module Elements
3
3
  module CheckBoxes
4
- class Hint < GOVUKDesignSystemFormBuilder::Base
4
+ class Hint < Base
5
+ include Traits::Hint
6
+
5
7
  def initialize(builder, object_name, attribute_name, hint_text, value:)
6
8
  super(builder, object_name, attribute_name)
7
9
 
@@ -1,7 +1,7 @@
1
1
  module GOVUKDesignSystemFormBuilder
2
2
  module Elements
3
3
  module CheckBoxes
4
- class Label < GOVUKDesignSystemFormBuilder::Base
4
+ class Label < Base
5
5
  def initialize(builder, object_name, attribute_name, checkbox, value:, link_errors: true)
6
6
  super(builder, object_name, attribute_name)
7
7
 
@@ -1,6 +1,10 @@
1
1
  module GOVUKDesignSystemFormBuilder
2
2
  module Elements
3
- class Date < GOVUKDesignSystemFormBuilder::Base
3
+ class Date < Base
4
+ include Traits::Error
5
+ include Traits::Hint
6
+ include Traits::Supplemental
7
+
4
8
  SEGMENTS = { day: '3i', month: '2i', year: '1i' }.freeze
5
9
 
6
10
  def initialize(builder, object_name, attribute_name, legend:, hint_text:, date_of_birth: false, omit_day:, &block)
@@ -14,7 +18,7 @@ module GOVUKDesignSystemFormBuilder
14
18
 
15
19
  def html
16
20
  Containers::FormGroup.new(@builder, @object_name, @attribute_name).html do
17
- Containers::Fieldset.new(@builder, legend: @legend, described_by: [error_id, hint_id, supplemental_id]).html do
21
+ Containers::Fieldset.new(@builder, @object_name, @attribute_name, legend: @legend, described_by: [error_id, hint_id, supplemental_id]).html do
18
22
  safe_join(
19
23
  [
20
24
  hint_element.html,
@@ -1,6 +1,8 @@
1
1
  module GOVUKDesignSystemFormBuilder
2
2
  module Elements
3
- class ErrorMessage < GOVUKDesignSystemFormBuilder::Base
3
+ class ErrorMessage < Base
4
+ include Traits::Error
5
+
4
6
  def initialize(builder, object_name, attribute_name)
5
7
  super(builder, object_name, attribute_name)
6
8
  end
@@ -1,6 +1,8 @@
1
1
  module GOVUKDesignSystemFormBuilder
2
2
  module Elements
3
- class ErrorSummary < GOVUKDesignSystemFormBuilder::Base
3
+ class ErrorSummary < Base
4
+ include Traits::Error
5
+
4
6
  def initialize(builder, object_name, title)
5
7
  @builder = builder
6
8
  @object_name = object_name
@@ -1,6 +1,11 @@
1
1
  module GOVUKDesignSystemFormBuilder
2
2
  module Elements
3
- class File < GOVUKDesignSystemFormBuilder::Base
3
+ class File < Base
4
+ include Traits::Error
5
+ include Traits::Hint
6
+ include Traits::Label
7
+ include Traits::Supplemental
8
+
4
9
  def initialize(builder, object_name, attribute_name, hint_text:, label:, **extra_args, &block)
5
10
  super(builder, object_name, attribute_name, &block)
6
11
 
@@ -1,11 +1,13 @@
1
1
  module GOVUKDesignSystemFormBuilder
2
2
  module Elements
3
- class Hint < GOVUKDesignSystemFormBuilder::Base
3
+ class Hint < Base
4
+ include Traits::Hint
5
+
4
6
  def initialize(builder, object_name, attribute_name, text, value = nil, radio: false, checkbox: false)
5
7
  super(builder, object_name, attribute_name)
6
8
 
7
9
  @value = value
8
- @hint_text = text
10
+ @hint_text = hint_text(text)
9
11
  @radio_class = radio_class(radio)
10
12
  @checkbox_class = checkbox_class(checkbox)
11
13
  end
@@ -18,6 +20,13 @@ module GOVUKDesignSystemFormBuilder
18
20
 
19
21
  private
20
22
 
23
+ def hint_text(supplied)
24
+ [
25
+ supplied.presence,
26
+ localised_text('hint')
27
+ ].compact.first
28
+ end
29
+
21
30
  def hint_classes
22
31
  %w(govuk-hint).push(@radio_class, @checkbox_class).compact
23
32
  end
@@ -2,6 +2,11 @@ module GOVUKDesignSystemFormBuilder
2
2
  module Elements
3
3
  module Inputs
4
4
  class Base < GOVUKDesignSystemFormBuilder::Base
5
+ include Traits::Error
6
+ include Traits::Hint
7
+ include Traits::Label
8
+ include Traits::Supplemental
9
+
5
10
  def initialize(builder, object_name, attribute_name, hint_text:, label:, width:, **extra_args, &block)
6
11
  super(builder, object_name, attribute_name, &block)
7
12
 
@@ -1,6 +1,6 @@
1
1
  module GOVUKDesignSystemFormBuilder
2
2
  module Elements
3
- class Label < GOVUKDesignSystemFormBuilder::Base
3
+ class Label < Base
4
4
  def initialize(builder, object_name, attribute_name, text: nil, value: nil, size: nil, hidden: false, radio: false, checkbox: false, tag: nil, link_errors: true)
5
5
  super(builder, object_name, attribute_name)
6
6
 
@@ -37,12 +37,12 @@ module GOVUKDesignSystemFormBuilder
37
37
  end
38
38
 
39
39
  def label_text(option_text, hidden)
40
- text = [option_text, @value, @attribute_name.capitalize].compact.first
40
+ text = [option_text, @value, localised_text('label'), @attribute_name.capitalize].compact.first.to_s
41
41
 
42
42
  if hidden
43
43
  tag.span(text, class: %w(govuk-visually-hidden))
44
44
  else
45
- raw(text)
45
+ text
46
46
  end
47
47
  end
48
48
 
@@ -1,7 +1,11 @@
1
1
  module GOVUKDesignSystemFormBuilder
2
2
  module Elements
3
3
  module Radios
4
- class Collection < GOVUKDesignSystemFormBuilder::Base
4
+ class Collection < Base
5
+ include Traits::Error
6
+ include Traits::Hint
7
+ include Traits::Supplemental
8
+
5
9
  def initialize(builder, object_name, attribute_name, collection, value_method:, text_method:, hint_method:, hint_text:, legend:, inline:, small:, bold_labels:, &block)
6
10
  super(builder, object_name, attribute_name, &block)
7
11
 
@@ -18,7 +22,7 @@ module GOVUKDesignSystemFormBuilder
18
22
 
19
23
  def html
20
24
  Containers::FormGroup.new(@builder, @object_name, @attribute_name).html do
21
- Containers::Fieldset.new(@builder, legend: @legend, described_by: [error_id, hint_id, supplemental_id]).html do
25
+ Containers::Fieldset.new(@builder, @object_name, @attribute_name, legend: @legend, described_by: [error_id, hint_id, supplemental_id]).html do
22
26
  safe_join(
23
27
  [
24
28
  hint_element.html,
@@ -2,16 +2,20 @@ module GOVUKDesignSystemFormBuilder
2
2
  module Elements
3
3
  module Radios
4
4
  class CollectionRadioButton < Base
5
+ include Traits::Hint
6
+ include Traits::CollectionItem
7
+
5
8
  # @param link_errors [Boolean] used to control the id generated for radio buttons. The
6
9
  # error summary requires that the id of the first radio is linked-to from the corresponding
7
10
  # error message. As when the summary is built what happens later in the form is unknown, we
8
11
  # need to control this to ensure the link is generated correctly
9
12
  def initialize(builder, object_name, attribute_name, item, value_method:, text_method:, hint_method:, link_errors: false, bold_labels:)
10
13
  super(builder, object_name, attribute_name)
14
+
11
15
  @item = item
12
- @value = item.send(value_method)
13
- @text = item.send(text_method)
14
- @hint_text = item.send(hint_method) if hint_method.present?
16
+ @value = retrieve(item, value_method)
17
+ @label_text = retrieve(item, text_method)
18
+ @hint_text = retrieve(item, hint_method)
15
19
  @link_errors = link_errors
16
20
  @bold_labels = bold_labels
17
21
  end
@@ -41,7 +45,7 @@ module GOVUKDesignSystemFormBuilder
41
45
  end
42
46
 
43
47
  def label_element
44
- @label_element ||= Elements::Label.new(@builder, @object_name, @attribute_name, text: @text, value: @value, radio: true, size: label_size, link_errors: @link_errors)
48
+ @label_element ||= Elements::Label.new(@builder, @object_name, @attribute_name, text: @label_text, value: @value, radio: true, size: label_size, link_errors: @link_errors)
45
49
  end
46
50
 
47
51
  def label_size
@@ -2,6 +2,9 @@ module GOVUKDesignSystemFormBuilder
2
2
  module Elements
3
3
  module Radios
4
4
  class FieldsetRadioButton < Base
5
+ include Traits::Hint
6
+ include Traits::Conditional
7
+
5
8
  def initialize(builder, object_name, attribute_name, value, label:, hint_text:, link_errors:, &block)
6
9
  super(builder, object_name, attribute_name)
7
10
 
@@ -1,6 +1,11 @@
1
1
  module GOVUKDesignSystemFormBuilder
2
2
  module Elements
3
- class Select < GOVUKDesignSystemFormBuilder::Base
3
+ class Select < Base
4
+ include Traits::Error
5
+ include Traits::Label
6
+ include Traits::Hint
7
+ include Traits::Supplemental
8
+
4
9
  def initialize(builder, object_name, attribute_name, collection, value_method:, text_method:, options: {}, html_options: {}, hint_text:, label:, &block)
5
10
  super(builder, object_name, attribute_name, &block)
6
11
 
@@ -1,6 +1,6 @@
1
1
  module GOVUKDesignSystemFormBuilder
2
2
  module Elements
3
- class Submit < GOVUKDesignSystemFormBuilder::Base
3
+ class Submit < Base
4
4
  def initialize(builder, text, warning:, secondary:, prevent_double_click:, validate:, &block)
5
5
  fail ArgumentError, 'buttons can be warning or secondary' if warning && secondary
6
6
 
@@ -1,8 +1,14 @@
1
1
  module GOVUKDesignSystemFormBuilder
2
2
  module Elements
3
3
  class TextArea < Base
4
+ include Traits::Error
5
+ include Traits::Hint
6
+ include Traits::Label
7
+ include Traits::Supplemental
8
+
4
9
  def initialize(builder, object_name, attribute_name, hint_text:, label:, rows:, max_words:, max_chars:, threshold:, **extra_args, &block)
5
10
  super(builder, object_name, attribute_name, &block)
11
+
6
12
  @label = label
7
13
  @hint_text = hint_text
8
14
  @extra_args = extra_args
@@ -43,6 +49,14 @@ module GOVUKDesignSystemFormBuilder
43
49
  end
44
50
  end
45
51
 
52
+ # Provides an id for use by the textual description of character and word limits.
53
+ #
54
+ # @note In order for the GOV.UK Frontend JavaScript to pick up this associated field
55
+ # it has to have the same id as the text area with the additional suffix of '-info'
56
+ def limit_id
57
+ [field_id(link_errors: true), 'info'].join('-')
58
+ end
59
+
46
60
  def limit?
47
61
  @max_words || @max_chars
48
62
  end
@@ -0,0 +1,16 @@
1
+ module GOVUKDesignSystemFormBuilder
2
+ module Traits
3
+ module CollectionItem
4
+ private
5
+
6
+ def retrieve(item, method)
7
+ case method
8
+ when Symbol, String
9
+ item.send(method)
10
+ when Proc
11
+ method.call(item)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ module GOVUKDesignSystemFormBuilder
2
+ module Traits
3
+ module Conditional
4
+ private
5
+
6
+ def conditional_id
7
+ build_id('conditional')
8
+ end
9
+
10
+ def wrap_conditional(block)
11
+ content_tag('div', class: conditional_classes, id: conditional_id) do
12
+ capture { block.call }
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ module GOVUKDesignSystemFormBuilder
2
+ module Traits
3
+ module Error
4
+ def error_id
5
+ return nil unless has_errors?
6
+
7
+ build_id('error')
8
+ end
9
+
10
+ private
11
+
12
+ def error_element
13
+ @error_element ||= Elements::ErrorMessage.new(@builder, @object_name, @attribute_name)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ module GOVUKDesignSystemFormBuilder
2
+ module Traits
3
+ module Hint
4
+ def hint_id
5
+ return nil if @hint_text.blank?
6
+
7
+ build_id('hint')
8
+ end
9
+
10
+ private
11
+
12
+ def hint_element
13
+ @hint_element ||= Elements::Hint.new(@builder, @object_name, @attribute_name, @hint_text)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,11 @@
1
+ module GOVUKDesignSystemFormBuilder
2
+ module Traits
3
+ module Label
4
+ private
5
+
6
+ def label_element
7
+ @label_element ||= Elements::Label.new(@builder, @object_name, @attribute_name, **@label)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,21 @@
1
+ module GOVUKDesignSystemFormBuilder
2
+ module Traits
3
+ module Localisation
4
+ private
5
+
6
+ def localised_text(context)
7
+ key = localisation_key(context)
8
+
9
+ return nil unless I18n.exists?(key)
10
+
11
+ I18n.translate(key)
12
+ end
13
+
14
+ def localisation_key(context)
15
+ return nil unless @object_name.present? && @attribute_name.present?
16
+
17
+ ['helpers', context, @object_name, @attribute_name].join('.')
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,17 @@
1
+ module GOVUKDesignSystemFormBuilder
2
+ module Traits
3
+ module Supplemental
4
+ def supplemental_id
5
+ return nil if @block_content.blank?
6
+
7
+ build_id('supplemental')
8
+ end
9
+
10
+ private
11
+
12
+ def supplemental_content
13
+ @supplemental_content ||= Containers::Supplemental.new(@builder, @object_name, @attribute_name, @block_content)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -1,3 +1,3 @@
1
1
  module GOVUKDesignSystemFormBuilder
2
- VERSION = '1.0.1'.freeze
2
+ VERSION = '1.1.0.beta.1'.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.0.1
4
+ version: 1.1.0.beta.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Yates
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-11-23 00:00:00.000000000 Z
11
+ date: 2019-12-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionview
@@ -53,19 +53,19 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '5.2'
55
55
  - !ruby/object:Gem::Dependency
56
- name: govuk-lint
56
+ name: rubocop-govuk
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '4'
61
+ version: '2'
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: '4'
68
+ version: '2'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: pry
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -204,14 +204,14 @@ dependencies:
204
204
  requirements:
205
205
  - - "~>"
206
206
  - !ruby/object:Gem::Version
207
- version: 3.12.0
207
+ version: 3.13.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: 3.12.0
214
+ version: 3.13.0
215
215
  - !ruby/object:Gem::Dependency
216
216
  name: sassc
217
217
  requirement: !ruby/object:Gem::Requirement
@@ -226,6 +226,20 @@ dependencies:
226
226
  - - "~>"
227
227
  - !ruby/object:Gem::Version
228
228
  version: 2.2.1
229
+ - !ruby/object:Gem::Dependency
230
+ name: sass
231
+ requirement: !ruby/object:Gem::Requirement
232
+ requirements:
233
+ - - ">="
234
+ - !ruby/object:Gem::Version
235
+ version: '0'
236
+ type: :development
237
+ prerelease: false
238
+ version_requirements: !ruby/object:Gem::Requirement
239
+ requirements:
240
+ - - ">="
241
+ - !ruby/object:Gem::Version
242
+ version: '0'
229
243
  - !ruby/object:Gem::Dependency
230
244
  name: slim
231
245
  requirement: !ruby/object:Gem::Requirement
@@ -283,6 +297,13 @@ files:
283
297
  - lib/govuk_design_system_formbuilder/elements/select.rb
284
298
  - lib/govuk_design_system_formbuilder/elements/submit.rb
285
299
  - lib/govuk_design_system_formbuilder/elements/text_area.rb
300
+ - lib/govuk_design_system_formbuilder/traits/collection_item.rb
301
+ - lib/govuk_design_system_formbuilder/traits/conditional.rb
302
+ - lib/govuk_design_system_formbuilder/traits/error.rb
303
+ - lib/govuk_design_system_formbuilder/traits/hint.rb
304
+ - lib/govuk_design_system_formbuilder/traits/label.rb
305
+ - lib/govuk_design_system_formbuilder/traits/localisation.rb
306
+ - lib/govuk_design_system_formbuilder/traits/supplemental.rb
286
307
  - lib/govuk_design_system_formbuilder/version.rb
287
308
  homepage: https://govuk-form-builder.netlify.com
288
309
  licenses:
@@ -304,9 +325,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
304
325
  version: '0'
305
326
  required_rubygems_version: !ruby/object:Gem::Requirement
306
327
  requirements:
307
- - - ">="
328
+ - - ">"
308
329
  - !ruby/object:Gem::Version
309
- version: '0'
330
+ version: 1.3.1
310
331
  requirements: []
311
332
  rubygems_version: 3.0.3
312
333
  signing_key: