Skip to content

Commit

Permalink
Clean up unused methods from AV::Base and pass in the template object…
Browse files Browse the repository at this point in the history
… on rendering.
  • Loading branch information
josevalim committed Oct 10, 2010
1 parent 11aa515 commit 49b6f33
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 31 deletions.
29 changes: 13 additions & 16 deletions actionpack/lib/action_view/base.rb
Expand Up @@ -155,9 +155,6 @@ module ActionView #:nodoc:
# #
# See the ActionView::Helpers::PrototypeHelper::JavaScriptGenerator::GeneratorMethods documentation for more details. # See the ActionView::Helpers::PrototypeHelper::JavaScriptGenerator::GeneratorMethods documentation for more details.
class Base class Base
module Subclasses
end

include Helpers, Rendering, Partials, ::ERB::Util, Context include Helpers, Rendering, Partials, ::ERB::Util, Context


# Specify whether RJS responses should be wrapped in a try/catch block # Specify whether RJS responses should be wrapped in a try/catch block
Expand All @@ -177,12 +174,12 @@ class << self
delegate :logger, :to => 'ActionController::Base', :allow_nil => true delegate :logger, :to => 'ActionController::Base', :allow_nil => true
end end


attr_accessor :base_path, :assigns, :template_extension, :lookup_context attr_accessor :_template
attr_internal :captures, :request, :controller, :template, :config attr_internal :request, :controller, :config, :assigns, :lookup_context


delegate :formats, :formats=, :locale, :locale=, :view_paths, :view_paths=, :to => :lookup_context delegate :formats, :formats=, :locale, :locale=, :view_paths, :view_paths=, :to => :lookup_context


delegate :request_forgery_protection_token, :template, :params, :session, :cookies, :response, :headers, delegate :request_forgery_protection_token, :params, :session, :cookies, :response, :headers,
:flash, :action_name, :controller_name, :to => :controller :flash, :action_name, :controller_name, :to => :controller


delegate :logger, :to => :controller, :allow_nil => true delegate :logger, :to => :controller, :allow_nil => true
Expand All @@ -197,26 +194,26 @@ def self.process_view_paths(value)
end end


def assign(new_assigns) # :nodoc: def assign(new_assigns) # :nodoc:
self.assigns = new_assigns.each { |key, value| instance_variable_set("@#{key}", value) } @_assigns = new_assigns.each { |key, value| instance_variable_set("@#{key}", value) }
end end


def initialize(lookup_context = nil, assigns_for_first_render = {}, controller = nil, formats = nil) #:nodoc: def initialize(lookup_context = nil, assigns_for_first_render = {}, controller = nil, formats = nil) #:nodoc:
assign(assigns_for_first_render) assign(assigns_for_first_render)
self.helpers = self.class.helpers || Module.new self.helpers = Module.new unless self.class.helpers

if @_controller = controller
@_request = controller.request if controller.respond_to?(:request)
end

@_config = controller && controller.respond_to?(:config) ? controller.config.inheritable_copy : {}


@_config = {}
@_content_for = Hash.new { |h,k| h[k] = ActiveSupport::SafeBuffer.new } @_content_for = Hash.new { |h,k| h[k] = ActiveSupport::SafeBuffer.new }
@_virtual_path = nil @_virtual_path = nil
@output_buffer = nil @output_buffer = nil


@lookup_context = lookup_context.is_a?(ActionView::LookupContext) ? if @_controller = controller
@_request = controller.request if controller.respond_to?(:request)
@_config = controller.config.inheritable_copy if controller.respond_to?(:config)
end

@_lookup_context = lookup_context.is_a?(ActionView::LookupContext) ?
lookup_context : ActionView::LookupContext.new(lookup_context) lookup_context : ActionView::LookupContext.new(lookup_context)
@lookup_context.formats = formats if formats @_lookup_context.formats = formats if formats
end end


def store_content_for(key, value) def store_content_for(key, value)
Expand Down
4 changes: 2 additions & 2 deletions actionpack/lib/action_view/helpers/translation_helper.rb
Expand Up @@ -45,8 +45,8 @@ def localize(*args)
private private
def scope_key_by_partial(key) def scope_key_by_partial(key)
if key.to_s.first == "." if key.to_s.first == "."
if @_virtual_path if (path = @_template && @_template.virtual_path)
@_virtual_path.gsub(%r{/_?}, ".") + key.to_s path.gsub(%r{/_?}, ".") + key.to_s
else else
raise "Cannot use t(#{key.inspect}) shortcut because path is not available" raise "Cannot use t(#{key.inspect}) shortcut because path is not available"
end end
Expand Down
7 changes: 5 additions & 2 deletions actionpack/lib/action_view/template.rb
Expand Up @@ -133,12 +133,15 @@ def initialize(source, identifier, handler, details)
# we use a bang in this instrumentation because you don't want to # 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. # consume this in production. This is only slow if it's being listened to.
def render(view, locals, &block) def render(view, locals, &block)
old_template, view._template = view._template, self
ActiveSupport::Notifications.instrument("!render_template.action_view", :virtual_path => @virtual_path) do ActiveSupport::Notifications.instrument("!render_template.action_view", :virtual_path => @virtual_path) do
compile!(view) compile!(view)
view.send(method_name, locals, &block) view.send(method_name, locals, &block)
end end
rescue Exception => e rescue Exception => e
handle_render_error(view, e) handle_render_error(view, e)
ensure
view._template = old_template
end end


def mime_type def mime_type
Expand Down Expand Up @@ -272,9 +275,9 @@ def compile(view, mod) #:nodoc:
# encoding of the code # encoding of the code
source = <<-end_src source = <<-end_src
def #{method_name}(local_assigns) def #{method_name}(local_assigns)
_old_virtual_path, @_virtual_path = @_virtual_path, #{@virtual_path.inspect};_old_output_buffer = @output_buffer;#{locals_code};#{code} _old_output_buffer = @output_buffer;#{locals_code};#{code}
ensure ensure
@_virtual_path, @output_buffer = _old_virtual_path, _old_output_buffer @output_buffer = _old_output_buffer
end end
end_src end_src


Expand Down
18 changes: 10 additions & 8 deletions actionpack/test/template/template_test.rb
Expand Up @@ -4,12 +4,14 @@
class TestERBTemplate < ActiveSupport::TestCase class TestERBTemplate < ActiveSupport::TestCase
ERBHandler = ActionView::Template::Handlers::ERB.new ERBHandler = ActionView::Template::Handlers::ERB.new


class Context class LookupContext
class LookupContext def disable_cache
def disable_cache yield
yield
end
end end
end

class Context
attr_accessor :_template


def initialize def initialize
@output_buffer = "original" @output_buffer = "original"
Expand All @@ -22,7 +24,7 @@ def hello


def partial def partial
ActionView::Template.new( ActionView::Template.new(
"<%= @_virtual_path %>", "<%= @_template.virtual_path %>",
"partial", "partial",
ERBHandler, ERBHandler,
:virtual_path => "partial" :virtual_path => "partial"
Expand Down Expand Up @@ -84,9 +86,9 @@ def test_restores_buffer
end end


def test_virtual_path def test_virtual_path
@template = new_template("<%= @_virtual_path %>" \ @template = new_template("<%= @_template.virtual_path %>" \
"<%= partial.render(self, {}) %>" \ "<%= partial.render(self, {}) %>" \
"<%= @_virtual_path %>") "<%= @_template.virtual_path %>")
assert_equal "hellopartialhello", render assert_equal "hellopartialhello", render
end end


Expand Down
4 changes: 2 additions & 2 deletions actionpack/test/template/translation_helper_test.rb
Expand Up @@ -31,13 +31,13 @@ def test_delegates_localize_to_i18n


def test_scoping_by_partial def test_scoping_by_partial
I18n.expects(:translate).with("test.translation.helper", :raise => true).returns("helper") I18n.expects(:translate).with("test.translation.helper", :raise => true).returns("helper")
@view = ActionView::Base.new(ActionController::Base.view_paths, {}) @view = ::ActionView::Base.new(ActionController::Base.view_paths, {})
assert_equal "helper", @view.render(:file => "test/translation") assert_equal "helper", @view.render(:file => "test/translation")
end end


def test_scoping_by_partial_of_an_array def test_scoping_by_partial_of_an_array
I18n.expects(:translate).with("test.scoped_translation.foo.bar", :raise => true).returns(["foo", "bar"]) I18n.expects(:translate).with("test.scoped_translation.foo.bar", :raise => true).returns(["foo", "bar"])
@view = ActionView::Base.new(ActionController::Base.view_paths, {}) @view = ::ActionView::Base.new(ActionController::Base.view_paths, {})
assert_equal "foobar", @view.render(:file => "test/scoped_translation") assert_equal "foobar", @view.render(:file => "test/scoped_translation")
end end


Expand Down
2 changes: 1 addition & 1 deletion actionpack/test/template/url_helper_test.rb
Expand Up @@ -9,7 +9,7 @@ class UrlHelperTest < ActiveSupport::TestCase
# or request. # or request.
# #
# In those cases, we'll set up a simple mock # In those cases, we'll set up a simple mock
attr_accessor :controller, :request attr_accessor :controller, :request, :_template


routes = ActionDispatch::Routing::RouteSet.new routes = ActionDispatch::Routing::RouteSet.new
routes.draw do routes.draw do
Expand Down

0 comments on commit 49b6f33

Please sign in to comment.