From 2f683fd870d0e4c5aff38510ef03c7e5144a1ea4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sun, 1 May 2011 11:53:09 +0200 Subject: Remove more dependencies from the view. --- actionpack/lib/action_view/base.rb | 6 +----- actionpack/lib/action_view/renderer/partial_renderer.rb | 14 ++++++++++---- actionpack/lib/action_view/renderer/renderer.rb | 1 + actionpack/lib/action_view/rendering.rb | 17 ++++++----------- actionpack/test/template/log_subscriber_test.rb | 7 +++---- 5 files changed, 21 insertions(+), 24 deletions(-) diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb index 0866e808e6..37cd4d9ddc 100644 --- a/actionpack/lib/action_view/base.rb +++ b/actionpack/lib/action_view/base.rb @@ -202,17 +202,13 @@ module ActionView #:nodoc: lookup_context : ActionView::LookupContext.new(lookup_context) @_lookup_context.formats = formats if formats - @_renderer = ActionView::Renderer.new(@_lookup_context, self) + @view_renderer = ActionView::Renderer.new(@_lookup_context, self) end def controller_path @controller_path ||= controller && controller.controller_path end - def controller_prefixes - @controller_prefixes ||= controller && controller._prefixes - end - ActiveSupport.run_load_hooks(:action_view, self) end end diff --git a/actionpack/lib/action_view/renderer/partial_renderer.rb b/actionpack/lib/action_view/renderer/partial_renderer.rb index 83efc95f39..0eeead2e5d 100644 --- a/actionpack/lib/action_view/renderer/partial_renderer.rb +++ b/actionpack/lib/action_view/renderer/partial_renderer.rb @@ -217,9 +217,11 @@ module ActionView class PartialRenderer < AbstractRenderer #:nodoc: PARTIAL_NAMES = Hash.new {|h,k| h[k] = {} } + # TODO Controller should not come from the view def initialize(view, *) super - @partial_names = PARTIAL_NAMES[@view.controller.class.name] + @controller = @view.controller + @partial_names = PARTIAL_NAMES[@controller.class.name] end def setup(options, block) @@ -292,7 +294,7 @@ module ActionView locals[as] = object content = @template.render(view, locals) do |*name| - view._block_layout_for(*name, &block) + view._layout_for(*name, &block) end content = layout.render(view, locals){ content } if layout @@ -301,6 +303,10 @@ module ActionView private + def controller_prefixes + @controller_prefixes ||= @controller && @controller._prefixes + end + def collection if @options.key?(:collection) collection = @options[:collection] @@ -324,7 +330,7 @@ module ActionView end def find_template(path=@path, locals=@locals.keys) - prefixes = path.include?(?/) ? [] : @view.controller_prefixes + prefixes = path.include?(?/) ? [] : controller_prefixes @lookup_context.find_template(path, prefixes, true, locals) end @@ -365,7 +371,7 @@ module ActionView object = object.to_model if object.respond_to?(:to_model) object.class.model_name.partial_path.dup.tap do |partial| - path = @view.controller_prefixes.first + path = controller_prefixes.first partial.insert(0, "#{File.dirname(path)}/") if partial.include?(?/) && path.include?(?/) end end diff --git a/actionpack/lib/action_view/renderer/renderer.rb b/actionpack/lib/action_view/renderer/renderer.rb index f0ee103d80..7b95c2dd92 100644 --- a/actionpack/lib/action_view/renderer/renderer.rb +++ b/actionpack/lib/action_view/renderer/renderer.rb @@ -6,6 +6,7 @@ module ActionView attr_accessor :lookup_context # TODO: render_context should not be an initialization parameter + # TODO: controller should be received on initialization def initialize(lookup_context, render_context) @render_context = render_context @lookup_context = lookup_context diff --git a/actionpack/lib/action_view/rendering.rb b/actionpack/lib/action_view/rendering.rb index 017a27976c..9850965456 100644 --- a/actionpack/lib/action_view/rendering.rb +++ b/actionpack/lib/action_view/rendering.rb @@ -4,7 +4,7 @@ module ActionView # = Action View Rendering module Rendering # This is temporary until we remove the renderer dependency from AV. - delegate :render, :render_body, :to => :@_renderer + delegate :render, :render_body, :to => :@view_renderer # Returns the contents that are yielded to a layout, given a name or a block. # @@ -52,20 +52,15 @@ module ActionView # Hello David # # - def _layout_for(*args) - name = args.first - name = :layout unless name.is_a?(Symbol) - @_view_flow.get(name).html_safe - end - - # Handle layout for calls from partials that supports blocks. - def _block_layout_for(*args, &block) + def _layout_for(*args, &block) name = args.first - if !name.is_a?(Symbol) && block + if name.is_a?(Symbol) + @_view_flow.get(name).html_safe + elsif block capture(*args, &block) else - _layout_for(*args) + @_view_flow.get(:layout).html_safe end end end diff --git a/actionpack/test/template/log_subscriber_test.rb b/actionpack/test/template/log_subscriber_test.rb index 8b8b005a1d..50e1cccd3b 100644 --- a/actionpack/test/template/log_subscriber_test.rb +++ b/actionpack/test/template/log_subscriber_test.rb @@ -9,7 +9,9 @@ class AVLogSubscriberTest < ActiveSupport::TestCase def setup super @old_logger = ActionController::Base.logger - @view = ActionView::Base.new(ActionController::Base.view_paths, {}) + @controller = Object.new + @controller.stubs(:_prefixes).returns(%w(test)) + @view = ActionView::Base.new(ActionController::Base.view_paths, {}, @controller) Rails.stubs(:root).returns(File.expand_path(FIXTURE_LOAD_PATH)) ActionView::LogSubscriber.attach_to :action_view end @@ -57,7 +59,6 @@ class AVLogSubscriberTest < ActiveSupport::TestCase end def test_render_partial_with_implicit_path - @view.stubs(:controller_prefixes).returns(%w(test)) @view.render(Customer.new("david"), :greeting => "hi") wait @@ -74,7 +75,6 @@ class AVLogSubscriberTest < ActiveSupport::TestCase end def test_render_collection_with_implicit_path - @view.stubs(:controller_prefixes).returns(%w(test)) @view.render([ Customer.new("david"), Customer.new("mary") ], :greeting => "hi") wait @@ -83,7 +83,6 @@ class AVLogSubscriberTest < ActiveSupport::TestCase end def test_render_collection_template_without_path - @view.stubs(:controller_prefixes).returns(%w(test)) @view.render([ GoodCustomer.new("david"), Customer.new("mary") ], :greeting => "hi") wait -- cgit v1.2.3