govuk_design_system_formbuilder 2.7.1 → 2.7.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (29) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +21 -7
  3. data/lib/govuk_design_system_formbuilder/base.rb +1 -1
  4. data/lib/govuk_design_system_formbuilder/builder.rb +13 -6
  5. data/lib/govuk_design_system_formbuilder/containers/check_boxes.rb +1 -1
  6. data/lib/govuk_design_system_formbuilder/containers/check_boxes_fieldset.rb +2 -1
  7. data/lib/govuk_design_system_formbuilder/containers/fieldset.rb +1 -1
  8. data/lib/govuk_design_system_formbuilder/containers/form_group.rb +1 -1
  9. data/lib/govuk_design_system_formbuilder/containers/radio_buttons_fieldset.rb +3 -2
  10. data/lib/govuk_design_system_formbuilder/containers/radios.rb +1 -1
  11. data/lib/govuk_design_system_formbuilder/elements/check_boxes/fieldset_check_box.rb +2 -2
  12. data/lib/govuk_design_system_formbuilder/elements/check_boxes/label.rb +5 -1
  13. data/lib/govuk_design_system_formbuilder/elements/collection_select.rb +1 -1
  14. data/lib/govuk_design_system_formbuilder/elements/error_summary.rb +14 -3
  15. data/lib/govuk_design_system_formbuilder/elements/file.rb +1 -1
  16. data/lib/govuk_design_system_formbuilder/elements/label.rb +1 -1
  17. data/lib/govuk_design_system_formbuilder/elements/legend.rb +1 -1
  18. data/lib/govuk_design_system_formbuilder/elements/radios/collection.rb +5 -1
  19. data/lib/govuk_design_system_formbuilder/elements/radios/fieldset_radio_button.rb +2 -2
  20. data/lib/govuk_design_system_formbuilder/elements/select.rb +1 -1
  21. data/lib/govuk_design_system_formbuilder/elements/text_area.rb +1 -1
  22. data/lib/govuk_design_system_formbuilder/presenters/error_summary.rb +29 -0
  23. data/lib/govuk_design_system_formbuilder/traits/fieldset_item.rb +5 -5
  24. data/lib/govuk_design_system_formbuilder/traits/html_attributes.rb +13 -7
  25. data/lib/govuk_design_system_formbuilder/traits/input.rb +1 -1
  26. data/lib/govuk_design_system_formbuilder/traits/select.rb +1 -1
  27. data/lib/govuk_design_system_formbuilder/version.rb +1 -1
  28. data/lib/govuk_design_system_formbuilder.rb +9 -5
  29. metadata +8 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 710ba81f8f737d55dd18626c24a9353862d9b53886127a064e5dc10ddf6fd750
4
- data.tar.gz: e82e7198bf059a97d9b0cb9ce0659133b8082dfac2e71c7c37fc4ff3ff1e38fa
3
+ metadata.gz: bb37ec8e436af4e21bbc44dfbc7dbbead355c07f3f41f802f03de5550b7c419c
4
+ data.tar.gz: 8aba7fb23067d7ee5fc422488525b8d0d013bf7f76b67ecb31647619e4901f8f
5
5
  SHA512:
6
- metadata.gz: cd1fee6e4db64215afaa4ceb8083138ee01f11407137cf5885bf8823e0e86cf34ef27e7178118e2a10fe82eef82ce22163d7cef6e1f8f953e5356dde0de1fe96
7
- data.tar.gz: c5b20b72db3dd9598fe20e3c42527b715cca82d936bd53a9255b7033fff6620fd631e74f3072d5092852b838be89ebb4dd3b04d567c711a17d6a909e52a0eed3
6
+ metadata.gz: cfcabda01e6b764bbc17ea8185180b48095c2eae04cf308387c7fa430b374fa0c40bffb50e7bacf2d7c0abadca362e4fb07ae27699e7645bd732d9a9487a792a
7
+ data.tar.gz: 37ee3a7a8d6dbf5eaf441d81102e507e17043e79b4b774b3ebdad9e1d6d8607e509651c5b9586d029d1be1732805e950f1824e46f47cb88d5106f5a011bf2ff4
data/README.md CHANGED
@@ -1,14 +1,13 @@
1
1
  # GOV.UK Design System Form Builder for Rails
2
2
 
3
- [![Tests](https://github.com/DFE-Digital/govuk_design_system_formbuilder/workflows/Tests/badge.svg)](https://github.com/DFE-Digital/govuk_design_system_formbuilder/actions)
4
- [![Maintainability](https://api.codeclimate.com/v1/badges/fde73b5dc9476197281b/maintainability)](https://codeclimate.com/github/DFE-Digital/govuk_design_system_formbuilder/maintainability)
3
+ [![Tests](https://github.com/DFE-Digital/govuk-formbuilder/workflows/Tests/badge.svg)](https://github.com/DFE-Digital/govuk-formbuilder/actions)
4
+ [![Maintainability](https://api.codeclimate.com/v1/badges/110136fb22341d3ba646/maintainability)](https://codeclimate.com/github/DFE-Digital/govuk-formbuilder/maintainability)
5
5
  [![Gem Version](https://badge.fury.io/rb/govuk_design_system_formbuilder.svg)](https://badge.fury.io/rb/govuk_design_system_formbuilder)
6
6
  [![Gem](https://img.shields.io/gem/dt/govuk_design_system_formbuilder?logo=rubygems)](https://rubygems.org/gems/govuk_design_system_formbuilder)
7
- [![Test Coverage](https://api.codeclimate.com/v1/badges/fde73b5dc9476197281b/test_coverage)](https://codeclimate.com/github/DFE-Digital/govuk_design_system_formbuilder/test_coverage)
8
- [![Dependabot Status](https://api.dependabot.com/badges/status?host=github&repo=DFE-Digital/govuk_design_system_formbuilder)](https://dependabot.com)
9
- [![GitHub license](https://img.shields.io/github/license/DFE-Digital/govuk_design_system_formbuilder)](https://github.com/DFE-Digital/govuk_design_system_formbuilder/blob/master/LICENSE)
10
- [![GOV.UK Design System Version](https://img.shields.io/badge/GOV.UK%20Design%20System-3.13.0-brightgreen)](https://design-system.service.gov.uk)
11
- [![Rails](https://img.shields.io/badge/Ruby-2.6.7%20%E2%95%B1%202.7.3%20%E2%95%B1%203.0.1-E16D6D)](https://www.ruby-lang.org/en/downloads/)
7
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/110136fb22341d3ba646/test_coverage)](https://codeclimate.com/github/DFE-Digital/govuk-formbuilder/test_coverage)
8
+ [![GitHub license](https://img.shields.io/github/license/DFE-Digital/govuk_design_system_formbuilder)](https://github.com/DFE-Digital/govuk-formbuilder/blob/master/LICENSE)
9
+ [![GOV.UK Design System Version](https://img.shields.io/badge/GOV.UK%20Design%20System-3.14.0-brightgreen)](https://design-system.service.gov.uk)
10
+ [![Rails](https://img.shields.io/badge/Ruby-2.6.8%20%E2%95%B1%202.7.4%20%E2%95%B1%203.0.2-E16D6D)](https://www.ruby-lang.org/en/downloads/)
12
11
  [![Ruby](https://img.shields.io/badge/Rails-6.0.4%20%E2%95%B1%206.1.3.2-E16D6D)](https://weblog.rubyonrails.org/releases/)
13
12
 
14
13
  This library provides an easy-to-use form builder for the [GOV.UK Design System](https://design-system.service.gov.uk/).
@@ -129,6 +128,21 @@ To help keep the logs clean and tidy, please configure git to use your full name
129
128
  git config --global user.name "Julius Hibbert"
130
129
  ```
131
130
 
131
+ ## Services using this library
132
+
133
+ Approximately [40 services use this library](https://github.com/DFE-Digital/govuk-formbuilder/network/dependents),
134
+ here are a few from the <abbr title="Department for Education">DfE</abbr>, <abbr title="Ministry of Justice">MoJ</abbr>, and
135
+ <abbr title="Department for Business, Energy & Industrial Strategy">BEIS</abbr>.
136
+
137
+ * [Apply for teacher training](https://www.github.com/DFE-Digital/apply-for-teacher-training)
138
+ * [Teaching Vacancies](https://www.github.com/DFE-Digital/teaching-vacancies)
139
+ * [Get a teacher training adviser](https://www.github.com/DFE-Digital/get-teacher-training-adviser-service/)
140
+ * [Claim for crown court defence](https://www.github.com/ministryofjustice/Claim-for-Crown-Court-Defence)
141
+ * [Appeal to the tax tribunal](https://www.github.com/ministryofjustice/tax-tribunals-datacapture)
142
+ * [Apply to court about child arrangements](https://www.github.com/ministryofjustice/c100-application)
143
+ * [Trade Tariff duty calculator](https://www.github.com/trade-tariff/trade-tariff-duty-calculator)
144
+ * [Report your official development assistance](https://www.github.com/UKGovernmentBEIS/beis-report-official-development-assistance)
145
+
132
146
  ## Thanks 👩🏽‍⚖️
133
147
 
134
148
  This project was inspired by [Ministry of Justice's GovukElementsFormBuilder](https://github.com/ministryofjustice/govuk_elements_form_builder),
@@ -57,7 +57,7 @@ module GOVUKDesignSystemFormBuilder
57
57
  @builder.object.errors.messages[@attribute_name].present?
58
58
  end
59
59
 
60
- def described_by(*ids)
60
+ def combine_references(*ids)
61
61
  ids.flatten.compact
62
62
  end
63
63
 
@@ -471,9 +471,9 @@ module GOVUKDesignSystemFormBuilder
471
471
  # @note Unlike the Rails +#collection_radio_buttons+ helper, this version can also insert
472
472
  # hints per item in the collection by supplying a +:hint_method+
473
473
  #
474
- # @note +:bold_labels+, while false by default, is set to true when a
475
- # +:hint_method+ is provided. This is done to make the label stand out more
476
- # from the hint.
474
+ # @note +:bold_labels+, is +nil+ (falsy) by default. When a +:hint_method+
475
+ # is provided it will become +true+ to make the label stand out more
476
+ # from the hint. The choice can be overridden with +true+ or +false+.
477
477
  #
478
478
  # @param attribute_name [Symbol] The name of the attribute
479
479
  # @param collection [Enumerable<Object>] Options to be added to the +select+ element
@@ -538,7 +538,7 @@ module GOVUKDesignSystemFormBuilder
538
538
  # :name,
539
539
  # legend: -> { tag.h3('Which category do you belong to?') }
540
540
  #
541
- def govuk_collection_radio_buttons(attribute_name, collection, value_method, text_method = nil, hint_method = nil, hint: {}, legend: {}, caption: {}, inline: false, small: false, bold_labels: false, classes: nil, include_hidden: config.default_collection_radio_buttons_include_hidden, form_group: {}, &block)
541
+ def govuk_collection_radio_buttons(attribute_name, collection, value_method, text_method = nil, hint_method = nil, hint: {}, legend: {}, caption: {}, inline: false, small: false, bold_labels: nil, classes: nil, include_hidden: config.default_collection_radio_buttons_include_hidden, form_group: {}, &block)
542
542
  Elements::Radios::Collection.new(
543
543
  self,
544
544
  object_name,
@@ -951,6 +951,13 @@ module GOVUKDesignSystemFormBuilder
951
951
  # which they were defined on the model).
952
952
  # @option kwargs [Hash] kwargs additional arguments are applied as attributes to the error summary +div+ element
953
953
  # @param block [Block] arbitrary HTML that will be rendered between title and error message list
954
+ # @param presenter [Class,Object] the class or object that is responsible for formatting a list of error
955
+ # messages that will be rendered in the summary.
956
+ #
957
+ # * When a class is specified it will be instantiated with the object's errors in the +object.errors.messages+ format.
958
+ # * When an object is specified it will be used as-is.
959
+ #
960
+ # The object must implement +#formatted_error_messages+, see {Presenters::ErrorSummaryPresenter} for more details.
954
961
  #
955
962
  # @note Only the first error in the +#errors+ array for each attribute will
956
963
  # be included.
@@ -959,8 +966,8 @@ module GOVUKDesignSystemFormBuilder
959
966
  # = f.govuk_error_summary 'Uh-oh, spaghettios'
960
967
  #
961
968
  # @see https://design-system.service.gov.uk/components/error-summary/ GOV.UK error summary
962
- def govuk_error_summary(title = config.default_error_summary_title, link_base_errors_to: nil, order: nil, **kwargs, &block)
963
- Elements::ErrorSummary.new(self, object_name, title, link_base_errors_to: link_base_errors_to, order: order, **kwargs, &block).html
969
+ def govuk_error_summary(title = config.default_error_summary_title, presenter: config.default_error_summary_presenter, link_base_errors_to: nil, order: nil, **kwargs, &block)
970
+ Elements::ErrorSummary.new(self, object_name, title, link_base_errors_to: link_base_errors_to, order: order, presenter: presenter, **kwargs, &block).html
964
971
  end
965
972
 
966
973
  # Generates a fieldset containing the contents of the block
@@ -22,7 +22,7 @@ module GOVUKDesignSystemFormBuilder
22
22
  end
23
23
 
24
24
  def classes
25
- [%(#{brand}-checkboxes), small_class, custom_classes].flatten.compact
25
+ combine_references(%(#{brand}-checkboxes), small_class, custom_classes)
26
26
  end
27
27
 
28
28
  def small_class
@@ -5,6 +5,8 @@ module GOVUKDesignSystemFormBuilder
5
5
  include Traits::Hint
6
6
 
7
7
  def initialize(builder, object_name, attribute_name, hint:, legend:, caption:, small:, classes:, form_group:, multiple:, &block)
8
+ fail LocalJumpError, 'no block given' unless block_given?
9
+
8
10
  super(builder, object_name, attribute_name, &block)
9
11
 
10
12
  @legend = legend
@@ -14,7 +16,6 @@ module GOVUKDesignSystemFormBuilder
14
16
  @classes = classes
15
17
  @form_group = form_group
16
18
  @multiple = multiple
17
- @block_content = capture { block.call }
18
19
  end
19
20
 
20
21
  def html
@@ -8,7 +8,7 @@ module GOVUKDesignSystemFormBuilder
8
8
 
9
9
  @legend = legend
10
10
  @caption = caption
11
- @described_by = described_by(described_by)
11
+ @described_by = combine_references(described_by)
12
12
  @attribute_name = attribute_name
13
13
  @html_attributes = kwargs
14
14
  end
@@ -15,7 +15,7 @@ module GOVUKDesignSystemFormBuilder
15
15
  private
16
16
 
17
17
  def classes
18
- [form_group_class, error_class, custom_classes].flatten.compact
18
+ combine_references(form_group_class, error_class, custom_classes)
19
19
  end
20
20
 
21
21
  def form_group_class
@@ -5,7 +5,9 @@ module GOVUKDesignSystemFormBuilder
5
5
  include Traits::Error
6
6
 
7
7
  def initialize(builder, object_name, attribute_name, hint:, legend:, caption:, inline:, small:, classes:, form_group:, &block)
8
- super(builder, object_name, attribute_name)
8
+ fail LocalJumpError, 'no block given' unless block_given?
9
+
10
+ super(builder, object_name, attribute_name, &block)
9
11
 
10
12
  @inline = inline
11
13
  @small = small
@@ -14,7 +16,6 @@ module GOVUKDesignSystemFormBuilder
14
16
  @hint = hint
15
17
  @classes = classes
16
18
  @form_group = form_group
17
- @block_content = capture { block.call }
18
19
  end
19
20
 
20
21
  def html
@@ -25,7 +25,7 @@ module GOVUKDesignSystemFormBuilder
25
25
  end
26
26
 
27
27
  def classes
28
- [%(#{brand}-radios), inline_class, small_class, custom_classes].flatten.compact
28
+ combine_references(%(#{brand}-radios), inline_class, small_class, custom_classes)
29
29
  end
30
30
 
31
31
  def inline_class
@@ -8,7 +8,7 @@ module GOVUKDesignSystemFormBuilder
8
8
  include Traits::FieldsetItem
9
9
 
10
10
  def initialize(builder, object_name, attribute_name, value, unchecked_value, label:, hint:, link_errors:, multiple:, exclusive:, **kwargs, &block)
11
- super(builder, object_name, attribute_name)
11
+ super(builder, object_name, attribute_name, &block)
12
12
 
13
13
  @value = value
14
14
  @unchecked_value = unchecked_value
@@ -19,7 +19,7 @@ module GOVUKDesignSystemFormBuilder
19
19
  @html_attributes = kwargs
20
20
  @exclusive = exclusive
21
21
 
22
- conditional_content(&block)
22
+ conditional_content(@block_content)
23
23
  end
24
24
 
25
25
  private
@@ -16,7 +16,7 @@ module GOVUKDesignSystemFormBuilder
16
16
 
17
17
  def html
18
18
  @checkbox.label(for: field_id(link_errors: @link_errors), class: label_classes) do
19
- [localised_text(:label), @checkbox.text, @value].compact.first.to_s
19
+ label_content.to_s
20
20
  end
21
21
  end
22
22
 
@@ -25,6 +25,10 @@ module GOVUKDesignSystemFormBuilder
25
25
  def label_classes
26
26
  %w(label checkboxes__label).prefix(brand)
27
27
  end
28
+
29
+ def label_content
30
+ [localised_text(:label), @checkbox.text, @value].find(&:presence)
31
+ end
28
32
  end
29
33
  end
30
34
  end
@@ -40,7 +40,7 @@ module GOVUKDesignSystemFormBuilder
40
40
  {
41
41
  id: field_id(link_errors: true),
42
42
  class: classes,
43
- aria: { describedby: described_by(hint_id, error_id, supplemental_id) }
43
+ aria: { describedby: combine_references(hint_id, error_id, supplemental_id) }
44
44
  }
45
45
  end
46
46
 
@@ -4,13 +4,14 @@ module GOVUKDesignSystemFormBuilder
4
4
  include Traits::Error
5
5
  include Traits::HTMLAttributes
6
6
 
7
- def initialize(builder, object_name, title, link_base_errors_to:, order:, **kwargs, &block)
7
+ def initialize(builder, object_name, title, link_base_errors_to:, order:, presenter:, **kwargs, &block)
8
8
  super(builder, object_name, nil, &block)
9
9
 
10
10
  @title = title
11
11
  @link_base_errors_to = link_base_errors_to
12
12
  @html_attributes = kwargs
13
13
  @order = order
14
+ @presenter = presenter
14
15
  end
15
16
 
16
17
  def html
@@ -33,7 +34,17 @@ module GOVUKDesignSystemFormBuilder
33
34
 
34
35
  def list
35
36
  tag.ul(class: [%(#{brand}-list), summary_class('list')]) do
36
- safe_join(error_messages.map { |attribute, messages| list_item(attribute, messages.first) })
37
+ safe_join(presenter.formatted_error_messages.map { |args| list_item(*args) })
38
+ end
39
+ end
40
+
41
+ # If the provided @presenter is a class, instantiate it with the sorted
42
+ # error_messages from our object. Otherwise (if it's any other object),
43
+ # treat it like a presenter
44
+ def presenter
45
+ (@presenter.is_a?(Class) ? @presenter.new(error_messages) : @presenter).tap do |p|
46
+ fail(ArgumentError, "error summary presenter doesn't implement #formatted_error_messages") unless
47
+ p.respond_to?(:formatted_error_messages)
37
48
  end
38
49
  end
39
50
 
@@ -114,7 +125,7 @@ module GOVUKDesignSystemFormBuilder
114
125
  module: %(#{brand}-error-summary)
115
126
  },
116
127
  aria: {
117
- labelledby: summary_title_id
128
+ labelledby: [summary_title_id.presence]
118
129
  }
119
130
  }
120
131
  end
@@ -35,7 +35,7 @@ module GOVUKDesignSystemFormBuilder
35
35
  {
36
36
  id: field_id(link_errors: true),
37
37
  class: classes,
38
- aria: { describedby: described_by(hint_id, error_id, supplemental_id) }
38
+ aria: { describedby: combine_references(hint_id, error_id, supplemental_id) }
39
39
  }
40
40
  end
41
41
 
@@ -50,7 +50,7 @@ module GOVUKDesignSystemFormBuilder
50
50
  end
51
51
 
52
52
  def retrieve_text(option_text, hidden)
53
- text = [option_text, localised_text(:label), @attribute_name.capitalize].compact.first
53
+ text = [option_text, localised_text(:label), @attribute_name.capitalize].find(&:presence)
54
54
 
55
55
  if hidden
56
56
  tag.span(text, class: %(#{brand}-visually-hidden))
@@ -45,7 +45,7 @@ module GOVUKDesignSystemFormBuilder
45
45
  end
46
46
 
47
47
  def retrieve_text(supplied_text)
48
- [supplied_text, localised_text(:legend), @attribute_name&.capitalize].compact.first
48
+ [supplied_text, localised_text(:legend), @attribute_name&.capitalize].find(&:presence)
49
49
  end
50
50
 
51
51
  def classes
@@ -21,7 +21,11 @@ module GOVUKDesignSystemFormBuilder
21
21
  @classes = classes
22
22
  @form_group = form_group
23
23
  @include_hidden = include_hidden
24
- @bold_labels = hint_method.present? || bold_labels
24
+ @bold_labels = if bold_labels.nil?
25
+ hint_method.present?
26
+ else
27
+ bold_labels
28
+ end
25
29
  end
26
30
 
27
31
  def html
@@ -8,7 +8,7 @@ module GOVUKDesignSystemFormBuilder
8
8
  include Traits::FieldsetItem
9
9
 
10
10
  def initialize(builder, object_name, attribute_name, value, label:, hint:, link_errors:, **kwargs, &block)
11
- super(builder, object_name, attribute_name)
11
+ super(builder, object_name, attribute_name, &block)
12
12
 
13
13
  @value = value
14
14
  @label = label
@@ -16,7 +16,7 @@ module GOVUKDesignSystemFormBuilder
16
16
  @link_errors = has_errors? && link_errors
17
17
  @html_attributes = kwargs
18
18
 
19
- conditional_content(&block)
19
+ conditional_content(@block_content)
20
20
  end
21
21
 
22
22
  private
@@ -38,7 +38,7 @@ module GOVUKDesignSystemFormBuilder
38
38
  {
39
39
  id: field_id(link_errors: true),
40
40
  class: classes,
41
- aria: { describedby: described_by(hint_id, error_id) }
41
+ aria: { describedby: combine_references(hint_id, error_id) }
42
42
  }
43
43
  end
44
44
  end
@@ -53,7 +53,7 @@ module GOVUKDesignSystemFormBuilder
53
53
  id: field_id(link_errors: true),
54
54
  class: classes,
55
55
  rows: @rows,
56
- aria: { describedby: described_by(hint_id, error_id, supplemental_id, limit_description_id) },
56
+ aria: { describedby: combine_references(hint_id, error_id, supplemental_id, limit_description_id) },
57
57
  }
58
58
  end
59
59
 
@@ -0,0 +1,29 @@
1
+ module Presenters
2
+ # This is the default presenter for {GOVUKDesignSystemFormBuilder::Elements::ErrorSummary} and is
3
+ # intended to be easily replaceable should you have specific requirements that aren't met here.
4
+ #
5
+ # The basic behaviour is to always show the first error message. In Rails, error message order is
6
+ # determined by the order in which the validations run, but if you need to do any other transformation
7
+ # or concatenation, this is the place to do it.
8
+ class ErrorSummaryPresenter
9
+ # @param [Hash] error_messages the error message hash in a format that matches Rails'
10
+ # `object.errors.messages`, so the format should be:
11
+ #
12
+ # @example Input format:
13
+ # ErrorSummaryPresenter.new({ attribute_one: ["first error", "second error"], attribute_two: ["third error"] })
14
+ def initialize(error_messages)
15
+ @error_messages = error_messages
16
+ end
17
+
18
+ # Converts +@error_messages+ into an array of argument arrays that will be
19
+ # passed into {GOVUKDesignSystemFormBuilder::Elements::ErrorSummary#list_item}.
20
+ #
21
+ # @return [Array<Array(Symbol, String)>] array of attribute and message arrays
22
+ #
23
+ # @example Output format given the input above:
24
+ # [[:attribute_one, "first error"], [:attribute_two, "third error"]]
25
+ def formatted_error_messages
26
+ @error_messages.map { |attribute, messages| [attribute, messages.first] }
27
+ end
28
+ end
29
+ end
@@ -4,7 +4,7 @@ module GOVUKDesignSystemFormBuilder
4
4
  using PrefixableArray
5
5
 
6
6
  def html
7
- safe_join([item, @conditional])
7
+ safe_join([item, @conditional_content])
8
8
  end
9
9
 
10
10
  private
@@ -24,7 +24,7 @@ module GOVUKDesignSystemFormBuilder
24
24
  id: field_id(link_errors: @link_errors),
25
25
  class: classes,
26
26
  multiple: @multiple,
27
- aria: { describedby: [hint_id] },
27
+ aria: { describedby: [hint_id.presence] },
28
28
  data: { 'aria-controls' => @conditional_id }
29
29
  }
30
30
  end
@@ -61,9 +61,9 @@ module GOVUKDesignSystemFormBuilder
61
61
  build_id('conditional')
62
62
  end
63
63
 
64
- def conditional_content(&block)
65
- if (conditional_block_content = block_given? && (capture { block.call }).presence)
66
- @conditional = conditional_container(conditional_block_content)
64
+ def conditional_content(block_content)
65
+ if block_content.present?
66
+ @conditional_content = conditional_container(block_content)
67
67
  @conditional_id = conditional_id
68
68
  end
69
69
  end
@@ -4,13 +4,19 @@ module GOVUKDesignSystemFormBuilder
4
4
  # Attributes eases working with default and custom attributes by:
5
5
  # * deeply merging them so both the default (required) attributes are
6
6
  # present
7
- # * joins the arrays into strings to maintain Rails 6.0.3 compatibility
7
+ # * joins the arrays into strings to maintain Rails 6.0.* compatibility
8
8
  class Attributes
9
- # Rather than attempt to combine these attributes, just overwrite the
10
- # form internally-generated values with those that are passed in. This
11
- # prevents the merge/unique value logic from affecting the content
12
- # (i.e. by remvoving duplicated words).
13
- UNMERGEABLE = [%i(id), %i(value), %i(title), %i(alt), %i(href), %i(aria label)].freeze
9
+ # Only try to combine and merge these attributes that contain a list of
10
+ # values separated by a space. All other values should be merged in a
11
+ # regular fashion (where the custom value overrides the default)
12
+ MERGEABLE = [
13
+ %i(class),
14
+ %i(aria controls),
15
+ %i(aria describedby),
16
+ %i(aria flowto),
17
+ %i(aria labelledby),
18
+ %i(aria owns),
19
+ ].freeze
14
20
 
15
21
  def initialize(defaults, custom)
16
22
  @merged = defaults.deeper_merge(deep_split_values(custom))
@@ -36,7 +42,7 @@ module GOVUKDesignSystemFormBuilder
36
42
  end
37
43
 
38
44
  def split_mergeable(key, value, parent = nil)
39
- return value if [parent, key].compact.in?(UNMERGEABLE)
45
+ return value.presence unless [parent, key].compact.in?(MERGEABLE)
40
46
 
41
47
  value.split
42
48
  end
@@ -48,7 +48,7 @@ module GOVUKDesignSystemFormBuilder
48
48
  {
49
49
  id: field_id(link_errors: true),
50
50
  class: classes,
51
- aria: { describedby: described_by(hint_id, error_id, supplemental_id) }
51
+ aria: { describedby: combine_references(hint_id, error_id, supplemental_id) }
52
52
  }
53
53
  end
54
54
 
@@ -4,7 +4,7 @@ module GOVUKDesignSystemFormBuilder
4
4
  private
5
5
 
6
6
  def classes
7
- [%(#{brand}-select), error_class].flatten.compact
7
+ combine_references(%(#{brand}-select), error_class)
8
8
  end
9
9
 
10
10
  def error_class
@@ -1,3 +1,3 @@
1
1
  module GOVUKDesignSystemFormBuilder
2
- VERSION = '2.7.1'.freeze
2
+ VERSION = '2.7.5'.freeze
3
3
  end
@@ -1,7 +1,7 @@
1
1
  require 'deep_merge/rails_compat'
2
2
  require 'active_support/configurable'
3
3
 
4
- [%w(refinements *.rb), %w(traits *.rb), %w(*.rb), %w(elements ** *.rb), %w(containers ** *.rb)]
4
+ [%w(presenters *.rb), %w(refinements *.rb), %w(traits *.rb), %w(*.rb), %w(elements ** *.rb), %w(containers ** *.rb)]
5
5
  .flat_map { |matcher| Dir.glob(File.join(__dir__, 'govuk_design_system_formbuilder', *matcher)) }
6
6
  .each { |file| require file }
7
7
 
@@ -37,16 +37,19 @@ module GOVUKDesignSystemFormBuilder
37
37
  # button in radio button fieldsets. As per the GOV.UK Design System spec,
38
38
  # it defaults to 'or'.
39
39
  #
40
- # * +:default_error_summary_title+ sets the text used in error summary
41
- # blocks. As per the GOV.UK Design System spec, it defaults to
42
- # 'There is a problem'.
43
- #
44
40
  # * +:default_collection_check_boxes_include_hidden+ controls whether or not
45
41
  # a hidden field is added when rendering a collection of check boxes
46
42
  #
47
43
  # * +:default_collection_radio_buttons_include_hidden+ controls whether or not
48
44
  # a hidden field is added when rendering a collection of radio buttons
49
45
  #
46
+ # * +:default_error_summary_title+ sets the text used in error summary
47
+ # blocks. As per the GOV.UK Design System spec, it defaults to
48
+ # 'There is a problem'.
49
+ #
50
+ # * +:default_error_summary_presenter+ the class that's instantiated when
51
+ # rendering an error summary and formats the messages for each attribute
52
+ #
50
53
  # * +:default_error_summary_error_order_method+ is the method that the library
51
54
  # will check for on the bound object to see whether or not to try ordering the
52
55
  # error messages
@@ -73,6 +76,7 @@ module GOVUKDesignSystemFormBuilder
73
76
  default_radio_divider_text: 'or',
74
77
  default_check_box_divider_text: 'or',
75
78
  default_error_summary_title: 'There is a problem',
79
+ default_error_summary_presenter: Presenters::ErrorSummaryPresenter,
76
80
  default_error_summary_error_order_method: nil,
77
81
  default_collection_check_boxes_include_hidden: true,
78
82
  default_collection_radio_buttons_include_hidden: true,
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: 2.7.1
4
+ version: 2.7.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Yates
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-06-25 00:00:00.000000000 Z
11
+ date: 2021-10-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: deep_merge
@@ -72,14 +72,14 @@ dependencies:
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: 4.0.0
75
+ version: 4.1.0
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: 4.0.0
82
+ version: 4.1.0
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: pry
85
85
  requirement: !ruby/object:Gem::Requirement
@@ -274,14 +274,14 @@ dependencies:
274
274
  requirements:
275
275
  - - "~>"
276
276
  - !ruby/object:Gem::Version
277
- version: 0.21.1
277
+ version: 0.22.0
278
278
  type: :development
279
279
  prerelease: false
280
280
  version_requirements: !ruby/object:Gem::Requirement
281
281
  requirements:
282
282
  - - "~>"
283
283
  - !ruby/object:Gem::Version
284
- version: 0.21.1
284
+ version: 0.22.0
285
285
  description: A Rails form builder that generates form inputs adhering to the GOV.UK
286
286
  Design System
287
287
  email:
@@ -331,6 +331,7 @@ files:
331
331
  - lib/govuk_design_system_formbuilder/elements/select.rb
332
332
  - lib/govuk_design_system_formbuilder/elements/submit.rb
333
333
  - lib/govuk_design_system_formbuilder/elements/text_area.rb
334
+ - lib/govuk_design_system_formbuilder/presenters/error_summary.rb
334
335
  - lib/govuk_design_system_formbuilder/proxy.rb
335
336
  - lib/govuk_design_system_formbuilder/refinements/prefixable_array.rb
336
337
  - lib/govuk_design_system_formbuilder/traits/caption.rb
@@ -354,6 +355,7 @@ metadata:
354
355
  documentation_uri: https://www.rubydoc.info/gems/govuk_design_system_formbuilder/GOVUKDesignSystemFormBuilder/Builder
355
356
  homepage_uri: https://govuk-form-builder.netlify.app
356
357
  source_code_uri: https://github.com/DFE-Digital/govuk_design_system_formbuilder
358
+ github_repo: https://github.com/DFE-Digital/govuk_design_system_formbuilder
357
359
  post_install_message:
358
360
  rdoc_options: []
359
361
  require_paths: