Skip to content

Commit

Permalink
Partial template name does no more have to be a valid Ruby identifier
Browse files Browse the repository at this point in the history
because the partial renderer would not create an lvar per each template since c67005f
  • Loading branch information
amatsuda committed Feb 5, 2015
1 parent 4ca1dda commit da9038e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 16 deletions.
10 changes: 10 additions & 0 deletions actionview/CHANGELOG.md
@@ -1,3 +1,13 @@
* Partial template name does no more have to be a valid Ruby identifier.

There used to be a naming rule that the partial name should start with
underscore, and should be followed by any combination of letters, numbers
and underscores.
But now we can give our partials any name starting with underscore, such as
_🍔.html.erb.

*Akira Matsuda*

* Change the default template handler from `ERB` to `Raw`.

Files without a template handler in their extension will be rendered using the raw
Expand Down
5 changes: 2 additions & 3 deletions actionview/lib/action_view/renderer/partial_renderer.rb
Expand Up @@ -519,7 +519,7 @@ def retrieve_template_keys
def retrieve_variable(path, as)
variable = as || begin
base = path[-1] == "/" ? "" : File.basename(path)
raise_invalid_identifier(path) unless base =~ /\A_?([a-z]\w*)(\.\w+)*\z/
raise_invalid_identifier(path) unless base =~ /\A_?(.*)(?:\.\w+)*\z/
$1.to_sym
end
if @collection
Expand All @@ -530,8 +530,7 @@ def retrieve_variable(path, as)
end

IDENTIFIER_ERROR_MESSAGE = "The partial name (%s) is not a valid Ruby identifier; " +
"make sure your partial name starts with underscore, " +
"and is followed by any combination of letters, numbers and underscores."
"make sure your partial name starts with underscore."

OPTION_AS_ERROR_MESSAGE = "The value (%s) of the option `as` is not a valid Ruby identifier; " +
"make sure it starts with lowercase letter, " +
Expand Down
1 change: 1 addition & 0 deletions actionview/test/fixtures/test/_FooBar.html.erb
@@ -0,0 +1 @@
🍣
Empty file.
21 changes: 8 additions & 13 deletions actionview/test/template/render_test.rb
Expand Up @@ -171,30 +171,25 @@ def test_render_partial_with_locals_from_default
assert_equal "only partial", @view.render("test/partial_only", :counter_counter => 5)
end

def test_render_partial_with_invalid_name
e = assert_raises(ArgumentError) { @view.render(:partial => "test/200") }
assert_equal "The partial name (test/200) is not a valid Ruby identifier; " +
"make sure your partial name starts with underscore, " +
"and is followed by any combination of letters, numbers and underscores.", e.message
def test_render_partial_with_number
assert_nothing_raised { @view.render(:partial => "test/200") }
end

def test_render_partial_with_missing_filename
e = assert_raises(ArgumentError) { @view.render(:partial => "test/") }
assert_equal "The partial name (test/) is not a valid Ruby identifier; " +
"make sure your partial name starts with underscore, " +
"and is followed by any combination of letters, numbers and underscores.", e.message
assert_raises(ActionView::MissingTemplate) { @view.render(:partial => "test/") }
end

def test_render_partial_with_incompatible_object
e = assert_raises(ArgumentError) { @view.render(:partial => nil) }
assert_equal "'#{nil.inspect}' is not an ActiveModel-compatible object. It must implement :to_partial_path.", e.message
end

def test_render_partial_starting_with_a_capital
assert_nothing_raised { @view.render(:partial => 'test/FooBar') }
end

def test_render_partial_with_hyphen
e = assert_raises(ArgumentError) { @view.render(:partial => "test/a-in") }
assert_equal "The partial name (test/a-in) is not a valid Ruby identifier; " +
"make sure your partial name starts with underscore, " +
"and is followed by any combination of letters, numbers and underscores.", e.message
assert_nothing_raised { @view.render(:partial => "test/a-in") }
end

def test_render_partial_with_invalid_option_as
Expand Down

4 comments on commit da9038e

@tgxworld
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_:heart:.html.erb

@gustaflindqvist
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Love this update!

@andradei
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it, specially the testcase for it 🍣

@AbhimanyuAryan
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

Please sign in to comment.