In ActionView::TestCase::Behavior, assign variables right before

rendering the view.

- Previously, _assigns were locked down the first time _view was
  referenced.

[#4931 state:resolved]

Signed-off-by: José Valim <jose.valim@gmail.com>
This commit is contained in:
David Chelimsky 2010-06-23 07:59:56 -05:00 committed by José Valim
parent e8c064bbe0
commit 0e0df4b0c5
3 changed files with 16 additions and 2 deletions

View File

@ -204,8 +204,12 @@ module ActionView #:nodoc:
value.dup : ActionView::PathSet.new(Array.wrap(value))
end
def assign(new_assigns) # :nodoc:
self.assigns = new_assigns.each { |key, value| instance_variable_set("@#{key}", value) }
end
def initialize(lookup_context = nil, assigns_for_first_render = {}, controller = nil, formats = nil) #:nodoc:
self.assigns = assigns_for_first_render.each { |key, value| instance_variable_set("@#{key}", value) }
assign(assigns_for_first_render)
self.helpers = self.class.helpers || Module.new
if @_controller = controller

View File

@ -99,6 +99,7 @@ module ActionView
end
def render(options = {}, local_assigns = {}, &block)
_view.assign(_assigns)
@rendered << output = _view.render(options, local_assigns, &block)
output
end
@ -147,7 +148,7 @@ module ActionView
def _view
@_view ||= begin
view = ActionView::Base.new(ActionController::Base.view_paths, _assigns, @controller)
view = ActionView::Base.new(ActionController::Base.view_paths, {}, @controller)
view.singleton_class.send :include, _helpers
view.singleton_class.send :include, @controller._router.url_helpers
view.singleton_class.send :delegate, :alert, :notice, :to => "request.flash"

View File

@ -200,6 +200,15 @@ module ActionView
assert_match /Hello: EloyHello: Manfred/, render(:file => 'test/list')
end
test "is able to render partials from templates and also use instance variables after _view has been referenced" do
@controller.controller_path = "test"
_view
@customers = [stub(:name => 'Eloy'), stub(:name => 'Manfred')]
assert_match /Hello: EloyHello: Manfred/, render(:file => 'test/list')
end
end
class AssertionsTest < ActionView::TestCase