mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Track rendered templates in stack so the current template can always be accessed. Added ActionView::Base#template to access the template object.
This commit is contained in:
parent
0f651aec4e
commit
ac50ee0edf
5 changed files with 23 additions and 7 deletions
|
@ -218,7 +218,7 @@ module ActionController #:nodoc:
|
||||||
# Returns the template of the file which was used to
|
# Returns the template of the file which was used to
|
||||||
# render this response (or nil)
|
# render this response (or nil)
|
||||||
def rendered_template
|
def rendered_template
|
||||||
template.send(:_first_render)
|
template.instance_variable_get(:@_first_render)
|
||||||
end
|
end
|
||||||
|
|
||||||
# A shortcut to the flash. Returns an empty hash if no session flash exists.
|
# A shortcut to the flash. Returns an empty hash if no session flash exists.
|
||||||
|
|
|
@ -222,6 +222,7 @@ module ActionView #:nodoc:
|
||||||
def initialize(view_paths = [], assigns_for_first_render = {}, controller = nil)#:nodoc:
|
def initialize(view_paths = [], assigns_for_first_render = {}, controller = nil)#:nodoc:
|
||||||
@assigns = assigns_for_first_render
|
@assigns = assigns_for_first_render
|
||||||
@assigns_added = nil
|
@assigns_added = nil
|
||||||
|
@_render_stack = []
|
||||||
@controller = controller
|
@controller = controller
|
||||||
@helpers = ProxyModule.new(self)
|
@helpers = ProxyModule.new(self)
|
||||||
self.view_paths = view_paths
|
self.view_paths = view_paths
|
||||||
|
@ -271,9 +272,13 @@ module ActionView #:nodoc:
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
# Access the current template being rendered.
|
||||||
attr_accessor :_first_render, :_last_render
|
# Returns a ActionView::Template object.
|
||||||
|
def template
|
||||||
|
@_render_stack.last
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
# Evaluates the local assigns and controller ivars, pushes them to the view.
|
# Evaluates the local assigns and controller ivars, pushes them to the view.
|
||||||
def _evaluate_assigns_and_ivars #:nodoc:
|
def _evaluate_assigns_and_ivars #:nodoc:
|
||||||
unless @assigns_added
|
unless @assigns_added
|
||||||
|
@ -312,7 +317,7 @@ module ActionView #:nodoc:
|
||||||
template
|
template
|
||||||
elsif template = self.view_paths[template_file_name]
|
elsif template = self.view_paths[template_file_name]
|
||||||
template
|
template
|
||||||
elsif _first_render && template = self.view_paths["#{template_file_name}.#{_first_render.format_and_extension}"]
|
elsif @_render_stack.first && template = self.view_paths["#{template_file_name}.#{@_render_stack.first.format_and_extension}"]
|
||||||
template
|
template
|
||||||
elsif template_format == :js && template = self.view_paths["#{template_file_name}.html"]
|
elsif template_format == :js && template = self.view_paths["#{template_file_name}.html"]
|
||||||
@template_format = :html
|
@template_format = :html
|
||||||
|
|
|
@ -25,13 +25,16 @@ module ActionView
|
||||||
def render(view, local_assigns = {})
|
def render(view, local_assigns = {})
|
||||||
compile(local_assigns)
|
compile(local_assigns)
|
||||||
|
|
||||||
view.send(:_first_render=, self) unless view.send(:_first_render)
|
stack = view.instance_variable_get(:@_render_stack)
|
||||||
view.send(:_last_render=, self)
|
stack.push(self)
|
||||||
|
|
||||||
|
# This is only used for TestResponse to set rendered_template
|
||||||
|
view.instance_variable_set(:@_first_render, self) unless view.instance_variable_get(:@_first_render)
|
||||||
|
|
||||||
view.send(:_evaluate_assigns_and_ivars)
|
view.send(:_evaluate_assigns_and_ivars)
|
||||||
view.send(:_set_controller_content_type, mime_type) if respond_to?(:mime_type)
|
view.send(:_set_controller_content_type, mime_type) if respond_to?(:mime_type)
|
||||||
|
|
||||||
view.send(method_name(local_assigns), local_assigns) do |*names|
|
result = view.send(method_name(local_assigns), local_assigns) do |*names|
|
||||||
ivar = :@_proc_for_layout
|
ivar = :@_proc_for_layout
|
||||||
if view.instance_variable_defined?(ivar) and proc = view.instance_variable_get(ivar)
|
if view.instance_variable_defined?(ivar) and proc = view.instance_variable_get(ivar)
|
||||||
view.capture(*names, &proc)
|
view.capture(*names, &proc)
|
||||||
|
@ -39,6 +42,9 @@ module ActionView
|
||||||
view.instance_variable_get(ivar)
|
view.instance_variable_get(ivar)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
stack.pop
|
||||||
|
result
|
||||||
end
|
end
|
||||||
|
|
||||||
def method_name(local_assigns)
|
def method_name(local_assigns)
|
||||||
|
|
1
actionpack/test/fixtures/test/template.erb
vendored
Normal file
1
actionpack/test/fixtures/test/template.erb
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
<%= template.path %>
|
|
@ -41,6 +41,10 @@ class ViewRenderTest < Test::Unit::TestCase
|
||||||
assert_equal "The secret is in the sauce\n", @view.render("test/dot.directory/render_file_with_ivar")
|
assert_equal "The secret is in the sauce\n", @view.render("test/dot.directory/render_file_with_ivar")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_render_has_access_current_template
|
||||||
|
assert_equal "test/template.erb", @view.render("test/template.erb")
|
||||||
|
end
|
||||||
|
|
||||||
def test_render_update
|
def test_render_update
|
||||||
# TODO: You should not have to stub out template because template is self!
|
# TODO: You should not have to stub out template because template is self!
|
||||||
@view.instance_variable_set(:@template, @view)
|
@view.instance_variable_set(:@template, @view)
|
||||||
|
|
Loading…
Reference in a new issue