mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Rename _assigns to view_assigns in AV::TC
- also add tests - also deprecate _assigns [#5751 state:resolved] Signed-off-by: Santiago Pastorino <santiago@wyeworks.com>
This commit is contained in:
parent
49cc01002e
commit
f656796d05
2 changed files with 52 additions and 9 deletions
|
@ -103,7 +103,7 @@ module ActionView
|
||||||
end
|
end
|
||||||
|
|
||||||
def render(options = {}, local_assigns = {}, &block)
|
def render(options = {}, local_assigns = {}, &block)
|
||||||
view.assign(_assigns)
|
view.assign(view_assigns)
|
||||||
@rendered << output = view.render(options, local_assigns, &block)
|
@rendered << output = view.render(options, local_assigns, &block)
|
||||||
output
|
output
|
||||||
end
|
end
|
||||||
|
@ -169,15 +169,19 @@ module ActionView
|
||||||
|
|
||||||
alias_method :_view, :view
|
alias_method :_view, :view
|
||||||
|
|
||||||
EXCLUDE_IVARS = %w{
|
INTERNAL_IVARS = %w{
|
||||||
|
@__name__
|
||||||
@_assertion_wrapped
|
@_assertion_wrapped
|
||||||
|
@_assertions
|
||||||
@_result
|
@_result
|
||||||
|
@_routes
|
||||||
@controller
|
@controller
|
||||||
@layouts
|
@layouts
|
||||||
@locals
|
@locals
|
||||||
@method_name
|
@method_name
|
||||||
@output_buffer
|
@output_buffer
|
||||||
@partials
|
@partials
|
||||||
|
@passed
|
||||||
@rendered
|
@rendered
|
||||||
@request
|
@request
|
||||||
@routes
|
@routes
|
||||||
|
@ -187,16 +191,24 @@ module ActionView
|
||||||
@view_context_class
|
@view_context_class
|
||||||
}
|
}
|
||||||
|
|
||||||
def _instance_variables
|
def _user_defined_ivars
|
||||||
instance_variables.map(&:to_s) - EXCLUDE_IVARS
|
instance_variables.map(&:to_s) - INTERNAL_IVARS
|
||||||
|
end
|
||||||
|
|
||||||
|
# Returns a Hash of instance variables and their values, as defined by
|
||||||
|
# the user in the test case, which are then assigned to the view being
|
||||||
|
# rendered. This is generally intended for internal use and extension
|
||||||
|
# frameworks.
|
||||||
|
def view_assigns
|
||||||
|
_user_defined_ivars.inject({}) do |hash, var|
|
||||||
|
hash.merge(var.sub('@','').to_sym => instance_variable_get(var))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def _assigns
|
def _assigns
|
||||||
_instance_variables.inject({}) do |hash, var|
|
ActiveSupport::Deprecation.warn "ActionView::TestCase#_assigns is deprecated and will be removed in future versions. " <<
|
||||||
name = var[1..-1].to_sym
|
"Please use view_assigns instead."
|
||||||
hash[name] = instance_variable_get(var)
|
view_assigns
|
||||||
hash
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def _routes
|
def _routes
|
|
@ -116,6 +116,37 @@ module ActionView
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class AssignsTest < ActionView::TestCase
|
||||||
|
setup do
|
||||||
|
ActiveSupport::Deprecation.stubs(:warn)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "_assigns delegates to user_defined_ivars" do
|
||||||
|
self.expects(:view_assigns)
|
||||||
|
_assigns
|
||||||
|
end
|
||||||
|
|
||||||
|
test "_assigns is deprecated" do
|
||||||
|
ActiveSupport::Deprecation.expects(:warn)
|
||||||
|
_assigns
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class ViewAssignsTest < ActionView::TestCase
|
||||||
|
test "view_assigns returns a Hash of user defined ivars" do
|
||||||
|
@a = 'b'
|
||||||
|
@c = 'd'
|
||||||
|
assert_equal({:a => 'b', :c => 'd'}, view_assigns)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "view_assigns excludes internal ivars" do
|
||||||
|
INTERNAL_IVARS.each do |ivar|
|
||||||
|
assert defined?(ivar), "expected #{ivar} to be defined"
|
||||||
|
assert !view_assigns.keys.include?(ivar.sub('@','').to_sym), "expected #{ivar} to be excluded from view_assigns"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class HelperExposureTest < ActionView::TestCase
|
class HelperExposureTest < ActionView::TestCase
|
||||||
helper(Module.new do
|
helper(Module.new do
|
||||||
def render_from_helper
|
def render_from_helper
|
Loading…
Reference in a new issue