Skip to content

Commit 22a2148

Browse files
committed
view component lookup_context cache in class attribute, support rails 6+
1 parent d098bf5 commit 22a2148

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

lib/view_component/base.rb

+5-4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ 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+
2730
# Entrypoint for rendering components.
2831
#
2932
# view_context: ActionView context from calling view
@@ -52,9 +55,7 @@ def render_in(view_context, &block)
5255
self.class.compile(raise_errors: true)
5356

5457
@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
58+
@lookup_context = vc_lookup_context
5859

5960
# required for path helpers in older Rails versions
6061
@view_renderer ||= view_context.view_renderer
@@ -66,7 +67,7 @@ def render_in(view_context, &block)
6667
@virtual_path ||= virtual_path
6768

6869
# For template variants (+phone, +desktop, etc.)
69-
@variant ||= @lookup_context.variants.first
70+
@variant ||= view_context.lookup_context.variants.first
7071

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

test/view_component/view_component_test.rb

+7-4
Original file line numberDiff line numberDiff line change
@@ -672,11 +672,14 @@ def test_inherited_inline_component_inherits_inline_method
672672
assert_selector("input[type='text'][name='name']")
673673
end
674674

675-
def test_cache_digest
676-
cache_key = render_inline(CacheComponent.new).text
675+
if Rails.version.to_f >= 6.0
676+
def test_cache_digest
677+
cache_key = render_inline(CacheComponent.new).text
677678

678-
assert_includes cache_key, "cache-key"
679-
assert_match(/\w{32}/, cache_key.sub("cache-key", ""))
679+
assert_includes cache_key, "cache-key"
680+
681+
assert_match(/\w{32}/, cache_key.sub("cache-key", ""))
682+
end
680683
end
681684

682685
end

0 commit comments

Comments
 (0)