Skip to content
This repository
Browse code

Track rendered templates in stack so the current template can always …

…be accessed. Added ActionView::Base#template to access the template object.
  • Loading branch information...
commit ac50ee0edfa0df90ae7a8dd09f4a41ecbd1c7a94 1 parent 0f651ae
Joshua Peek josh authored
2  actionpack/lib/action_controller/test_process.rb
@@ -218,7 +218,7 @@ def redirect_url_match?( pattern )
218 218 # Returns the template of the file which was used to
219 219 # render this response (or nil)
220 220 def rendered_template
221   - template.send(:_first_render)
  221 + template.instance_variable_get(:@_first_render)
222 222 end
223 223
224 224 # A shortcut to the flash. Returns an empty hash if no session flash exists.
11 actionpack/lib/action_view/base.rb
@@ -222,6 +222,7 @@ def include(*args)
222 222 def initialize(view_paths = [], assigns_for_first_render = {}, controller = nil)#:nodoc:
223 223 @assigns = assigns_for_first_render
224 224 @assigns_added = nil
  225 + @_render_stack = []
225 226 @controller = controller
226 227 @helpers = ProxyModule.new(self)
227 228 self.view_paths = view_paths
@@ -271,9 +272,13 @@ def template_format
271 272 end
272 273 end
273 274
274   - private
275   - attr_accessor :_first_render, :_last_render
  275 + # Access the current template being rendered.
  276 + # Returns a ActionView::Template object.
  277 + def template
  278 + @_render_stack.last
  279 + end
276 280
  281 + private
277 282 # Evaluates the local assigns and controller ivars, pushes them to the view.
278 283 def _evaluate_assigns_and_ivars #:nodoc:
279 284 unless @assigns_added
@@ -312,7 +317,7 @@ def _pick_template(template_path)
312 317 template
313 318 elsif template = self.view_paths[template_file_name]
314 319 template
315   - elsif _first_render && template = self.view_paths["#{template_file_name}.#{_first_render.format_and_extension}"]
  320 + elsif @_render_stack.first && template = self.view_paths["#{template_file_name}.#{@_render_stack.first.format_and_extension}"]
316 321 template
317 322 elsif template_format == :js && template = self.view_paths["#{template_file_name}.html"]
318 323 @template_format = :html
12 actionpack/lib/action_view/renderable.rb
@@ -25,13 +25,16 @@ def compiled_source
25 25 def render(view, local_assigns = {})
26 26 compile(local_assigns)
27 27
28   - view.send(:_first_render=, self) unless view.send(:_first_render)
29   - view.send(:_last_render=, self)
  28 + stack = view.instance_variable_get(:@_render_stack)
  29 + stack.push(self)
  30 +
  31 + # This is only used for TestResponse to set rendered_template
  32 + view.instance_variable_set(:@_first_render, self) unless view.instance_variable_get(:@_first_render)
30 33
31 34 view.send(:_evaluate_assigns_and_ivars)
32 35 view.send(:_set_controller_content_type, mime_type) if respond_to?(:mime_type)
33 36
34   - view.send(method_name(local_assigns), local_assigns) do |*names|
  37 + result = view.send(method_name(local_assigns), local_assigns) do |*names|
35 38 ivar = :@_proc_for_layout
36 39 if view.instance_variable_defined?(ivar) and proc = view.instance_variable_get(ivar)
37 40 view.capture(*names, &proc)
@@ -39,6 +42,9 @@ def render(view, local_assigns = {})
39 42 view.instance_variable_get(ivar)
40 43 end
41 44 end
  45 +
  46 + stack.pop
  47 + result
42 48 end
43 49
44 50 def method_name(local_assigns)
1  actionpack/test/fixtures/test/template.erb
... ... @@ -0,0 +1 @@
  1 +<%= template.path %>
4 actionpack/test/template/render_test.rb
@@ -41,6 +41,10 @@ def test_render_file_not_using_full_path_with_dot_in_path
41 41 assert_equal "The secret is in the sauce\n", @view.render("test/dot.directory/render_file_with_ivar")
42 42 end
43 43
  44 + def test_render_has_access_current_template
  45 + assert_equal "test/template.erb", @view.render("test/template.erb")
  46 + end
  47 +
44 48 def test_render_update
45 49 # TODO: You should not have to stub out template because template is self!
46 50 @view.instance_variable_set(:@template, @view)

0 comments on commit ac50ee0

Please sign in to comment.
Something went wrong with that request. Please try again.