view_partial_form_builder 0.1.4 → 0.1.5

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
  SHA256:
3
- metadata.gz: 99ab630e8601d66da9873d48a3c1abbb3611df65e8aa57cb165b3397f2d2b085
4
- data.tar.gz: d1a221e7ce27c1e3c48b6dad21c9cf5c72c67db3da17cf8d83a072d7691f5211
3
+ metadata.gz: 1d1550e43fb981483d25dedf89ab64478b5dd2c5803b4aa7345ae6d7efba4fe2
4
+ data.tar.gz: d8bb992d8d959d2288c761d764275af9e470081145884b6bfcff7b7dda3fdc45
5
5
  SHA512:
6
- metadata.gz: 44950718466b81b4928ffe33060cc7a90cce087702e8d3830d189bd2e5f1ec1b8fd0bdca68845f94ffdda02ec15febe8fa847bc78dc613e00682f28eab7a072c
7
- data.tar.gz: 1bae64c4d9601d0a265b442bc9de563c691a2d76753414901aac0db176757bc132ff22ed1e92e890d3415b2cc277d705b4cffe1830fe97c7a846176a59803f1f
6
+ metadata.gz: 121978b8280e71e3cf114291cf4874949c9323f7b909ab7cd93602f1a3fb00b143c2028ed1188623c8b926efe8b3a27e67005947599d66b8e1cb33c98d8c589e
7
+ data.tar.gz: 9e56d40123f55fac7ee53b3bd3e087ccc35bdf3ca709485cad0d636fb41f7f684744dbc068d7af13bcdc6aa246e9707373c557f792d86a1d3fee1cd76468e4f0
data/README.md CHANGED
@@ -87,7 +87,7 @@ generating HTML through Rails' helpers:
87
87
  <%# app/views/form_builder/_button.html.erb %>
88
88
 
89
89
  <div class="button-wrapper">
90
- <%= form.button(*arguments, **options, &block) %>
90
+ <%= form.button(value, options, &block) %>
91
91
  </div>
92
92
  ```
93
93
 
@@ -116,31 +116,8 @@ In addition, each view partial receives:
116
116
  * `form` - a reference to the instance of `ViewPartialFormBuilder`, which is a
117
117
  descendant of [`ActionView::Helpers::FormBuilder`][FormBuilder]
118
118
 
119
- * `arguments` - an Array containing the arguments the helper received, in the
120
- order they were received. This can be useful to pass to the view partial's
121
- helper by [splatting them][splat] out.
122
-
123
119
  * `&block` - a callable, `yield`-able block if the helper method was passed one
124
120
 
125
- In cases when a [`ActionView::Helpers::FormBuilder` helper
126
- method][FormBuilder]'s last arguments are options (either `Hash` instances or
127
- [keyword arguments][]), they're omitted from the `arguments` array.
128
-
129
- If you want to pass-through all arguments, options, and block parameters, you
130
- can splat them out:
131
-
132
- ```html+erb
133
- <%# app/views/form_builder/_label.html.erb %>
134
-
135
- <%= form.label(*arguments, **options, &block) %>
136
- ```
137
-
138
- ```html+erb
139
- <%# app/views/form_builder/_select.html.erb %>
140
-
141
- <%= form.select(*arguments, **html_options, &block) %>
142
- ```
143
-
144
121
  #### Handling DOMTokenList attributes
145
122
 
146
123
  An [HTML element's `class` attribute][mdn-class] is treated by browsers as a
@@ -158,16 +135,12 @@ When rendering a field's DOMTokenList-backed attributes (like `class` or
158
135
  controllers][stimulus-controller]), transforming and combining singular `String`
159
136
  instances into lists of token can be very useful.
160
137
 
161
- To simplify those scenarios, a partial's template-local optional attributes are
162
- made available with the `#merge_token_lists` method.
163
-
164
138
  These optional attributes are available through the `options` or `html_options`
165
139
  partial-local variables. Their name will depend on the partial's corresponding
166
140
  [`ActionView::Helpers::FormBuilder`][FormBuilder] interface.
167
141
 
168
- Calls to `#merge_token_lists` will merge the key-value pairs and return a new
169
- Hash-like structure. The attribute's value will be transformed into an `Array`.
170
- Given call to `form.text_field` and a corresponding partial declaration:
142
+ To "merge" attributes together, you can combine Ruby's `String` interpolation
143
+ and `Hash#delete`:
171
144
 
172
145
  ```html+erb
173
146
  <%# app/views/users/new.html.erb %>
@@ -177,7 +150,11 @@ Given call to `form.text_field` and a corresponding partial declaration:
177
150
 
178
151
  <# app/views/form_builder/_text_field.html.erb %>
179
152
 
180
- <%= form.text_field(*arguments, **options.merge_token_lists(class: "text-field")) %>
153
+ <%= form.text_field(
154
+ method,
155
+ class: "text-field #{options.delete(:class)}",
156
+ **options
157
+ ) %>
181
158
  ```
182
159
 
183
160
  The resulting HTML `<input>` element will merge have its [`class`
@@ -192,7 +169,6 @@ values:
192
169
  [button]: https://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-button
193
170
  [local_assigns]: https://api.rubyonrails.org/classes/ActionView/Template.html#method-i-local_assigns
194
171
  [splat]: https://ruby-doc.org/core-2.2.0/doc/syntax/calling_methods_rdoc.html#label-Array+to+Arguments+Conversion
195
- [keyword arguments]: https://thoughtbot.com/blog/ruby-2-keyword-arguments
196
172
  [mdn-class]: https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/class
197
173
  [DOMTokenList]: https://developer.mozilla.org/en-US/docs/Web/API/DOMTokenList
198
174
  [classList]: https://developer.mozilla.org/en-US/docs/Web/API/Element/classList
@@ -210,7 +186,7 @@ block-local `form` variable:
210
186
  ```erb
211
187
  <%# app/views/users/form_builder/_email_field.html.erb %>
212
188
 
213
- <%= form.default.email_field(*arguments, **options) %>
189
+ <%= form.default.email_field(method, options) %>
214
190
  ```
215
191
 
216
192
  When passing a `model:` or `scope:` to calls to [`form_with`][form_with],
@@ -225,26 +201,41 @@ For example, when calling `form_with(model: User.new)`, a partial declared in
225
201
  <%# app/views/users/form_builder/_password_field.html.erb %>
226
202
 
227
203
  <div class="password-field-wrapper">
228
- <%= form.password_field(*arguments, **options) %>
204
+ <%= form.password_field(method, options) %>
229
205
  </div>
230
206
  ```
231
207
 
232
- If you'd like to render a specific partial for a field, you can declare the name
233
- as the `partial:` option:
208
+ If you'd like to render a specific partial for a field, make sure that you pass
209
+ along the `form:` (along with any other partial-local variables) as part of the
210
+ `render` call's `locals:` option:
211
+
234
212
 
235
213
  ```erb
236
214
  <%# app/views/users/new.html.erb %>
237
215
 
238
216
  <%= form_with(model: User.new) do |form| %>
239
- <%= form.email_field(:email, partial: "emails/my_special_text_field") %>
217
+ <%= render("emails/my_special_email_field", {
218
+ form: form,
219
+ method: :email,
220
+ options: { class: "user-email" },
221
+ ) %>
240
222
  <% end %>
223
+
224
+ <%# app/views/emails/_my_special_email_field.html.erb %>
225
+
226
+ <%= form.email_field(
227
+ method,
228
+ class: "my-special-email #{options.delete(:class)},
229
+ **options
230
+ ) %>
241
231
  ```
242
232
 
243
233
  #### Composing partials
244
234
 
245
- Passing a `partial:` key can be useful for layering partials on top of one
246
- another. For instance, consider an administrative interface that shares styles
247
- with a consumer facing site, but has additional bells and whistles.
235
+ Layering partials on top of one another can be useful to share foundational
236
+ styles and configuration across your fields. For instance, consider an
237
+ administrative interface that shares styles with a consumer facing site, but has
238
+ additional bells and whistles.
248
239
 
249
240
  Declare the consumer facing inputs (in this example, `<input type="search">`):
250
241
 
@@ -252,11 +243,16 @@ Declare the consumer facing inputs (in this example, `<input type="search">`):
252
243
  <%# app/views/form_builder/_search_field.html.erb %>
253
244
 
254
245
  <%= form.search_field(
255
- *arguments,
256
- **options.merge_token_lists(
257
- class: "search-field",
258
- "data-controller": "input->search#executeQuery",
259
- ),
246
+ method,
247
+ class: "
248
+ search-field
249
+ #{options.delete(:class}
250
+ ",
251
+ "data-controller": "
252
+ input->search#executeQuery
253
+ #{options.delete(:"data-controller")}
254
+ ",
255
+ **options
260
256
  ) %>
261
257
  ```
262
258
 
@@ -267,12 +263,15 @@ foundation built by the more general definitions:
267
263
  <%# app/views/admin/form_builder/_search_field.html.erb %>
268
264
 
269
265
  <%= form.search_field(
270
- *arguments,
271
- partial: "form_builder/search_field",
272
- **options.merge_token_lists(
273
- class: "search-field--admin",
274
- "data-controller": "focus->admin-search#clearResults",
275
- ),
266
+ method,
267
+ class: "
268
+ search-field--admin
269
+ #{options.delete(:class}
270
+ ",
271
+ "data-controller": "
272
+ focus->admin-search#clearResults
273
+ #{options.delete(:"data-controller")}
274
+ ",
276
275
  ) %>
277
276
  ```
278
277
 
@@ -261,9 +261,17 @@ module ViewPartialFormBuilder
261
261
  def render_partial(field, locals, fallback:, &block)
262
262
  options = locals.fetch(:options, {})
263
263
  partial_override = options.delete(:partial)
264
- locals = objectify_options(options).merge(locals, form: self)
264
+ options_as_locals = objectify_options(options)
265
+ locals = DeprecatedHash.new(
266
+ options_as_locals.merge(locals, form: self),
267
+ deprecated_keys: options_as_locals.keys + [:arguments],
268
+ )
265
269
 
266
270
  partial = if partial_override.present?
271
+ ActiveSupport::Deprecation.new("0.2.0", "ViewPartialFormBuilder").warn(<<~WARNING)
272
+ Providing a `partial:` option for a form field is deprecated.
273
+ WARNING
274
+
267
275
  find_partial(partial_override, locals, prefixes: [])
268
276
  else
269
277
  find_partial(field, locals, prefixes: prefixes_after(field))
@@ -284,7 +292,15 @@ module ViewPartialFormBuilder
284
292
  prefixes,
285
293
  template_is_partial,
286
294
  locals.keys,
287
- ).first
295
+ ).first.tap do |partial|
296
+ root_directory = ViewPartialFormBuilder.view_partial_directory
297
+
298
+ if partial&.virtual_path == "#{root_directory}/_#{template_name}"
299
+ ActiveSupport::Deprecation.new("0.2.0", "ViewPartialFormBuilder").warn(<<~WARNING.strip)
300
+ Declare root-level partials in app/views/application/#{root_directory}/, not app/views/#{root_directory}/.
301
+ WARNING
302
+ end
303
+ end
288
304
  end
289
305
 
290
306
  def prefixes_after(template_name)
@@ -310,4 +326,22 @@ module ViewPartialFormBuilder
310
326
  end
311
327
  end
312
328
  end
329
+
330
+ class DeprecatedHash < Hash
331
+ def initialize(hash, deprecated_keys: [])
332
+ super()
333
+ merge!(hash)
334
+ @deprecated_keys = deprecated_keys
335
+ end
336
+
337
+ def [](key)
338
+ if @deprecated_keys.include?(key)
339
+ ActiveSupport::Deprecation.new("0.2.0", "ViewPartialFormBuilder").warn <<~WARNING
340
+ Accessing `#{key}` from partials is deprecated.
341
+ WARNING
342
+ end
343
+
344
+ super
345
+ end
346
+ end
313
347
  end
@@ -1,5 +1,13 @@
1
1
  module ViewPartialFormBuilder
2
2
  class HtmlAttributes
3
+ deprecate merge_token_lists: <<~'WARNING', deprecator: ActiveSupport::Deprecation.new("0.2.0", "ViewPartialFormBuilder")
4
+
5
+ As an alternative, merge arguments through String interpolation:
6
+
7
+ <%= form.label(method, class: "label #{options.delete(:class)}", **options) %>
8
+
9
+ WARNING
10
+
3
11
  def initialize(**attributes)
4
12
  @attributes = attributes
5
13
  end
@@ -1,3 +1,3 @@
1
1
  module ViewPartialFormBuilder
2
- VERSION = '0.1.4'
2
+ VERSION = '0.1.5'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: view_partial_form_builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sean Doyle