Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deprecate block_called_from_erb? pending a solution for getting it in…
…to apps
- Loading branch information
Showing
9 changed files
with
154 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
actionpack/lib/action_view/helpers/deprecated_block_helpers.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
module ActionView | ||
module Helpers | ||
module DeprecatedBlockHelpers | ||
extend ActiveSupport::Concern | ||
|
||
include ActionView::Helpers::TagHelper | ||
include ActionView::Helpers::TextHelper | ||
include ActionView::Helpers::JavaScriptHelper | ||
|
||
def content_tag(*, &block) | ||
block_called_from_erb?(block) ? safe_concat(super) : super | ||
end | ||
|
||
def javascript_tag(*, &block) | ||
block_called_from_erb?(block) ? safe_concat(super) : super | ||
end | ||
|
||
BLOCK_CALLED_FROM_ERB = 'defined? __in_erb_template' | ||
|
||
if RUBY_VERSION < '1.9.0' | ||
# Check whether we're called from an erb template. | ||
# We'd return a string in any other case, but erb <%= ... %> | ||
# can't take an <% end %> later on, so we have to use <% ... %> | ||
# and implicitly concat. | ||
def block_called_from_erb?(block) | ||
block && eval(BLOCK_CALLED_FROM_ERB, block) | ||
end | ||
else | ||
def block_called_from_erb?(block) | ||
block && eval(BLOCK_CALLED_FROM_ERB, block.binding) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
require "abstract_unit" | ||
|
||
module ERBTest | ||
module SharedTagHelpers | ||
extend ActiveSupport::Testing::Declarative | ||
|
||
def render_content(start, inside) | ||
template = block_helper(start, inside) | ||
ActionView::Template::Handlers::Erubis.new(template).evaluate(context.new) | ||
end | ||
|
||
test "percent equals works for content_tag" do | ||
assert_equal "<div>Hello world</div>", render_content("content_tag(:div)", "Hello world") | ||
end | ||
|
||
test "percent equals works for javascript_tag" do | ||
expected_output = "<script type=\"text/javascript\">\n//<![CDATA[\nalert('Hello')\n//]]>\n</script>" | ||
assert_equal expected_output, render_content("javascript_tag", "alert('Hello')") | ||
end | ||
|
||
test "percent equals works for javascript_tag with options" do | ||
expected_output = "<script id=\"the_js_tag\" type=\"text/javascript\">\n//<![CDATA[\nalert('Hello')\n//]]>\n</script>" | ||
assert_equal expected_output, render_content("javascript_tag(:id => 'the_js_tag')", "alert('Hello')") | ||
end | ||
end | ||
|
||
class TagHelperTest < ActiveSupport::TestCase | ||
def context | ||
Class.new do | ||
include ActionView::Helpers::TagHelper | ||
include ActionView::Helpers::JavaScriptHelper | ||
|
||
attr_accessor :output_buffer | ||
end | ||
end | ||
|
||
def block_helper(str, rest) | ||
"<%= #{str} do %>#{rest}<% end %>" | ||
end | ||
|
||
include SharedTagHelpers | ||
end | ||
|
||
class DeprecatedTagHelperTest < ActiveSupport::TestCase | ||
def context | ||
Class.new do | ||
include ActionView::Helpers::TagHelper | ||
include ActionView::Helpers::JavaScriptHelper | ||
include ActionView::Helpers::DeprecatedBlockHelpers | ||
attr_accessor :output_buffer | ||
end | ||
end | ||
|
||
def block_helper(str, rest) | ||
"<% __in_erb_template=true %><% #{str} do %>#{rest}<% end %>" | ||
end | ||
|
||
include SharedTagHelpers | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters