view_partial_form_builder 0.1.2 → 0.1.3

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: d222cf5e301a82aad3e0b9ecfd1bb379bcd569acaa49142d1a8da4397c5f5a01
4
- data.tar.gz: 3258a1ba69cd315e37ef00af032ed439aa80802ea77aa05a2ebaab1572bda6fc
3
+ metadata.gz: 2bad5344dd2dcb3ff6332ccce094cafee462d8dad93253e40002d90869908c7d
4
+ data.tar.gz: 75733a62d1ec0c8266e2577380a70d7459942a4b908ae17e5d98b25cdb6bfb0f
5
5
  SHA512:
6
- metadata.gz: 6708f5a86457c43e92a340d492251a3c05158afdc373f4dcf223b636b7ef094163c6d400f1f5845b5afba542edeb142e98bb367bd1d95043987edcc368a97744
7
- data.tar.gz: 41e75ce219a6c305a67c29656fc2e8b0561cda2f5e8e4196d2437de39f646e9e3388664e7c421471b4b374ae2e4b735716647f1a8ec0cb1d50cfd13cc8b2afad
6
+ metadata.gz: f847a1f3ceb8b4c390e94575fd6aeb0b37bf93daf49804c4fbc125b476f896250b14a80cb522c76ce32e33e31457f30cb222f28b978851f58ccaa68f19aa76ed
7
+ data.tar.gz: 682356c76cf59d62ad176adc0166e6e1ec4c8102b89d3214567cd2abea2721f5d6fdb70259f53b55aed6a955adcbf7f7c48275a37159fdb545caad23275df6cb
@@ -1,4 +1,4 @@
1
- require "view_partial_form_builder/lookup_context"
1
+ require "view_partial_form_builder/lookup_override"
2
2
  require "view_partial_form_builder/html_attributes"
3
3
 
4
4
  module ViewPartialFormBuilder
@@ -14,9 +14,9 @@ module ViewPartialFormBuilder
14
14
  @template,
15
15
  options,
16
16
  )
17
- @lookup_context = LookupContext.new(
18
- overridden_context: @template.lookup_context,
19
- object_name: object_name,
17
+ @lookup_override = LookupOverride.new(
18
+ prefixes: @template.lookup_context.prefixes,
19
+ object_name: @object_name,
20
20
  view_partial_directory: ViewPartialFormBuilder.view_partial_directory,
21
21
  )
22
22
  end
@@ -173,6 +173,17 @@ module ViewPartialFormBuilder
173
173
  render_partial("time_zone_select", locals, fallback: -> { super })
174
174
  end
175
175
 
176
+ def date_select(method, options = {}, **html_options)
177
+ locals = {
178
+ method: method,
179
+ options: options,
180
+ html_options: HtmlAttributes.new(html_options),
181
+ arguments: [method, options, html_options],
182
+ }
183
+
184
+ render_partial("date_select", locals, fallback: -> { super })
185
+ end
186
+
176
187
  def hidden_field(method, **options)
177
188
  @emitted_hidden_id = true if method == :id
178
189
 
@@ -247,55 +258,55 @@ module ViewPartialFormBuilder
247
258
 
248
259
  private
249
260
 
250
- attr_reader :lookup_context
251
-
252
261
  def render_partial(field, locals, fallback:, &block)
253
262
  options = locals.fetch(:options, {})
254
263
  partial_override = options.delete(:partial)
264
+ locals = objectify_options(options).merge(locals, form: self)
255
265
 
256
- if about_to_recurse_infinitely?(field, partial_override)
266
+ partial = if partial_override.present?
267
+ find_partial(partial_override, locals, prefixes: [])
268
+ else
269
+ find_partial(field, locals, prefixes: prefixes_after(field))
270
+ end
271
+
272
+ if partial.nil? || about_to_recurse_infinitely?(partial)
257
273
  fallback.call
258
274
  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
269
- end
275
+ partial.render(@template, locals, &block)
270
276
  end
271
277
  end
272
278
 
273
- def partial_exists?(template_name)
279
+ def find_partial(template_name, locals, prefixes:)
274
280
  template_is_partial = true
275
281
 
276
- lookup_context.template_exists?(
282
+ @template.lookup_context.find_all(
277
283
  template_name,
278
- lookup_context.prefixes,
284
+ prefixes,
279
285
  template_is_partial,
280
- )
286
+ locals.keys,
287
+ ).first
281
288
  end
282
289
 
283
- def render(partial_name, locals, &block)
284
- if block.present?
285
- @template.render(layout: partial_name, locals: locals, &block)
290
+ def prefixes_after(template_name)
291
+ prefixes = @lookup_override.prefixes
292
+ current_prefix = current_virtual_path.delete_suffix("/_#{template_name}")
293
+
294
+ if prefixes.include?(current_prefix)
295
+ prefixes.from(prefixes.index(current_prefix).to_i + 1)
286
296
  else
287
- @template.render(partial: partial_name, locals: locals)
297
+ prefixes
288
298
  end
289
299
  end
290
300
 
291
- def about_to_recurse_infinitely?(field, partial_override)
292
- @template.instance_eval do
293
- partial_name = "/#{field}"
294
- current_partial = @virtual_path.gsub("/_", "/")
295
- currently_rendering_field = current_partial.end_with?(partial_name)
301
+ def about_to_recurse_infinitely?(partial)
302
+ partial.virtual_path == current_virtual_path
303
+ end
296
304
 
297
- return true if currently_rendering_field && partial_override.nil?
298
- return true if currently_rendering_field && partial_override == current_partial
305
+ def current_virtual_path
306
+ if (current_template = @template.instance_values["current_template"])
307
+ current_template.virtual_path.to_s
308
+ else
309
+ @template.instance_values["virtual_path"].to_s
299
310
  end
300
311
  end
301
312
  end
@@ -1,29 +1,12 @@
1
1
  module ViewPartialFormBuilder
2
- class LookupContext
3
- delegate_missing_to :overridden_context
4
-
5
- def initialize(overridden_context:, object_name:, view_partial_directory:)
2
+ class LookupOverride
3
+ def initialize(prefixes:, object_name:, view_partial_directory:)
6
4
  @object_name = object_name.to_s.pluralize
7
- @overridden_context = overridden_context
8
- @prefixes = overridden_context.prefixes.dup.freeze
5
+ @prefixes = prefixes
9
6
  @view_partial_directory = view_partial_directory
10
7
  end
11
8
 
12
- def override(&block)
13
- previous_prefixes = overridden_context.prefixes
14
-
15
- overridden_context.prefixes = prefix_overrides
16
-
17
- yield
18
- ensure
19
- overridden_context.prefixes = previous_prefixes
20
- end
21
-
22
- private
23
-
24
- attr_reader :overridden_context, :object_name, :view_partial_directory
25
-
26
- def prefix_overrides
9
+ def prefixes
27
10
  *overridden_prefixes, root_prefix = @prefixes.dup
28
11
 
29
12
  prefixes = [
@@ -49,5 +32,9 @@ module ViewPartialFormBuilder
49
32
 
50
33
  prefixes.uniq
51
34
  end
35
+
36
+ private
37
+
38
+ attr_reader :object_name, :view_partial_directory
52
39
  end
53
40
  end
@@ -1,3 +1,3 @@
1
1
  module ViewPartialFormBuilder
2
- VERSION = '0.1.2'
2
+ VERSION = '0.1.3'
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.2
4
+ version: 0.1.3
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-16 00:00:00.000000000 Z
11
+ date: 2020-08-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionview
@@ -94,7 +94,7 @@ files:
94
94
  - lib/view_partial_form_builder/engine.rb
95
95
  - lib/view_partial_form_builder/form_builder.rb
96
96
  - lib/view_partial_form_builder/html_attributes.rb
97
- - lib/view_partial_form_builder/lookup_context.rb
97
+ - lib/view_partial_form_builder/lookup_override.rb
98
98
  - lib/view_partial_form_builder/version.rb
99
99
  homepage: https://github.com/seanpdoyle/view_partial_form_builder
100
100
  licenses: