Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Rails view components #644

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Loading