view_component 3.4.0 → 3.5.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of view_component might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fda2b08a3a0e713feb6e1fc6c1fef238f886cc4ab1ddd23e3d181bda02ad9c27
4
- data.tar.gz: e405fabfc3f12543caf45207f7222fdb9efcf9963dad10d3d37e47c131427e8b
3
+ metadata.gz: ce590ddce7011ae4362dde0733f78ba65992c31add6bac215555515d02ae1dc6
4
+ data.tar.gz: df4867599a61d2757e2f226a3c6b187981772e2d225fc442f9184484c0423e64
5
5
  SHA512:
6
- metadata.gz: 2a8be926941021f58d66f8b8c29893e5bd682426483914f16f431783c2e230029755e137a434f535fd081f51d3d9a63c219174ce68af9202f8f5f42f0f7087bd
7
- data.tar.gz: 4da3a061e16a87cc13440b4c1bf621da4d34a0b2443f28c34a132df0f4a85822b9f9ba12bea9b85c950e8b7991553c85a130eee75e1d7c84865feb30f66f17b5
6
+ metadata.gz: 0a39d71f089c0f4285dc1eecd324e44afc73f55565002a45c2d37292cc4fa8b05713f7237f04b530617ddc0bb2ccd08fab0c7d010400790dc00daf2d6c9c0546
7
+ data.tar.gz: 252a63a4ed7eefd7db529dc93081d3ccfb78555ac1d051540f7a69829a41d4d22fd94557fa2fe9eb88c012bd8dccfd2ceb813aae2582877e387a2df347e2d915
data/docs/CHANGELOG.md CHANGED
@@ -10,6 +10,20 @@ nav_order: 5
10
10
 
11
11
  ## main
12
12
 
13
+ ## 3.5.0
14
+
15
+ * Add Skroutz to users list.
16
+
17
+ *Chris Nitsas*
18
+
19
+ * Improve implementation of `#render_parent` so it respects variants and deep inheritance hierarchies.
20
+
21
+ *Cameron Dutro*
22
+
23
+ * Add CharlieHR to users list.
24
+
25
+ *Alex Balhatchet*
26
+
13
27
  ## 3.4.0
14
28
 
15
29
  * Avoid including Rails `url_helpers` into `Preview` class when they're not defined.
@@ -116,20 +116,44 @@ module ViewComponent
116
116
  end
117
117
 
118
118
  # Subclass components that call `super` inside their template code will cause a
119
- # double render if they emit the result:
119
+ # double render if they emit the result.
120
120
  #
121
121
  # ```erb
122
122
  # <%= super %> # double-renders
123
- # <% super %> # does not double-render
123
+ # <% super %> # doesn't double-render
124
124
  # ```
125
125
  #
126
- # Calls `super`, returning `nil` to avoid rendering the result twice.
126
+ # `super` also doesn't consider the current variant. `render_parent` renders the
127
+ # parent template considering the current variant and emits the result without
128
+ # double-rendering.
127
129
  def render_parent
128
- mtd = @__vc_variant ? "call_#{@__vc_variant}" : "call"
129
- method(mtd).super_method.call
130
+ render_parent_to_string
130
131
  nil
131
132
  end
132
133
 
134
+ # Renders the parent component to a string and returns it. This method is meant
135
+ # to be used inside custom #call methods when a string result is desired, eg.
136
+ #
137
+ # ```ruby
138
+ # def call
139
+ # "<div>#{render_parent_to_string}</div>"
140
+ # end
141
+ # ```
142
+ #
143
+ # When rendering the parent inside an .erb template, use `#render_parent` instead.
144
+ def render_parent_to_string
145
+ @__vc_parent_render_level ||= 0 # ensure a good starting value
146
+
147
+ begin
148
+ target_render = self.class.instance_variable_get(:@__vc_ancestor_calls)[@__vc_parent_render_level]
149
+ @__vc_parent_render_level += 1
150
+
151
+ target_render.bind_call(self, @__vc_variant)
152
+ ensure
153
+ @__vc_parent_render_level -= 1
154
+ end
155
+ end
156
+
133
157
  # Optional content to be returned after the rendered template.
134
158
  #
135
159
  # @return [String]
@@ -459,6 +483,13 @@ module ViewComponent
459
483
  # Set collection parameter to the extended component
460
484
  child.with_collection_parameter provided_collection_parameter
461
485
 
486
+ if instance_methods(false).include?(:render_template_for)
487
+ vc_ancestor_calls = defined?(@__vc_ancestor_calls) ? @__vc_ancestor_calls.dup : []
488
+
489
+ vc_ancestor_calls.unshift(instance_method(:render_template_for))
490
+ child.instance_variable_set(:@__vc_ancestor_calls, vc_ancestor_calls)
491
+ end
492
+
462
493
  super
463
494
  end
464
495
 
@@ -56,17 +56,17 @@ module ViewComponent
56
56
  RUBY
57
57
  # rubocop:enable Style/EvalWithLocation
58
58
 
59
+ component_class.define_method("_call_#{safe_class_name}", component_class.instance_method(:call))
60
+
59
61
  component_class.silence_redefinition_of_method("render_template_for")
60
62
  component_class.class_eval <<-RUBY, __FILE__, __LINE__ + 1
61
63
  def render_template_for(variant = nil)
62
- call
64
+ _call_#{safe_class_name}
63
65
  end
64
66
  RUBY
65
67
  end
66
68
  else
67
69
  templates.each do |template|
68
- # Remove existing compiled template methods,
69
- # as Ruby warns when redefining a method.
70
70
  method_name = call_method_name(template[:variant])
71
71
 
72
72
  redefinition_lock.synchronize do
@@ -95,15 +95,20 @@ module ViewComponent
95
95
 
96
96
  def define_render_template_for
97
97
  variant_elsifs = variants.compact.uniq.map do |variant|
98
- "elsif variant.to_sym == :'#{variant}'\n #{call_method_name(variant)}"
98
+ safe_name = "_call_variant_#{normalized_variant_name(variant)}_#{safe_class_name}"
99
+ component_class.define_method(safe_name, component_class.instance_method(call_method_name(variant)))
100
+
101
+ "elsif variant.to_sym == :'#{variant}'\n #{safe_name}"
99
102
  end.join("\n")
100
103
 
104
+ component_class.define_method("_call_#{safe_class_name}", component_class.instance_method(:call))
105
+
101
106
  body = <<-RUBY
102
107
  if variant.nil?
103
- call
108
+ _call_#{safe_class_name}
104
109
  #{variant_elsifs}
105
110
  else
106
- call
111
+ _call_#{safe_class_name}
107
112
  end
108
113
  RUBY
109
114
 
@@ -276,12 +281,17 @@ module ViewComponent
276
281
  variant.to_s.gsub("-", "__").gsub(".", "___")
277
282
  end
278
283
 
284
+ def safe_class_name
285
+ @safe_class_name ||= component_class.name.underscore.gsub("/", "__")
286
+ end
287
+
279
288
  def should_compile_superclass?
280
- development? && templates.empty? && !has_inline_template? &&
281
- !(
282
- component_class.instance_methods(false).include?(:call) ||
283
- component_class.private_instance_methods(false).include?(:call)
284
- )
289
+ development? && templates.empty? && !has_inline_template? && !call_defined?
290
+ end
291
+
292
+ def call_defined?
293
+ component_class.instance_methods(false).include?(:call) ||
294
+ component_class.private_instance_methods(false).include?(:call)
285
295
  end
286
296
  end
287
297
  end
@@ -3,7 +3,7 @@
3
3
  module ViewComponent
4
4
  module VERSION
5
5
  MAJOR = 3
6
- MINOR = 4
6
+ MINOR = 5
7
7
  PATCH = 0
8
8
  PRE = nil
9
9
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: view_component
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.4.0
4
+ version: 3.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ViewComponent Team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-07-11 00:00:00.000000000 Z
11
+ date: 2023-07-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport