Remove dependency from _template.

This commit is contained in:
José Valim
2011-05-01 11:14:38 +02:00
parent d08f65118c
commit bebaccdf4a
7 changed files with 26 additions and 25 deletions

View File

@@ -159,7 +159,7 @@ module ActionView #:nodoc:
end
end
attr_accessor :_template, :_view_flow
attr_accessor :_view_flow
attr_internal :request, :controller, :config, :assigns, :lookup_context
# TODO Consider removing those setters once we have the renderer in place.
@@ -191,6 +191,7 @@ module ActionView #:nodoc:
@_virtual_path = nil
@_view_flow = OutputFlow.new
@output_buffer = nil
@virtual_path = nil
if @_controller = controller
@_request = controller.request if controller.respond_to?(:request)

View File

@@ -1172,7 +1172,7 @@ module ActionView
class FormBuilder
# The methods which wrap a form helper call.
class_attribute :field_helpers
self.field_helpers = (FormHelper.instance_method_names - ['form_for'])
self.field_helpers = FormHelper.instance_method_names - %w(form_for convert_to_model)
attr_accessor :object_name, :object, :options

View File

@@ -63,8 +63,8 @@ module ActionView
private
def scope_key_by_partial(key)
if key.to_s.first == "."
if (path = @_template && @_template.virtual_path)
path.gsub(%r{/_?}, ".") + key.to_s
if @virtual_path
@virtual_path.gsub(%r{/_?}, ".") + key.to_s
else
raise "Cannot use t(#{key.inspect}) shortcut because path is not available"
end

View File

@@ -139,15 +139,12 @@ module ActionView
# we use a bang in this instrumentation because you don't want to
# consume this in production. This is only slow if it's being listened to.
def render(view, locals, buffer=nil, &block)
old_template, view._template = view._template, self
ActiveSupport::Notifications.instrument("!render_template.action_view", :virtual_path => @virtual_path) do
compile!(view)
view.send(method_name, locals, buffer, &block)
end
rescue Exception => e
handle_render_error(view, e)
ensure
view._template = old_template
end
def mime_type
@@ -174,12 +171,7 @@ module ActionView
end
def inspect
@inspect ||=
if defined?(Rails.root)
identifier.sub("#{Rails.root}/", '')
else
identifier
end
@inspect ||= defined?(Rails.root) ? identifier.sub("#{Rails.root}/", '') : identifier
end
protected
@@ -264,9 +256,9 @@ module ActionView
# encoding of the code
source = <<-end_src
def #{method_name}(local_assigns, output_buffer)
_old_output_buffer = @output_buffer;#{locals_code};#{code}
_old_virtual_path, @virtual_path = @virtual_path, #{@virtual_path.inspect};_old_output_buffer = @output_buffer;#{locals_code};#{code}
ensure
@output_buffer = _old_output_buffer
@virtual_path, @output_buffer = _old_virtual_path, _old_output_buffer
end
end_src

View File

@@ -147,9 +147,19 @@ module ActionView
module Locals
attr_accessor :locals
def _render_partial(options)
locals[options[:partial]] = options[:locals]
super(options)
def render(options = {}, local_assigns = {})
case options
when Hash
if block_given?
locals[options[:layout]] = options[:locals]
elsif options.key?(:partial)
locals[options[:partial]] = options[:locals]
end
else
locals[options] = local_assigns
end
super
end
end

View File

@@ -11,11 +11,9 @@ class TestERBTemplate < ActiveSupport::TestCase
end
class Context
attr_accessor :_template
def initialize
@output_buffer = "original"
@_virtual_path = nil
@virtual_path = nil
end
def hello
@@ -24,7 +22,7 @@ class TestERBTemplate < ActiveSupport::TestCase
def partial
ActionView::Template.new(
"<%= @_template.virtual_path %>",
"<%= @virtual_path %>",
"partial",
ERBHandler,
:virtual_path => "partial"
@@ -86,9 +84,9 @@ class TestERBTemplate < ActiveSupport::TestCase
end
def test_virtual_path
@template = new_template("<%= @_template.virtual_path %>" \
@template = new_template("<%= @virtual_path %>" \
"<%= partial.render(self, {}) %>" \
"<%= @_template.virtual_path %>")
"<%= @virtual_path %>")
assert_equal "hellopartialhello", render
end

View File

@@ -9,7 +9,7 @@ class UrlHelperTest < ActiveSupport::TestCase
# or request.
#
# In those cases, we'll set up a simple mock
attr_accessor :controller, :request, :_template
attr_accessor :controller, :request
routes = ActionDispatch::Routing::RouteSet.new
routes.draw do