Skip to content

Commit

Permalink
Add support for Rails view components (#644)
Browse files Browse the repository at this point in the history
Add support for view components

View components are using a block to be able to render slots.
  • Loading branch information
budu authored Dec 9, 2024
1 parent 5526517 commit 66e6c0b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/arbre/rails/rendering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module Arbre
module Rails
module Rendering

def render(*args)
rendered = helpers.render(*args)
def render(*args, &block)
rendered = helpers.render(*args, &block)
case rendered
when Arbre::Context
current_arbre_element.add_child rendered
Expand Down
15 changes: 15 additions & 0 deletions spec/rails/integration/rendering_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def render_partial_with_instance_variable
@my_instance_var = "From Instance Var"
render "arbre/page_with_arb_partial_and_assignment"
end

def render_with_block
render "arbre/page_with_render_with_block"
end
end

RSpec.describe TestController, "Rendering with Arbre", type: :request do
Expand All @@ -45,6 +49,7 @@ def render_partial_with_instance_variable
get 'test/render_with_instance_variable', controller: "test"
get 'test/render_partial_with_instance_variable', controller: "test"
get 'test/render_page_with_helpers', controller: "test"
get 'test/render_with_block', controller: "test"
end
end

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

it "renders with a block" do
get "/test/render_with_block"
expect(response).to be_successful
expect(body).to eq <<~HTML
<h1>Before Render</h1>
Hello from a render block
<h2>After Render</h2>
HTML
end

end
12 changes: 12 additions & 0 deletions spec/rails/templates/arbre/page_with_render_with_block.arb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true
render_in_object = Class.new do
def render_in(_, &block)
block.call
end
end.new

h1 "Before Render"
render render_in_object do
"Hello from a render block\n"
end
h2 "After Render"

0 comments on commit 66e6c0b

Please sign in to comment.