govuk_design_system_formbuilder 2.7.2 → 2.7.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 06a3aa374a6a6fa23d1fb52cdb7585fe50e067580328683b5dcbbcd8c6444aab
4
- data.tar.gz: 15c814a5314cbf8aa97fd9e8d47c640062286d6a2239be89265015f6eafab172
3
+ metadata.gz: 8dde478d08a40d1cc871b7f0b2db7180db591ec384d4d7cc160ce9523605d8f2
4
+ data.tar.gz: f406bfbc2ac3b5eb5a5ce5c6cade4138459323a6a9fe9e8cbe23dcf33edcd9cb
5
5
  SHA512:
6
- metadata.gz: c4319d5fbd9168b1869aa94a1206f57c50f4e2d1a238a06da0bbcae0a55b82a020895ca1cfc59de86fdb9ee0248cbd283a3f5b22142946b9951a96906e870c9e
7
- data.tar.gz: eec0ec045b248bd16cf671b4d289ea91429338693c541c74c30d92d481b2f2a833c490b064ed58bce7e95a49af03725faa6cb58b0222f0a5411153d1673d4a89
6
+ metadata.gz: 112918eb62e62548f16a27cb8e3a33e353d4be9543819f366f88671e628faff90a4ec28fb49a4a76d0d8ed0136569db090a36091c1cf6e1e3e0f360e770939c4
7
+ data.tar.gz: ae6d8df17006b0a5d77eb6bd21d8402f53eca1087c34071c39534ca8e9753e33a6ca555f242eadf2a83e6eeaa4cbc92689e19458351851ba408cf4b419b656e3
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)
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)
10
9
  [![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/)
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),
@@ -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
@@ -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
@@ -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
 
@@ -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
@@ -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
  # 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
9
  # Rather than attempt to combine these attributes, just overwrite the
10
10
  # form internally-generated values with those that are passed in. This
@@ -1,3 +1,3 @@
1
1
  module GOVUKDesignSystemFormBuilder
2
- VERSION = '2.7.2'.freeze
2
+ VERSION = '2.7.3'.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.2
4
+ version: 2.7.3
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-07-01 00:00:00.000000000 Z
11
+ date: 2021-09-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: deep_merge
@@ -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