mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Deprecate render locals to be assigned to instance variables
Rails partial rendering allows assigning instance variables. For example: render 'partial', :@name => "Maceo" This sets @name to "Maceo" on the ActionView::Base object. The allowed instance variables aren't restricted to the user's defined instance variables but can also override private Rails variables like @_assigns, @output_buffer, @_config, and @_default_form_builder.
This commit is contained in:
parent
1a60826c9d
commit
96d72d9b25
3 changed files with 15 additions and 1 deletions
|
@ -1,3 +1,7 @@
|
||||||
|
* Deprecate `render` locals to be assigned to instance variables.
|
||||||
|
|
||||||
|
*Petrik de Heus*
|
||||||
|
|
||||||
* Remove legacy default `media=screen` from `stylesheet_link_tag`.
|
* Remove legacy default `media=screen` from `stylesheet_link_tag`.
|
||||||
|
|
||||||
*André Luis Leal Cardoso Junior*
|
*André Luis Leal Cardoso Junior*
|
||||||
|
|
|
@ -319,6 +319,13 @@ module ActionView
|
||||||
# Only locals with valid variable names get set directly. Others will
|
# Only locals with valid variable names get set directly. Others will
|
||||||
# still be available in local_assigns.
|
# still be available in local_assigns.
|
||||||
locals = @locals - Module::RUBY_RESERVED_KEYWORDS
|
locals = @locals - Module::RUBY_RESERVED_KEYWORDS
|
||||||
|
deprecated_locals = locals.grep(/\A@+/)
|
||||||
|
if deprecated_locals.any?
|
||||||
|
ActiveSupport::Deprecation.warn(<<~MSG)
|
||||||
|
Passing instance variables to `render` is deprecated.
|
||||||
|
In Rails 7.0, #{deprecated_locals.to_sentence} will be ignored.
|
||||||
|
MSG
|
||||||
|
end
|
||||||
locals = locals.grep(/\A@?(?![A-Z0-9])(?:[[:alnum:]_]|[^\0-\177])+\z/)
|
locals = locals.grep(/\A@?(?![A-Z0-9])(?:[[:alnum:]_]|[^\0-\177])+\z/)
|
||||||
|
|
||||||
# Assign for the same variable is to suppress unused variable warning
|
# Assign for the same variable is to suppress unused variable warning
|
||||||
|
|
|
@ -52,8 +52,11 @@ class CompiledTemplatesTest < ActiveSupport::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_template_with_instance_variable_identifier
|
def test_template_with_instance_variable_identifier
|
||||||
|
expected_deprecation = "In Rails 7.0, @foo will be ignored."
|
||||||
|
assert_deprecated(expected_deprecation) do
|
||||||
assert_equal "bar", render(template: "test/render_file_instance_variable", locals: { "@foo": "bar" })
|
assert_equal "bar", render(template: "test/render_file_instance_variable", locals: { "@foo": "bar" })
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def test_template_gets_recompiled_when_using_different_keys_in_local_assigns
|
def test_template_gets_recompiled_when_using_different_keys_in_local_assigns
|
||||||
assert_equal "one", render(template: "test/render_file_with_locals_and_default")
|
assert_equal "one", render(template: "test/render_file_with_locals_and_default")
|
||||||
|
|
Loading…
Reference in a new issue