view_partial_form_builder 0.1.0 → 0.1.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
  SHA256:
3
- metadata.gz: e30eab928ee84a6217afbabc1c746aa7a7906efa5417fc323f6b78e476aa3fbb
4
- data.tar.gz: 21af6978419ac00cebba9989b86943eb2d4d102397d880cee50acb8a7ef11c7c
3
+ metadata.gz: eccf5b9ceb2917549fa81592dc4cb2a905b0ac4af8cd4a58a770b0b505905f0f
4
+ data.tar.gz: b554c6fcc8f97fee666a6e587351f6b37f8ba6bf0b4eececf63224d1f368f111
5
5
  SHA512:
6
- metadata.gz: 848d4ddf155cb750a63400e9a5aaa3ea062a4d01b3041294c6ddbf64c3570882e28052d6b55066c731320daae6b43e7fa53c47f1a89924603bab50c3ba3885be
7
- data.tar.gz: 05e69629609a81b0563bc5a234c3305dc0355e6f0b458d6aea41e75d49009d26a1e274a9ce7e4ccf6b50aa4ff90afa79c30220f44d97a4c754d9988fd9476046
6
+ metadata.gz: b5093c0eb1fc40b602233db5041fb3cd11b8bc7aef584c8c50f7246f92550cbe002c58d87d4906cc508187e3a77273791435fbd74b3f7d41dcfa014304b89a21
7
+ data.tar.gz: b0b9db9047dd007e45341866ac2fc54eb91ccff09df57bf86420b587369be7173f5051a82ca9e83c82d20e7dff32e98df60ac6c87591df9c5c83eeec811a62e3
data/README.md CHANGED
@@ -240,6 +240,59 @@ as the `partial:` option:
240
240
  <% end %>
241
241
  ```
242
242
 
243
+ #### Composing partials
244
+
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.
248
+
249
+ Declare the consumer facing inputs (in this example, `<input type="search">`):
250
+
251
+ ```html+erb
252
+ <%# app/views/form_builder/_search_field.html.erb %>
253
+
254
+ <%= form.search_field(
255
+ *arguments,
256
+ **options.merge_token_lists(
257
+ class: "search-field",
258
+ "data-controller": "input->search#executeQuery",
259
+ ),
260
+ ) %>
261
+ ```
262
+
263
+ Then, declare the administrative interface's inputs, in terms of overriding the
264
+ foundation built by the more general definitions:
265
+
266
+ ```html+erb
267
+ <%# app/views/admin/form_builder/_search_field.html.erb %>
268
+
269
+ <%= 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
+ ),
276
+ ) %>
277
+ ```
278
+
279
+ The rendered `admin/form_builder/search_field` partial combines options and
280
+ arguments from both partials:
281
+
282
+ ```html
283
+ <input
284
+ type="search"
285
+ class="
286
+ search-field
287
+ search-field--admin
288
+ "
289
+ data-controller="
290
+ input->search#executeQuery
291
+ focus->admin-search#clearResults
292
+ "
293
+ >
294
+ ```
295
+
243
296
  ### Configuration
244
297
 
245
298
  View partials lookup and resolution will be scoped to the
@@ -250,19 +250,22 @@ module ViewPartialFormBuilder
250
250
  attr_reader :lookup_context
251
251
 
252
252
  def render_partial(field, locals, fallback:, &block)
253
- return fallback.call if about_to_recurse_infinitely?(field)
254
-
255
253
  options = locals.fetch(:options, {})
256
254
  partial_override = options.delete(:partial)
257
- locals = objectify_options(options).merge(locals, form: self)
258
-
259
- lookup_context.override do
260
- if partial_override.present?
261
- render(partial_override, locals, &block)
262
- elsif partial_exists?(field)
263
- render(field, locals, &block)
264
- else
265
- fallback.call
255
+
256
+ if about_to_recurse_infinitely?(field, partial_override)
257
+ fallback.call
258
+ else
259
+ locals = objectify_options(options).merge(locals, form: self)
260
+
261
+ lookup_context.override do
262
+ if partial_override.present?
263
+ render(partial_override, locals, &block)
264
+ elsif partial_exists?(field)
265
+ render(field, locals, &block)
266
+ else
267
+ fallback.call
268
+ end
266
269
  end
267
270
  end
268
271
  end
@@ -285,11 +288,13 @@ module ViewPartialFormBuilder
285
288
  end
286
289
  end
287
290
 
288
- def about_to_recurse_infinitely?(field)
291
+ def about_to_recurse_infinitely?(field, partial_override)
289
292
  @template.instance_eval do
290
- *, partial = @virtual_path.split("/")
293
+ current_partial = @virtual_path.gsub("/_", "/")
294
+ currently_rendering_field = current_partial.end_with?(field)
291
295
 
292
- partial == "_#{field}"
296
+ return true if currently_rendering_field && partial_override.nil?
297
+ return true if currently_rendering_field && partial_override == current_partial
293
298
  end
294
299
  end
295
300
  end
@@ -1,3 +1,3 @@
1
1
  module ViewPartialFormBuilder
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: view_partial_form_builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sean Doyle
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-12 00:00:00.000000000 Z
11
+ date: 2020-04-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionview