-
-
Notifications
You must be signed in to change notification settings - Fork 78
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
Render helper with block containing Arbre element. #64
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
ActionView::Base.class_eval do | ||
def capture(*args) | ||
value = nil | ||
buffer = with_output_buffer { value = yield(*args) } | ||
|
||
# Override to handle Arbre elements inside helper blocks. | ||
# See https://github.com/rails/rails/issues/17661 | ||
# and https://github.com/rails/rails/pull/18024#commitcomment-8975180 | ||
value = value.to_s if value.is_a?(Arbre::Element) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Copied ActionView::Helpers::CaptureHelper.capture and added this line to convert Arbre::Element#to_s so the output is not lost. |
||
|
||
if (string = buffer.presence || value) && string.is_a?(String) | ||
ERB::Util.html_escape string | ||
end | ||
end | ||
end | ||
|
||
module Arbre | ||
module Rails | ||
class TemplateHandler | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,10 @@ def render_partial_with_instance_variable | |
@my_instance_var = "From Instance Var" | ||
render "arbre/page_with_arb_partial_and_assignment" | ||
end | ||
|
||
def render_page_with_helpers | ||
render "arbre/page_with_helpers" | ||
end | ||
end | ||
|
||
|
||
|
@@ -80,4 +84,15 @@ def render_partial_with_instance_variable | |
expect(body).to have_selector("p", text: "Partial: From Instance Var") | ||
end | ||
|
||
it "should render a page with helpers" do | ||
get "/test/render_page_with_helpers" | ||
expect(response).to be_success | ||
expect(body).to eq <<EOS | ||
<span>before h1 link</span> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use 2 spaces for indentation in a heredoc by using <<~ instead of <<. |
||
<h1><a href="/h1_link_path">h1 link text</a></h1> | ||
<span>before link_to block</span> | ||
<a href="/link_path"> <i class=\"link-class\">Link text</i> | ||
</a><span>at end</span> | ||
EOS | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
span { 'before h1 link' } | ||
h1 { link_to('/h1_link_path') { 'h1 link text' } } | ||
|
||
span { 'before link_to block' } | ||
text_node link_to('/link_path') { i("Link text", class: 'link-class') } | ||
|
||
span { 'at end' } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps this should be moved to its own file, named something like
lib/arbre/rails/capture.rb