1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Improve error handling when using partial name with hyphen. Fix #7079

This commit is contained in:
kennyj 2012-07-18 18:02:33 +09:00
parent b33e7ba140
commit a8d68d89e3
2 changed files with 19 additions and 7 deletions

View file

@ -344,12 +344,6 @@ module ActionView
paths.map! { |path| retrieve_variable(path).unshift(path) }
end
if String === partial && @variable.to_s !~ /^[a-z_][a-zA-Z_0-9]*$/
raise ArgumentError.new("The partial name (#{partial}) is not a valid Ruby identifier; " +
"make sure your partial name starts with a lowercase letter or underscore, " +
"and is followed by any combination of letters, numbers and underscores.")
end
self
end
@ -456,8 +450,19 @@ module ActionView
keys
end
IDENTIFIER_ERROR_MESSAGE = "The partial name (%s) is not a valid Ruby identifier; " +
"make sure your partial name starts with a lowercase letter or underscore, " +
"and is followed by any combination of letters, numbers and underscores."
def retrieve_variable(path)
variable = @options.fetch(:as) { path[%r'_?(\w+)(\.\w+)*$', 1] }.try(:to_sym)
variable = if as = @options[:as]
raise ArgumentError.new(IDENTIFIER_ERROR_MESSAGE % (path)) unless as.to_s =~ /\A[a-z_]\w*\z/
as.to_sym
else
base = path[-1] == "/" ? "" : File.basename(path)
raise ArgumentError.new(IDENTIFIER_ERROR_MESSAGE % (path)) unless base =~ /\A_?([a-z]\w*)(\.\w+)*\z/
$1.to_sym
end
variable_counter = :"#{variable}_counter" if @collection
[variable, variable_counter]
end

View file

@ -187,6 +187,13 @@ module RenderTestCases
assert_equal "'#{nil.inspect}' is not an ActiveModel-compatible object. It must implement :to_partial_path.", e.message
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 a lowercase letter or underscore, " +
"and is followed by any combination of letters, numbers and underscores.", e.message
end
def test_render_partial_with_errors
e = assert_raises(ActionView::Template::Error) { @view.render(:partial => "test/raise") }
assert_match %r!method.*doesnt_exist!, e.message