Skip to content

Commit

Permalink
In ActionView::TestCase::Behavior, assign variables right before
Browse files Browse the repository at this point in the history
rendering the view.

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

[rails#4931 state:resolved]
  • Loading branch information
dchelimsky committed Jun 23, 2010
1 parent 7008911 commit 1022209
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
6 changes: 5 additions & 1 deletion actionpack/lib/action_view/base.rb
Expand Up @@ -204,8 +204,12 @@ def self.process_view_paths(value)
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
Expand Down
3 changes: 2 additions & 1 deletion actionpack/lib/action_view/test_case.rb
Expand Up @@ -99,6 +99,7 @@ def config
end

def render(options = {}, local_assigns = {}, &block)
_view.assign(_assigns)
@rendered << output = _view.render(options, local_assigns, &block)
output
end
Expand Down Expand Up @@ -147,7 +148,7 @@ def _render_partial(options)

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"
Expand Down
9 changes: 9 additions & 0 deletions actionpack/test/template/test_case_test.rb
Expand Up @@ -200,6 +200,15 @@ def render_from_helper
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
Expand Down

0 comments on commit 1022209

Please sign in to comment.