govuk_elements_form_builder 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b43d881f552b63310176c72eaa304ed53e5b89de
4
- data.tar.gz: 2b0086ffaabcbf31e15311ffee4eddae8ad62cb1
3
+ metadata.gz: 09e273646a0e9edb0788ac45e25409e98a8a815f
4
+ data.tar.gz: aa0abb864b6b38852ac96005461c204ab031e9c6
5
5
  SHA512:
6
- metadata.gz: e94aa32b4bcaefa3020fe6797fcb7eb6acb2ed484bf70deec782908678c53fe21a2c4d9befac3f3d9370631f0f51ee05d883dca1c622ec5298cf9eb5253e0ce4
7
- data.tar.gz: 7633c157a26792fdc1ae018b649b16ecf0bfc50b3d505c6afd71a9b0369c86a99d99b961ddd6d302b7ac3b223668e611bce81b88f5189a61d4ce0583adaa6282
6
+ metadata.gz: 59ad72a527811abca00667b6f8457d790310b3e8d230509f56883b0b60c6218a87356ed2618a48f8fc324fca78e13e65102e16035c7bf62ae28afe87a367b31b
7
+ data.tar.gz: 1fdea4d35a4ce12392e26eeedef890d07261422d995bf43236d30a40bd27b24e6023a142c8077b857687cbaddce846da0c8543841282f0b3a1ad550eab1d0699
@@ -1,5 +1,6 @@
1
1
  require 'govuk_elements_form_builder/version'
2
2
  require 'govuk_elements_form_builder/form_builder'
3
+ require 'govuk_elements_form_builder/block_buffer'
3
4
  require_relative '../app/helpers/govuk_elements_errors_helper'
4
5
 
5
6
  module GovukElementsFormBuilder
@@ -0,0 +1,29 @@
1
+ module GovukElementsFormBuilder
2
+ #
3
+ # This class is intended to be a proxy for a FormBuilder object, and accumulate
4
+ # multiple form elements rendered inside a block. Its main reason for existence is
5
+ # to enable the following syntax when rendering revealing panels:
6
+ #
7
+ # f.radio_button_fieldset :asked_for_help do |fieldset|
8
+ # fieldset.radio_input(GenericYesNo::YES) do |radio|
9
+ # # Here, `radio` is a `BlockBuffer` instance, delegating all methods to
10
+ # # a `FormBuilder` instance, but doing the accumulation/concatenation.
11
+ # radio.text_field(:help_party)
12
+ # radio.text_field(:another_field)
13
+ # [...]
14
+ # end
15
+ # fieldset.radio_input(GenericYesNo::NO)
16
+ # end
17
+ #
18
+ class BlockBuffer
19
+ delegate :safe_concat, :send, to: :@form_object
20
+
21
+ def initialize(form_object)
22
+ @form_object = form_object
23
+ end
24
+
25
+ def method_missing(method, *args, &block)
26
+ safe_concat send(method, *args, &block)
27
+ end
28
+ end
29
+ end
@@ -5,9 +5,12 @@ module GovukElementsFormBuilder
5
5
  add_error_to_html_tag! html_tag, instance
6
6
  end
7
7
 
8
- delegate :content_tag, :tag, :safe_join, to: :@template
8
+ delegate :content_tag, :tag, :safe_join, :safe_concat, :capture, to: :@template
9
9
  delegate :errors, to: :@object
10
10
 
11
+ # Used to propagate the fieldset outer element attribute to the inner elements
12
+ attr_accessor :current_fieldset_attribute
13
+
11
14
  # Ensure fields_for yields a GovukElementsFormBuilder.
12
15
  def fields_for record_name, record_object = nil, fields_options = {}, &block
13
16
  super record_name, record_object, fields_options.merge(builder: self.class), &block
@@ -41,27 +44,27 @@ module GovukElementsFormBuilder
41
44
  end
42
45
  end
43
46
 
44
- def radio_button_fieldset attribute, options={}
47
+ def radio_button_fieldset attribute, options={}, &block
45
48
  content_tag :div,
46
49
  class: form_group_classes(attribute),
47
50
  id: form_group_id(attribute) do
48
51
  content_tag :fieldset, fieldset_options(attribute, options) do
49
52
  safe_join([
50
- fieldset_legend(attribute),
51
- radio_inputs(attribute, options)
53
+ fieldset_legend(attribute, options),
54
+ block_given? ? capture(self, &block) : radio_inputs(attribute, options)
52
55
  ], "\n")
53
56
  end
54
57
  end
55
58
  end
56
59
 
57
- def check_box_fieldset legend_key, attributes, options={}
60
+ def check_box_fieldset legend_key, attributes, options={}, &block
58
61
  content_tag :div,
59
62
  class: form_group_classes(attributes),
60
63
  id: form_group_id(attributes) do
61
64
  content_tag :fieldset, fieldset_options(attributes, options) do
62
65
  safe_join([
63
- fieldset_legend(legend_key),
64
- check_box_inputs(attributes)
66
+ fieldset_legend(legend_key, options),
67
+ block_given? ? capture(self, &block) : check_box_inputs(attributes, options)
65
68
  ], "\n")
66
69
  end
67
70
  end
@@ -82,48 +85,88 @@ module GovukElementsFormBuilder
82
85
 
83
86
  end
84
87
 
88
+ # The following method will generate revealing panel markup and internally call the
89
+ # `radio_inputs` private method. It is not intended to be used outside a
90
+ # fieldset tag (at the moment, `radio_button_fieldset`).
91
+ #
92
+ def radio_input choice, options = {}, &block
93
+ fieldset_attribute = self.current_fieldset_attribute
94
+
95
+ panel = if block_given? || options.key?(:panel_id)
96
+ panel_id = options.delete(:panel_id) { [fieldset_attribute, choice, 'panel'].join('_') }
97
+ options.merge!('data-target': panel_id)
98
+ revealing_panel(panel_id, flush: false, &block) if block_given?
99
+ end
100
+
101
+ option = radio_inputs(
102
+ fieldset_attribute,
103
+ options.merge(choices: [choice])
104
+ ).first + "\n"
105
+
106
+ safe_concat([option, panel].join)
107
+ end
108
+
109
+ # The following method will generate revealing panel markup and internally call the
110
+ # `check_box_inputs` private method. It is not intended to be used outside a
111
+ # fieldset tag (at the moment, `check_box_fieldset`).
112
+ #
113
+ def check_box_input attribute, options = {}, &block
114
+ panel = if block_given? || options.key?(:panel_id)
115
+ panel_id = options.delete(:panel_id) { [attribute, 'panel'].join('_') }
116
+ options.merge!('data-target': panel_id)
117
+ revealing_panel(panel_id, flush: false, &block) if block_given?
118
+ end
119
+
120
+ checkbox = check_box_inputs([attribute], options).first + "\n"
121
+
122
+ safe_concat([checkbox, panel].join)
123
+ end
124
+
125
+ def revealing_panel panel_id, options = {}, &block
126
+ panel = content_tag(
127
+ :div, class: 'panel panel-border-narrow js-hidden', id: panel_id
128
+ ) { block.call(BlockBuffer.new(self)) } + "\n"
129
+
130
+ options.fetch(:flush, true) ? safe_concat(panel) : panel
131
+ end
132
+
85
133
  private
86
134
 
135
+ # Given an attributes hash that could include any number of arbitrary keys, this method
136
+ # ensure we merge one or more 'default' attributes into the hash, creating the keys if
137
+ # don't exist, or merging the defaults if the keys already exists.
138
+ # It supports strings or arrays as values.
139
+ #
140
+ def merge_attributes attributes, default:
141
+ hash = attributes || {}
142
+ hash.merge(default) { |_key, oldval, newval| Array(newval) + Array(oldval) }
143
+ end
144
+
87
145
  def set_field_classes! options, attribute
88
- text_field_class = "form-control"
89
- text_field_class = [text_field_class, 'form-control-error'] if error_for? attribute
90
- options[:class] = case options[:class]
91
- when String
92
- [text_field_class, options[:class]]
93
- when Array
94
- options[:class].unshift text_field_class
95
- else
96
- options[:class] = text_field_class
97
- end
146
+ default_classes = ['form-control']
147
+ default_classes << 'form-control-error' if error_for?(attribute)
148
+
149
+ options ||= {}
150
+ options.merge!(
151
+ merge_attributes(options, default: {class: default_classes})
152
+ )
98
153
  end
99
154
 
100
155
  def set_label_classes! options
101
- text_field_class = "form-label"
102
-
103
- if options.present? && options[:label_options].present?
104
- options[:label_options][:class] = case options[:label_options][:class]
105
- when String
106
- [text_field_class, options[:label_options][:class]]
107
- when Array
108
- options[:label_options][:class].unshift text_field_class
109
- else
110
- options[:label_options][:class] = text_field_class
111
- end
112
- else
113
- options ||= {}
114
- options[:label_options] ||= {}
115
- options[:label_options][:class] = text_field_class
116
- end
117
-
156
+ options ||= {}
157
+ options[:label_options] ||= {}
158
+ options[:label_options].merge!(
159
+ merge_attributes(options[:label_options], default: {class: 'form-label'})
160
+ )
118
161
  end
119
162
 
120
- def check_box_inputs attributes
163
+ def check_box_inputs attributes, options
121
164
  attributes.map do |attribute|
122
165
  input = check_box(attribute)
123
166
  label = label(attribute) do |tag|
124
167
  localized_label("#{attribute}")
125
168
  end
126
- content_tag :div, class: "multiple-choice" do
169
+ content_tag :div, {class: 'multiple-choice'}.merge(options.slice(:class, :'data-target')) do
127
170
  input + label
128
171
  end
129
172
  end
@@ -142,18 +185,18 @@ module GovukElementsFormBuilder
142
185
  end
143
186
  text
144
187
  end
145
- content_tag :div, class: "multiple-choice" do
188
+ content_tag :div, {class: 'multiple-choice'}.merge(options.slice(:class, :'data-target')) do
146
189
  input + label
147
190
  end
148
191
  end
149
192
  end
150
193
 
151
- def fieldset_legend attribute
194
+ def fieldset_legend attribute, options
152
195
  legend = content_tag(:legend) do
153
196
  tags = [content_tag(
154
197
  :span,
155
198
  fieldset_text(attribute),
156
- class: 'form-label-bold'
199
+ merge_attributes(options[:legend_options], default: {class: 'form-label-bold'})
157
200
  )]
158
201
 
159
202
  if error_for? attribute
@@ -172,7 +215,9 @@ module GovukElementsFormBuilder
172
215
  legend.html_safe
173
216
  end
174
217
 
175
- def fieldset_options attributes, options
218
+ def fieldset_options attribute, options
219
+ self.current_fieldset_attribute = attribute
220
+
176
221
  fieldset_options = {}
177
222
  fieldset_options[:class] = 'inline' if options[:inline] == true
178
223
  fieldset_options
@@ -1,3 +1,3 @@
1
1
  module GovukElementsFormBuilder
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: govuk_elements_form_builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alistair Laing
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-05-21 00:00:00.000000000 Z
12
+ date: 2018-01-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -163,6 +163,7 @@ files:
163
163
  - Rakefile
164
164
  - app/helpers/govuk_elements_errors_helper.rb
165
165
  - lib/govuk_elements_form_builder.rb
166
+ - lib/govuk_elements_form_builder/block_buffer.rb
166
167
  - lib/govuk_elements_form_builder/components/error_summary.rb
167
168
  - lib/govuk_elements_form_builder/form_builder.rb
168
169
  - lib/govuk_elements_form_builder/version.rb