Skip to content

Commit 28dae87

Browse files
support rendering partials in components
Co-Authored-By: Aaron Patterson <[email protected]>
1 parent b0586c7 commit 28dae87

File tree

4 files changed

+16
-1
lines changed

4 files changed

+16
-1
lines changed

lib/action_view/component/base.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class Base < ActionView::Base
3333
include ActiveSupport::Configurable
3434
include ActionController::RequestForgeryProtection
3535

36+
delegate :render, to: :view_context
37+
3638
# Entrypoint for rendering components. Called by ActionView::Base#render.
3739
#
3840
# view_context: ActionView context from calling view
@@ -61,6 +63,7 @@ class Base < ActionView::Base
6163
def render_in(view_context, *args, &block)
6264
self.class.compile
6365
self.controller = view_context.controller
66+
@view_context = view_context
6467
@view_renderer ||= view_context.view_renderer
6568
@lookup_context ||= view_context.lookup_context
6669
@view_flow ||= view_context.view_flow
@@ -143,7 +146,7 @@ def type
143146

144147
private
145148

146-
attr_reader :content
149+
attr_reader :content, :view_context
147150
end
148151
end
149152
end

test/action_view/component_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ def test_renders_url_helper
124124
assert_includes result.text, "http://test.host/"
125125
end
126126

127+
def test_renders_another_component
128+
result = render_inline(AnotherComponent)
129+
130+
assert_equal trim_result(result.css("div").first.to_html), "<div>hello,world!</div>"
131+
end
132+
127133
def test_template_changes_are_not_reflected_in_production
128134
ActionView::Base.cache_template_loading = true
129135

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<%= render(MyComponent) %>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# frozen_string_literal: true
2+
3+
class AnotherComponent < ActionView::Component::Base
4+
def initialize(*); end
5+
end

0 commit comments

Comments
 (0)