Skip to content

Commit 32dafc2

Browse files
committed
view component lookup_context cache in class attribute
1 parent d098bf5 commit 32dafc2

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

lib/view_component/base.rb

+10-4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ class Base < ActionView::Base
2424
class_attribute :slots
2525
self.slots = {}
2626

27+
class_attribute :vc_lookup_context
28+
self.vc_lookup_context = ActionView::LookupContext.new(ActionView::PathSet.new([Rails.root.join("app/components")]), {}, [])
29+
30+
# For rails 5.2 and older
31+
class_attribute :vc_view_renderer
32+
@view_renderer = ActionView::Renderer.new(vc_lookup_context)
33+
2734
# Entrypoint for rendering components.
2835
#
2936
# view_context: ActionView context from calling view
@@ -52,9 +59,8 @@ def render_in(view_context, &block)
5259
self.class.compile(raise_errors: true)
5360

5461
@view_context = view_context
55-
@lookup_context ||= view_context.lookup_context.tap do |c|
56-
c.view_paths.unshift(Rails.root.join("app/components"))
57-
end
62+
@lookup_context = vc_lookup_context
63+
@view_renderer = vc_view_renderer
5864

5965
# required for path helpers in older Rails versions
6066
@view_renderer ||= view_context.view_renderer
@@ -66,7 +72,7 @@ def render_in(view_context, &block)
6672
@virtual_path ||= virtual_path
6773

6874
# For template variants (+phone, +desktop, etc.)
69-
@variant ||= @lookup_context.variants.first
75+
@variant ||= view_context.lookup_context.variants.first
7076

7177
# For caching, such as #cache_if
7278
@current_template = nil unless defined?(@current_template)

0 commit comments

Comments
 (0)