diff options
author | Andrew White <andrew.white@unboxed.co> | 2017-01-15 13:41:39 +0000 |
---|---|---|
committer | Andrew White <andrew.white@unboxed.co> | 2017-01-15 13:41:39 +0000 |
commit | b5edc55d9fc3f6fc87ab06b5897917e051a82aef (patch) | |
tree | db6b8adacb9552cf7b67467d4a266af5f4562d23 | |
parent | eec4c2e978be15b85ba8f3efa054cb43acf1bdda (diff) | |
download | rails-b5edc55d9fc3f6fc87ab06b5897917e051a82aef.tar.gz rails-b5edc55d9fc3f6fc87ab06b5897917e051a82aef.tar.bz2 rails-b5edc55d9fc3f6fc87ab06b5897917e051a82aef.zip |
Allow render locals to be assigned to instance variables
In #26672 we blocked use of Ruby keywords as identifiers for view
locals but inadvertently broke the use of instance variable names
as identifiers. Whilst not explicitly documented this behavior has
been around for a long time and there's no need to break it now.
Fixes #27480.
-rw-r--r-- | actionview/CHANGELOG.md | 6 | ||||
-rw-r--r-- | actionview/lib/action_view/template.rb | 2 | ||||
-rw-r--r-- | actionview/test/fixtures/test/render_file_instance_variable.erb | 1 | ||||
-rw-r--r-- | actionview/test/template/compiled_templates_test.rb | 4 |
4 files changed, 12 insertions, 1 deletions
diff --git a/actionview/CHANGELOG.md b/actionview/CHANGELOG.md index 59afed1f98..c12fb2e5ae 100644 --- a/actionview/CHANGELOG.md +++ b/actionview/CHANGELOG.md @@ -1,3 +1,9 @@ +* Allow render locals to be assigned to instance variables in a view. + + Fixes #27480. + + *Andrew White* + * Add `check_parameters` option to `current_page?` which makes it more strict. *Maksym Pugach* diff --git a/actionview/lib/action_view/template.rb b/actionview/lib/action_view/template.rb index 4b793c3b16..c067031d2d 100644 --- a/actionview/lib/action_view/template.rb +++ b/actionview/lib/action_view/template.rb @@ -326,7 +326,7 @@ module ActionView # Only locals with valid variable names get set directly. Others will # still be available in local_assigns. locals = @locals - Module::RUBY_RESERVED_KEYWORDS - locals = locals.grep(/\A(?![A-Z0-9])(?:[[:alnum:]_]|[^\0-\177])+\z/) + locals = locals.grep(/\A@?(?![A-Z0-9])(?:[[:alnum:]_]|[^\0-\177])+\z/) # Double assign to suppress the dreaded 'assigned but unused variable' warning locals.each_with_object("") { |key, code| code << "#{key} = #{key} = local_assigns[:#{key}];" } diff --git a/actionview/test/fixtures/test/render_file_instance_variable.erb b/actionview/test/fixtures/test/render_file_instance_variable.erb new file mode 100644 index 0000000000..5344ac8a66 --- /dev/null +++ b/actionview/test/fixtures/test/render_file_instance_variable.erb @@ -0,0 +1 @@ +<%= @foo %>
\ No newline at end of file diff --git a/actionview/test/template/compiled_templates_test.rb b/actionview/test/template/compiled_templates_test.rb index 40ac867b38..adb2be9be4 100644 --- a/actionview/test/template/compiled_templates_test.rb +++ b/actionview/test/template/compiled_templates_test.rb @@ -38,6 +38,10 @@ class CompiledTemplatesTest < ActiveSupport::TestCase assert_equal "🎂", render(file: "test/render_file_unicode_local", locals: { 🎃: "🎂" }) end + def test_template_with_instance_variable_identifier + assert_equal "bar", render(file: "test/render_file_instance_variable", locals: { "@foo": "bar" }) + end + def test_template_gets_recompiled_when_using_different_keys_in_local_assigns assert_equal "one", render(file: "test/render_file_with_locals_and_default") assert_equal "two", render(file: "test/render_file_with_locals_and_default", locals: { secret: "two" }) |