govuk_elements_rails 0.1.1 → 0.2.1

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
  SHA1:
3
- metadata.gz: 0d071bbff96fc040c1547979ae71864c74f8529b
4
- data.tar.gz: c3c2942a69785856cbafeea8da078a0081f72e8c
3
+ metadata.gz: 036d93e6fb397f955de23ed8adf114bc3b83b19a
4
+ data.tar.gz: 770cb5fb1d025ee758af29f64d02377410d6dd59
5
5
  SHA512:
6
- metadata.gz: 24e7ac1966e8b427518d6f6653c435d96d2927f8c24766cd8d9d796768d9ec6cf7f4457379dbba67e7d0fb87a55e85ab1a167e5f056e1934e632c839e174fe16
7
- data.tar.gz: d1b16bcf2af69b88ba374231e4e5fb7409b2e60c22b5cc94b07759b413654ea7e002d5c04e270667e709fe0dc03724758c5ad69e76c0027828fae4feaf8387e7
6
+ metadata.gz: 7c48cce45e3f80d92e84edd21456db964a73e3018e24d175e8cab56ff7d37a013ff26aaab41be857ce7fb83a8b217722699c98f675d0fe802c125ef0652411a5
7
+ data.tar.gz: aaea624fa297a9391ff7aad10b600ab65b200c9b51dc92741db5591c62c19bed291ea99fcdcbf46405ca241c1a5913d2c4a1bbbedb591983f9098b370774c934
data/README.md CHANGED
@@ -43,6 +43,13 @@ For example here are all the requires possible at present:
43
43
  //= require bind
44
44
  //= require selection-buttons
45
45
 
46
+ ## Usage of GovukElementsFormBuilder
47
+
48
+ To replace the default form builder with a version that generates GOV.UK Elements classed markup, set the following in
49
+ config/application.rb:
50
+
51
+ config.use_govuk_elements_form_builder = true
52
+
46
53
  ## Alternate ways to reuse GOV.UK Elements
47
54
 
48
55
  There are other alternate ways to include GOV.UK Elements files in a Rails
@@ -0,0 +1,77 @@
1
+ class GovukElementsFormBuilder < ActionView::Helpers::FormBuilder
2
+ attr_reader :template, :object_name
3
+
4
+ def text_area(method, **options)
5
+ with_label(method) {
6
+ super(method, add_class(options, 'text form-control'))
7
+ }
8
+ end
9
+
10
+ def bold_label(method, text: nil, **options)
11
+ label(method, text, add_class(options, 'form-label'))
12
+ end
13
+
14
+ def radio_buttons(method, values, labeler: labeler, **options)
15
+ radio_button_fieldset(method, options) {
16
+ with_label(method) {
17
+ values.map { |value|
18
+ label(method, value: value, class: 'block-label') {
19
+ radio_button(method, value) + labeler.call(value)
20
+ }
21
+ }.join.html_safe
22
+ }
23
+ }
24
+ end
25
+
26
+ def text_field(method, **options)
27
+ with_label(method) {
28
+ super(method, add_class(options, 'form-control'))
29
+ }
30
+ end
31
+
32
+ def password_field(method, **options)
33
+ with_label(method) {
34
+ super(method, add_class(options, 'form-control'))
35
+ }
36
+ end
37
+
38
+ def file_field(method, **options)
39
+ with_label(method) {
40
+ super(method, add_class(options, 'form-control'))
41
+ }
42
+ end
43
+
44
+ def collection_select(method, values, valuer: nil, labeler: nil, **options)
45
+ valuer ||= ->(a) { a.respond_to?(:to_param) ? a.to_param : a }
46
+ labeler ||= ->(a) { a }
47
+ with_label(method) {
48
+ super(
49
+ method, values, valuer, labeler, options,
50
+ class: 'select form-control'
51
+ )
52
+ }
53
+ end
54
+
55
+ def button(value = nil, **options)
56
+ super(value, add_class(options, 'button'))
57
+ end
58
+
59
+ def with_label(method, &content)
60
+ error_messages = object.errors[method].map { |error|
61
+ template.content_tag(:p, class: 'error') { error }
62
+ }.join.html_safe
63
+ bold_label(method) + content.call + error_messages
64
+ end
65
+
66
+ def add_class(options, klass)
67
+ options.merge(class: [options[:class], klass].compact.join(' '))
68
+ end
69
+
70
+ private
71
+
72
+ def radio_button_fieldset(method, options, &blk)
73
+ id = [object_name, method].join('_')
74
+ klass = options[:inline] ? 'inline' : ''
75
+ template.content_tag(:fieldset, id: id, class: klass, &blk)
76
+ end
77
+ end
@@ -1,4 +1,14 @@
1
1
  module GovUKElementsRails
2
2
  class Engine < ::Rails::Engine
3
+
4
+ initializer "govuk_elements_rails.default_form_builder" do |app|
5
+ use_builder = app.config.respond_to?(:use_govuk_elements_form_builder) &&
6
+ app.config.use_govuk_elements_form_builder
7
+
8
+ if use_builder
9
+ ActionView::Base.default_form_builder = GovukElementsFormBuilder
10
+ end
11
+ end
12
+
3
13
  end
4
14
  end
@@ -1,5 +1,5 @@
1
1
  module GovUKElementsRails
2
2
  def self.elements_version
3
- '0.1.1'
3
+ '0.2.1'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: govuk_elements_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rob McKinnon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-21 00:00:00.000000000 Z
11
+ date: 2014-12-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -60,6 +60,7 @@ extra_rdoc_files:
60
60
  files:
61
61
  - LICENCE
62
62
  - README.md
63
+ - app/builders/govuk_elements_form_builder.rb
63
64
  - lib/govuk_elements_rails.rb
64
65
  - lib/govuk_elements_rails/engine.rb
65
66
  - lib/govuk_elements_rails/version.rb