govuk_design_system_formbuilder 0.9.8 → 2.0.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.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +40 -35
  3. data/lib/govuk_design_system_formbuilder/base.rb +21 -46
  4. data/lib/govuk_design_system_formbuilder/builder.rb +528 -111
  5. data/lib/govuk_design_system_formbuilder/containers/character_count.rb +8 -7
  6. data/lib/govuk_design_system_formbuilder/containers/check_boxes.rb +20 -8
  7. data/lib/govuk_design_system_formbuilder/containers/check_boxes_fieldset.rb +39 -14
  8. data/lib/govuk_design_system_formbuilder/containers/fieldset.rb +21 -25
  9. data/lib/govuk_design_system_formbuilder/containers/form_group.rb +20 -9
  10. data/lib/govuk_design_system_formbuilder/containers/radio_buttons_fieldset.rb +28 -14
  11. data/lib/govuk_design_system_formbuilder/containers/radios.rb +27 -10
  12. data/lib/govuk_design_system_formbuilder/containers/supplemental.rb +8 -9
  13. data/lib/govuk_design_system_formbuilder/elements/caption.rb +41 -0
  14. data/lib/govuk_design_system_formbuilder/elements/check_boxes/collection.rb +34 -22
  15. data/lib/govuk_design_system_formbuilder/elements/check_boxes/collection_check_box.rb +28 -18
  16. data/lib/govuk_design_system_formbuilder/elements/check_boxes/fieldset_check_box.rb +47 -40
  17. data/lib/govuk_design_system_formbuilder/elements/check_boxes/label.rb +9 -3
  18. data/lib/govuk_design_system_formbuilder/elements/date.rb +65 -53
  19. data/lib/govuk_design_system_formbuilder/elements/error_message.rb +13 -8
  20. data/lib/govuk_design_system_formbuilder/elements/error_summary.rb +30 -31
  21. data/lib/govuk_design_system_formbuilder/elements/file.rb +31 -24
  22. data/lib/govuk_design_system_formbuilder/elements/hint.rb +50 -14
  23. data/lib/govuk_design_system_formbuilder/elements/inputs/email.rb +8 -0
  24. data/lib/govuk_design_system_formbuilder/elements/inputs/number.rb +6 -0
  25. data/lib/govuk_design_system_formbuilder/elements/inputs/password.rb +17 -0
  26. data/lib/govuk_design_system_formbuilder/elements/inputs/phone.rb +6 -0
  27. data/lib/govuk_design_system_formbuilder/elements/inputs/text.rb +6 -0
  28. data/lib/govuk_design_system_formbuilder/elements/inputs/url.rb +6 -0
  29. data/lib/govuk_design_system_formbuilder/elements/label.rb +63 -34
  30. data/lib/govuk_design_system_formbuilder/elements/legend.rb +87 -0
  31. data/lib/govuk_design_system_formbuilder/elements/radios/collection.rb +51 -35
  32. data/lib/govuk_design_system_formbuilder/elements/radios/collection_radio_button.rb +41 -19
  33. data/lib/govuk_design_system_formbuilder/elements/radios/fieldset_radio_button.rb +37 -24
  34. data/lib/govuk_design_system_formbuilder/elements/select.rb +34 -32
  35. data/lib/govuk_design_system_formbuilder/elements/submit.rb +39 -29
  36. data/lib/govuk_design_system_formbuilder/elements/text_area.rb +76 -32
  37. data/lib/govuk_design_system_formbuilder/traits/caption.rb +15 -0
  38. data/lib/govuk_design_system_formbuilder/traits/collection_item.rb +16 -0
  39. data/lib/govuk_design_system_formbuilder/traits/conditional.rb +17 -0
  40. data/lib/govuk_design_system_formbuilder/traits/error.rb +17 -0
  41. data/lib/govuk_design_system_formbuilder/traits/hint.rb +30 -0
  42. data/lib/govuk_design_system_formbuilder/traits/input.rb +105 -0
  43. data/lib/govuk_design_system_formbuilder/traits/label.rb +22 -0
  44. data/lib/govuk_design_system_formbuilder/traits/localisation.rb +51 -0
  45. data/lib/govuk_design_system_formbuilder/traits/supplemental.rb +17 -0
  46. data/lib/govuk_design_system_formbuilder/version.rb +1 -1
  47. data/lib/govuk_design_system_formbuilder.rb +89 -44
  48. metadata +62 -52
  49. data/lib/govuk_design_system_formbuilder/elements/check_boxes/hint.rb +0 -30
  50. data/lib/govuk_design_system_formbuilder/elements/inputs/base.rb +0 -79
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 34191822b2322a27884781b5fdc5d494385f0386b349c8253c213f76cef55600
4
- data.tar.gz: dc0d69655c5d8bcbeaa967b852566abec1b821ad9ef43a6f0a4bc65afafc841a
3
+ metadata.gz: f87c2ecd9fbd0615a3061159d7e87b42254115db22f59c260a6e4209f7ff742d
4
+ data.tar.gz: a167bde412b2158d9ed303af32e730d5ae27876b1ac4ee80d675b9bc81d15126
5
5
  SHA512:
6
- metadata.gz: 21f8a1d3f15d4de5f01bb54804fbb700ca902f6cb7dee40ab686b500d3d9133f2c3983b48fb4ee040ffcb24c19d03c5511736dc9ca732d3204caeb45ad185e20
7
- data.tar.gz: 154333ebb09be62fd38a1d4c0b3449d0a140d2c732e1010f2a164b095f93cd1d67c6870fbf6908729cc8a78cc02ea131f67677f685cef680a276611706b1b2b6
6
+ metadata.gz: 4d184b3fa9ed93c35ea9ef15a72bf79f3520632fd3f501d93cc089a2e4b4e73fb73c2078083a9e0785f812802f2b406c972b568033148c9875bf1c4906ee9122
7
+ data.tar.gz: 3462d38207e9220b41bc3bc899d5218bc9ef982e609e2c96f58abbe0d8d20d9e9bd515b1be4566f77b37a4b543e0929a02fd9fa18bb0001815067aa0e6b07ea9
data/README.md CHANGED
@@ -2,44 +2,42 @@
2
2
 
3
3
  [![Build Status](https://travis-ci.com/DFE-Digital/govuk_design_system_formbuilder.svg?branch=master)](https://travis-ci.com/DFE-Digital/govuk_design_system_formbuilder)
4
4
  [![Maintainability](https://api.codeclimate.com/v1/badges/fde73b5dc9476197281b/maintainability)](https://codeclimate.com/github/DFE-Digital/govuk_design_system_formbuilder/maintainability)
5
- [![Test Coverage](https://api.codeclimate.com/v1/badges/fde73b5dc9476197281b/test_coverage)](https://codeclimate.com/github/DFE-Digital/govuk_design_system_formbuilder/test_coverage)
6
5
  [![Gem Version](https://badge.fury.io/rb/govuk_design_system_formbuilder.svg)](https://badge.fury.io/rb/govuk_design_system_formbuilder)
7
- [![Netlify Status](https://api.netlify.com/api/v1/badges/d4c50b8d-6ca3-4797-9ab3-6e0731c72b44/deploy-status)](https://app.netlify.com/sites/govuk-form-builder/deploys)
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.9.1-brightgreen)](https://design-system.service.gov.uk)
8
11
 
9
- This gem provides a easy-to-use form builder that generates forms that are
10
- fully-compliant with version 3.3.0 of the [GOV.UK Design System](https://design-system.service.gov.uk/),
11
- minimising the amount of markup you need to write.
12
+ This library provides an easy-to-use form builder for the [GOV.UK Design System](https://design-system.service.gov.uk/).
12
13
 
13
- The latest version of this gem that supports GOV.UK Design System version 2 is
14
- [0.7.10](https://github.com/DFE-Digital/govuk_design_system_formbuilder/releases/tag/v0.7.10).
14
+ It is intended to make creating forms **quick**, **easy** and **familiar** for Ruby on Rails developers.
15
15
 
16
- In addition to the basic markup, the more-advanced functionality of the Design
17
- System is exposed via the API. Adding [JavaScript-enhanced word count
18
- checking](https://govuk-form-builder.netlify.com/form-elements/text-area/)
19
- to text areas or [setting the size and weight of
20
- labels](https://govuk-form-builder.netlify.com/introduction/labels-hints-and-legends/)
21
- on text fields requires only a single argument.
16
+ ## Documentation 📚
22
17
 
23
- If you're still not sure what a form builder is or how it works, don't worry!
24
- [This screencast](https://www.youtube.com/watch?v=PhoFZ0qXAlA) should give you
25
- an idea of what's on offer and [the official guide](https://govuk-form-builder.netlify.com) goes
26
- into a bit more depth on how everything works 😅
18
+ The gem comes with [a full guide](https://govuk-form-builder.netlify.app/) that
19
+ covers most aspects of day-to-day use, along with code and output examples. The
20
+ examples in the guide are generated from the builder itself so it will always
21
+ be up to date.
22
+
23
+ [![Netlify Status](https://api.netlify.com/api/v1/badges/d4c50b8d-6ca3-4797-9ab3-6e0731c72b44/deploy-status)](https://app.netlify.com/sites/govuk-form-builder/deploys)
27
24
 
28
- ## What's included
25
+ ## What's included 🧳
29
26
 
30
27
  * 100% compatibility with the GOV.UK Design System
31
- * Full control of labels, hints, fieldsets and legends
28
+ * Full control of labels, legends, hints, captions and fieldsets
32
29
  * No overwriting of Rails' default form helpers
33
30
  * Automatic ARIA associations between hints, errors and inputs
34
31
  * Most helpers take blocks for arbitrary content
35
32
  * Additional params for programmatically adding hints to check box and radio
36
33
  button collections
34
+ * Full I18n support
35
+ * Automatic handling of ActiveRecord validation error messages
37
36
  * No external dependencies
38
37
  * An exhaustive test suite
39
- * [Extensive documentation](https://www.rubydoc.info/gems/govuk_design_system_formbuilder/GOVUKDesignSystemFormBuilder/Builder)
40
- * [A full guide](https://govuk-form-builder.netlify.com/)
38
+ * [Extensive technical documentation](https://www.rubydoc.info/gems/govuk_design_system_formbuilder/GOVUKDesignSystemFormBuilder/Builder)
41
39
 
42
- ## Installation
40
+ ## Installation 🏗
43
41
 
44
42
  You can install the form builder gem by running the `gem install
45
43
  govuk_design_system_formbuilder` or by adding the following line
@@ -59,10 +57,11 @@ pre-configured template:
59
57
  * [DfE Boilerplate](https://github.com/DFE-Digital/govuk-rails-boilerplate)
60
58
  * [MoJ Rails Template](https://github.com/ministryofjustice/moj_rails_template)
61
59
 
62
- ## Setup
60
+ ## Setup 🔧
63
61
 
64
- To use the form builder in an ad hoc basis you can specify it
65
- as an argument to `form_for` or `form_with`:
62
+ To use the form builder in an ad hoc basis you can specify it as an argument to
63
+ `form_for` or `form_with`. These examples are written in [Slim](https://slim-lang.com) but
64
+ other templating languages like ERB and [Haml](https://haml.info/) work just as well.
66
65
 
67
66
  ```slim
68
67
  = form_for @some_object, builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f|
@@ -77,7 +76,7 @@ class ApplicationController < ActionController::Base
77
76
  end
78
77
  ```
79
78
 
80
- Now we can get started! 🎉
79
+ Now we can get started!
81
80
 
82
81
  ```slim
83
82
  = form_for @person do |f|
@@ -92,16 +91,16 @@ Now we can get started! 🎉
92
91
  :name,
93
92
  :description,
94
93
  label: { text: "Which department do you work for?" },
95
- hint_text: "If you don't know ask your manager" }
94
+ hint: { text: "If you don't know ask your manager" }
96
95
 
97
96
  = f.govuk_submit 'Away we go!'
98
97
  ```
99
98
 
100
- ## Developing and running the tests
99
+ ## Developing and running the tests 👨🏻‍🏭
101
100
 
102
- The form builder is covered by RSpec, to run all the tests first ensure that
103
- all of the development and testing prerequisite gems are installed. At the root
104
- of a freshly-cloned repo run:
101
+ The form builder is tested with RSpec. To run all the tests first ensure that
102
+ the development and testing prerequisite gems are installed. At the root of a
103
+ freshly-cloned repo run:
105
104
 
106
105
  ```sh
107
106
  bundle
@@ -110,10 +109,10 @@ bundle
110
109
  Now, if everything was successful, run RSpec:
111
110
 
112
111
  ```sh
113
- bundle exec rspec -fd
112
+ bundle exec rspec
114
113
  ```
115
114
 
116
- ## Contributing
115
+ ## Contributing 📦
117
116
 
118
117
  Bug reports and feature requests are most welcome, please raise an issue or
119
118
  submit a pull request.
@@ -122,7 +121,13 @@ Currently we're using [GOVUK Lint](https://github.com/alphagov/govuk-lint) to
122
121
  ensure code meets the GOV.UK guidelines. Please ensure that any PRs also adhere
123
122
  to this standard.
124
123
 
125
- ## Thanks
124
+ To help keep the logs clean and tidy, please configure git to use your full name:
125
+
126
+ ```sh
127
+ git config --global user.name "Julius Hibbert"
128
+ ```
129
+
130
+ ## Thanks 👩🏽‍⚖️
126
131
 
127
- This project was inspired by [MoJ's GovukElementsFormBuilder](https://github.com/ministryofjustice/govuk_elements_form_builder),
132
+ This project was inspired by [Ministry of Justice's GovukElementsFormBuilder](https://github.com/ministryofjustice/govuk_elements_form_builder),
128
133
  but is leaner, more modular and written in a more idiomatic style.
@@ -1,28 +1,32 @@
1
1
  module GOVUKDesignSystemFormBuilder
2
+ module PrefixableArray
3
+ refine Array do
4
+ def prefix(text, delimiter: '-')
5
+ map { |item| text + delimiter + item }
6
+ end
7
+ end
8
+ end
9
+
2
10
  class Base
3
- delegate :capture, :content_tag, :safe_join, :tag, :raw, :link_to, to: :@builder
11
+ delegate :content_tag, :safe_join, :tag, :link_to, :capture, to: :@builder
12
+ delegate :config, to: GOVUKDesignSystemFormBuilder
4
13
 
5
14
  def initialize(builder, object_name, attribute_name, &block)
6
- @builder = builder
7
- @object_name = object_name
15
+ @builder = builder
16
+ @object_name = object_name
8
17
  @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)
18
+ @block_content = capture { block.call } if block_given?
14
19
  end
15
20
 
16
- def error_element
17
- @error_element ||= Elements::ErrorMessage.new(@builder, @object_name, @attribute_name)
21
+ # objects that implement #to_s can be passed directly into #safe_join
22
+ def to_s
23
+ html || ''
18
24
  end
19
25
 
20
- def label_element
21
- @label_element ||= Elements::Label.new(@builder, @object_name, @attribute_name, **@label)
22
- end
26
+ private
23
27
 
24
- def supplemental_content
25
- @supplemental_content ||= Containers::Supplemental.new(@builder, @object_name, @attribute_name, @block_content)
28
+ def brand(override = nil)
29
+ override || config.brand
26
30
  end
27
31
 
28
32
  # returns the id value used for the input
@@ -46,45 +50,16 @@ module GOVUKDesignSystemFormBuilder
46
50
  end
47
51
  end
48
52
 
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
53
  def has_errors?
72
- @builder.object.errors.any? &&
54
+ @builder.object.respond_to?(:errors) &&
55
+ @builder.object.errors.any? &&
73
56
  @builder.object.errors.messages.dig(@attribute_name).present?
74
57
  end
75
58
 
76
- def wrap_conditional(block)
77
- content_tag('div', class: conditional_classes, id: conditional_id) do
78
- capture { block.call }
79
- end
80
- end
81
-
82
59
  def described_by(*ids)
83
60
  ids.flatten.compact.join(' ').presence
84
61
  end
85
62
 
86
- private
87
-
88
63
  # Builds the values used for HTML id attributes throughout the builder
89
64
  #
90
65
  # @param id_type [String] a description of the id's type, eg +hint+, +error+