govuk_design_system_formbuilder 2.7.6 → 2.8.0

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: 16dd3a6e9641b3d0aa7e91ee6318bcd0f993d10c00105e1c977f54120eb966d9
4
- data.tar.gz: 5aa914a964cac5770d3e1c002aa9e38a20b4bf21c612a9c78c5edfa503c8e98f
3
+ metadata.gz: 84999693662af580363425f17c82d516d675c30d000ccd7e46a3d4607b63b32e
4
+ data.tar.gz: fe9b2a8f8cd05ec1eb56d915223a387ba4d86390dbee1e771f9c32263afd054a
5
5
  SHA512:
6
- metadata.gz: 870971ada7f130cd54a13cff52f852d07e1581dd8ebf04f802ae93ca2103325fa2e283e9812170c282da4c5b1a056490799828f0f8d374a7b8d92a053bc6114e
7
- data.tar.gz: 7e931f7fe3966909bdd2c4ff16ee3fe7d06e0c52925c03d36fb300a99d7105a842ee6f5aeac165b3824495efd7b3d189ac925378c4a10b7e57c37f8d4230eeb2
6
+ metadata.gz: f48bdf7f824047df1dccc6b53412cb454f3f30af9805ba5b6fafa91d89454e03ef929d534919b0695ca0db0804d84de18a01779074382d96e7c457c2e4a727cf
7
+ data.tar.gz: c5d24743fa044cf0b71b3b3609239c0787ed0551a6993f74f238385217782f4167439470070c4dd0f4d24b1bdcb1446cb6c996d3aef51f001c016dde55945800
@@ -912,6 +912,7 @@ module GOVUKDesignSystemFormBuilder
912
912
  # @option caption size [String] the size of the caption, can be +xl+, +l+ or +m+. Defaults to +m+
913
913
  # @option caption kwargs [Hash] additional arguments are applied as attributes on the caption +span+ element
914
914
  # @param omit_day [Boolean] do not render a day input, only capture month and year
915
+ # @param maxlength_enabled [Boolean] adds maxlength attribute to day, month and year inputs (2, 2, and 4, respectively)
915
916
  # @param wildcards [Boolean] add an 'X' to the day and month patterns so users can add approximate dates
916
917
  # @param form_group [Hash] configures the form group
917
918
  # @option form_group classes [Array,String] sets the form group's classes
@@ -936,8 +937,8 @@ module GOVUKDesignSystemFormBuilder
936
937
  # @example A date input with legend supplied as a proc
937
938
  # = f.govuk_date_field :finishes_on,
938
939
  # legend: -> { tag.h3('Which category do you belong to?') }
939
- def govuk_date_field(attribute_name, hint: {}, legend: {}, caption: {}, date_of_birth: false, omit_day: false, form_group: {}, wildcards: false, **kwargs, &block)
940
- Elements::Date.new(self, object_name, attribute_name, hint: hint, legend: legend, caption: caption, date_of_birth: date_of_birth, omit_day: omit_day, form_group: form_group, wildcards: wildcards, **kwargs, &block).html
940
+ def govuk_date_field(attribute_name, hint: {}, legend: {}, caption: {}, date_of_birth: false, omit_day: false, maxlength_enabled: false, form_group: {}, wildcards: false, **kwargs, &block)
941
+ Elements::Date.new(self, object_name, attribute_name, hint: hint, legend: legend, caption: caption, date_of_birth: date_of_birth, omit_day: omit_day, maxlength_enabled: maxlength_enabled, form_group: form_group, wildcards: wildcards, **kwargs, &block).html
941
942
  end
942
943
 
943
944
  # Generates a summary of errors in the form, each linking to the corresponding
@@ -6,21 +6,23 @@ module GOVUKDesignSystemFormBuilder
6
6
  include Traits::Error
7
7
  include Traits::Hint
8
8
  include Traits::Supplemental
9
+ include Traits::HTMLClasses
9
10
 
10
11
  SEGMENTS = { day: '3i', month: '2i', year: '1i' }.freeze
11
12
  MULTIPARAMETER_KEY = { day: 3, month: 2, year: 1 }.freeze
12
13
 
13
- def initialize(builder, object_name, attribute_name, legend:, caption:, hint:, omit_day:, form_group:, wildcards:, date_of_birth: false, **kwargs, &block)
14
+ def initialize(builder, object_name, attribute_name, legend:, caption:, hint:, omit_day:, maxlength_enabled:, form_group:, wildcards:, date_of_birth: false, **kwargs, &block)
14
15
  super(builder, object_name, attribute_name, &block)
15
16
 
16
- @legend = legend
17
- @caption = caption
18
- @hint = hint
19
- @date_of_birth = date_of_birth
20
- @omit_day = omit_day
21
- @form_group = form_group
22
- @wildcards = wildcards
23
- @html_attributes = kwargs
17
+ @legend = legend
18
+ @caption = caption
19
+ @hint = hint
20
+ @date_of_birth = date_of_birth
21
+ @omit_day = omit_day
22
+ @maxlength_enabled = maxlength_enabled
23
+ @form_group = form_group
24
+ @wildcards = wildcards
25
+ @html_attributes = kwargs
24
26
  end
25
27
 
26
28
  def html
@@ -47,6 +49,10 @@ module GOVUKDesignSystemFormBuilder
47
49
  @omit_day
48
50
  end
49
51
 
52
+ def maxlength_enabled?
53
+ @maxlength_enabled
54
+ end
55
+
50
56
  def day
51
57
  return if omit_day?
52
58
 
@@ -104,7 +110,8 @@ module GOVUKDesignSystemFormBuilder
104
110
  pattern: pattern(segment),
105
111
  inputmode: 'numeric',
106
112
  value: value,
107
- autocomplete: date_of_birth_autocomplete_value(segment)
113
+ autocomplete: date_of_birth_autocomplete_value(segment),
114
+ maxlength: (width if maxlength_enabled?),
108
115
  )
109
116
  end
110
117
 
@@ -115,10 +122,12 @@ module GOVUKDesignSystemFormBuilder
115
122
  end
116
123
 
117
124
  def classes(width)
118
- %w(input date-input__input).prefix(brand).tap do |classes|
119
- classes.push(%(#{brand}-input--width-#{width}))
120
- classes.push(%(#{brand}-input--error)) if has_errors?
121
- end
125
+ build_classes(
126
+ %(input),
127
+ %(date-input__input),
128
+ %(input--width-#{width}),
129
+ %(input--error) => has_errors?,
130
+ ).prefix(brand)
122
131
  end
123
132
 
124
133
  # if the field has errors we want the govuk_error_summary to
@@ -148,7 +157,7 @@ module GOVUKDesignSystemFormBuilder
148
157
  end
149
158
 
150
159
  def label_classes
151
- %w(label date-input__label).prefix(brand)
160
+ build_classes(%(label), %(date-input__label)).prefix(brand)
152
161
  end
153
162
  end
154
163
  end
@@ -20,7 +20,7 @@ module GOVUKDesignSystemFormBuilder
20
20
  end
21
21
 
22
22
  def message
23
- @builder.object.errors.messages[@attribute_name]&.first
23
+ set_message_safety(@builder.object.errors.messages[@attribute_name]&.first)
24
24
  end
25
25
  end
26
26
  end
@@ -81,7 +81,9 @@ module GOVUKDesignSystemFormBuilder
81
81
  end
82
82
 
83
83
  def list_item(attribute, message, url = nil)
84
- tag.li(link_to(message, url || same_page_link(field_id(attribute)), **link_options))
84
+ target = url || same_page_link(field_id(attribute))
85
+
86
+ tag.li(link_to(set_message_safety(message), target, **link_options))
85
87
  end
86
88
 
87
89
  def same_page_link(target)
@@ -8,6 +8,7 @@ module GOVUKDesignSystemFormBuilder
8
8
  include Traits::Label
9
9
  include Traits::Supplemental
10
10
  include Traits::HTMLAttributes
11
+ include Traits::HTMLClasses
11
12
 
12
13
  def initialize(builder, object_name, attribute_name, hint:, label:, caption:, form_group:, **kwargs, &block)
13
14
  super(builder, object_name, attribute_name, &block)
@@ -40,9 +41,7 @@ module GOVUKDesignSystemFormBuilder
40
41
  end
41
42
 
42
43
  def classes
43
- %w(file-upload).prefix(brand).tap do |c|
44
- c.push(%(#{brand}-file-upload--error)) if has_errors?
45
- end
44
+ build_classes(%(file-upload), %(file-upload--error) => has_errors?).prefix(brand)
46
45
  end
47
46
  end
48
47
  end
@@ -8,6 +8,7 @@ module GOVUKDesignSystemFormBuilder
8
8
  include Traits::Label
9
9
  include Traits::Supplemental
10
10
  include Traits::HTMLAttributes
11
+ include Traits::HTMLClasses
11
12
 
12
13
  def initialize(builder, object_name, attribute_name, hint:, label:, caption:, rows:, max_words:, max_chars:, threshold:, form_group:, **kwargs, &block)
13
14
  super(builder, object_name, attribute_name, &block)
@@ -42,10 +43,7 @@ module GOVUKDesignSystemFormBuilder
42
43
  end
43
44
 
44
45
  def classes
45
- %w(textarea).prefix(brand).tap do |classes|
46
- classes.push(%(#{brand}-textarea--error)) if has_errors?
47
- classes.push(%(#{brand}-js-character-count)) if limit?
48
- end
46
+ build_classes(%(textarea), %(textarea--error) => has_errors?, %(js-character-count) => limit?).prefix(brand)
49
47
  end
50
48
 
51
49
  def options
@@ -86,7 +84,7 @@ module GOVUKDesignSystemFormBuilder
86
84
  end
87
85
 
88
86
  def limit_description_classes
89
- %w(hint character-count__message).prefix(brand)
87
+ build_classes(%(hint), %(character-count__message)).prefix(brand)
90
88
  end
91
89
 
92
90
  def limit_description_id
@@ -12,6 +12,10 @@ module GOVUKDesignSystemFormBuilder
12
12
  def error_element
13
13
  @error_element ||= Elements::ErrorMessage.new(*bound)
14
14
  end
15
+
16
+ def set_message_safety(message)
17
+ config.trust_error_messages ? message.html_safe : message
18
+ end
15
19
  end
16
20
  end
17
21
  end
@@ -0,0 +1,36 @@
1
+ module GOVUKDesignSystemFormBuilder
2
+ module Traits
3
+ module HTMLClasses
4
+ # combine all the classes in *args with any keys from **kwargs
5
+ # where the value is true. Roughly based on the behaviour from
6
+ # Rails' class_names, but recreated here as that returns a string
7
+ # where we want an array.
8
+ def build_classes(*args, **kwargs)
9
+ # FIXME: we need to handle the arguments differently for Ruby 2.6.x because
10
+ # Ruby 2.7.0 brought the separation of positional and keyword
11
+ # arguments.
12
+ #
13
+ # This can be removed once support for 2.6.x is dropped.
14
+ #
15
+ # https://www.ruby-lang.org/en/news/2019/12/12/separation-of-positional-and-keyword-arguments-in-ruby-3-0/
16
+ return ruby_2_6_build_classes_fallback(*args) if RUBY_VERSION < "2.7.0"
17
+
18
+ (args + kwargs.map { |k, v| k if v }).compact
19
+ end
20
+
21
+ private
22
+
23
+ def ruby_2_6_build_classes_fallback(*args)
24
+ [].tap do |classes|
25
+ args.each do |arg|
26
+ if arg.is_a?(Hash)
27
+ arg.each { |k, v| classes.append(k) if v }
28
+ else
29
+ classes.append(arg)
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -1,3 +1,3 @@
1
1
  module GOVUKDesignSystemFormBuilder
2
- VERSION = '2.7.6'.freeze
2
+ VERSION = '2.8.0'.freeze
3
3
  end
@@ -71,6 +71,10 @@ module GOVUKDesignSystemFormBuilder
71
71
  #
72
72
  # * +:enable_logger+ controls whether or not the library will emit log
73
73
  # messages via Rails.logger.warn, defaults to +true+
74
+ #
75
+ # * +:trust_error_messages+ call html_safe on error messages before they're
76
+ # rendered. This allows formatting markup to be used to change the display
77
+ # of the error message. Defaults to +false+
74
78
  # ===
75
79
  DEFAULTS = {
76
80
  brand: 'govuk',
@@ -95,7 +99,8 @@ module GOVUKDesignSystemFormBuilder
95
99
  localisation_schema_legend: nil,
96
100
  localisation_schema_caption: nil,
97
101
 
98
- enable_logger: true
102
+ enable_logger: true,
103
+ trust_error_messages: false,
99
104
  }.freeze
100
105
 
101
106
  DEFAULTS.each_key { |k| config_accessor(k) { DEFAULTS[k] } }
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.6
4
+ version: 2.8.0
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-11-17 00:00:00.000000000 Z
11
+ date: 2021-12-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: deep_merge
@@ -340,6 +340,7 @@ files:
340
340
  - lib/govuk_design_system_formbuilder/traits/fieldset_item.rb
341
341
  - lib/govuk_design_system_formbuilder/traits/hint.rb
342
342
  - lib/govuk_design_system_formbuilder/traits/html_attributes.rb
343
+ - lib/govuk_design_system_formbuilder/traits/html_classes.rb
343
344
  - lib/govuk_design_system_formbuilder/traits/input.rb
344
345
  - lib/govuk_design_system_formbuilder/traits/label.rb
345
346
  - lib/govuk_design_system_formbuilder/traits/localisation.rb