Skip to content

Commit 481a99e

Browse files
committed
Add support for view components
View components are using a block to be able to render slots.
1 parent 5526517 commit 481a99e

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

lib/arbre/rails/rendering.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ module Arbre
33
module Rails
44
module Rendering
55

6-
def render(*args)
7-
rendered = helpers.render(*args)
6+
def render(*args, &block)
7+
rendered = helpers.render(*args, &block)
88
case rendered
99
when Arbre::Context
1010
current_arbre_element.add_child rendered

spec/rails/integration/rendering_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ def render_partial_with_instance_variable
3131
@my_instance_var = "From Instance Var"
3232
render "arbre/page_with_arb_partial_and_assignment"
3333
end
34+
35+
def render_with_block
36+
render "arbre/page_with_render_with_block"
37+
end
3438
end
3539

3640
RSpec.describe TestController, "Rendering with Arbre", type: :request do
@@ -45,6 +49,7 @@ def render_partial_with_instance_variable
4549
get 'test/render_with_instance_variable', controller: "test"
4650
get 'test/render_partial_with_instance_variable', controller: "test"
4751
get 'test/render_page_with_helpers', controller: "test"
52+
get 'test/render_with_block', controller: "test"
4853
end
4954
end
5055

@@ -96,4 +101,14 @@ def render_partial_with_instance_variable
96101
expect(body).to have_css("p", text: "Partial: From Instance Var")
97102
end
98103

104+
it "renders with a block" do
105+
get "/test/render_with_block"
106+
expect(response).to be_successful
107+
expect(body).to eq <<~HTML
108+
<h1>Before Render</h1>
109+
Hello from a render block
110+
<h2>After Render</h2>
111+
HTML
112+
end
113+
99114
end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# frozen_string_literal: true
2+
render_in_object = Class.new do
3+
def render_in(_, &block)
4+
block.call
5+
end
6+
7+
def method_missing(*args)
8+
''
9+
end
10+
end.new
11+
12+
h1 "Before Render"
13+
render render_in_object do
14+
"Hello from a render block\n"
15+
end
16+
h2 "After Render"

0 commit comments

Comments
 (0)