From 9f5cd0156ab907d8097fc9c588823a9b09038b93 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Tue, 11 Aug 2009 23:43:15 -0700 Subject: More cleanup of ActionView and reduction in need for blocks in some cases: * only one of partial_name or :as will be available as a local * `object` is removed * Simplify _layout_for in most cases. * Remove <% render :partial do |args| %> * <% render :partial do %> still works fine --- actionpack/lib/action_view/render/partials.rb | 13 +++++-------- actionpack/lib/action_view/render/rendering.rb | 22 ++++++---------------- actionpack/lib/action_view/template/handler.rb | 4 ---- actionpack/lib/action_view/template/template.rb | 1 - actionpack/test/controller/render_test.rb | 9 --------- .../test/fixtures/test/_customer_with_var.erb | 2 +- actionpack/test/fixtures/test/_hash_object.erb | 2 +- .../using_layout_around_block_with_args.html.erb | 1 - actionpack/test/template/render_test.rb | 2 +- 9 files changed, 14 insertions(+), 42 deletions(-) delete mode 100644 actionpack/test/fixtures/test/using_layout_around_block_with_args.html.erb diff --git a/actionpack/lib/action_view/render/partials.rb b/actionpack/lib/action_view/render/partials.rb index e287021eb1..188f0d0214 100644 --- a/actionpack/lib/action_view/render/partials.rb +++ b/actionpack/lib/action_view/render/partials.rb @@ -223,16 +223,14 @@ module ActionView def collection_with_template(template) options = @options - segments, locals, as = [], @locals, options[:as] || :object + segments, locals, as = [], @locals, options[:as] || template.variable_name - variable_name = template.variable_name counter_name = template.counter_name locals[counter_name] = -1 collection.each do |object| locals[counter_name] += 1 - locals[variable_name] = object - locals[as] = object if as + locals[as] = object segments << template.render(@view, locals) end @@ -242,14 +240,13 @@ module ActionView def collection_without_template options = @options - segments, locals, as = [], @locals, options[:as] || :object + segments, locals, as = [], @locals, options[:as] index, template = -1, nil collection.each do |object| template = find_template(partial_path(object)) locals[template.counter_name] = (index += 1) locals[template.variable_name] = object - locals[as] = object if as segments << template.render(@view, locals) end @@ -265,8 +262,8 @@ module ActionView @locals[:object] = @locals[template.variable_name] = object @locals[@options[:as]] = object if @options[:as] - content = template.render(@view, @locals) do |*names| - @view._layout_for(names, &@block) + content = template.render(@view, @locals) do |*name| + @view._layout_for(*name, &@block) end return content if @block || !@options[:layout] find_template(@options[:layout]).render(@view, @locals) { content } diff --git a/actionpack/lib/action_view/render/rendering.rb b/actionpack/lib/action_view/render/rendering.rb index e505fe6c8c..e04800e6e8 100644 --- a/actionpack/lib/action_view/render/rendering.rb +++ b/actionpack/lib/action_view/render/rendering.rb @@ -70,21 +70,11 @@ module ActionView # In this case, the layout would receive the block passed into render :layout, # and the Struct specified in the layout would be passed into the block. The result # would be Hello David. - def _layout_for(names, &block) + def _layout_for(name = nil) + return @_content_for[name || :layout] if !block_given? || name + with_output_buffer do - # This is due to the potentially ambiguous use of yield when - # a block is passed in to a template *and* there is a content_for() - # of the same name. Suggested solution: require explicit use of content_for - # in these ambiguous cases. - # - # We would be able to continue supporting yield in all non-ambiguous - # cases. Question: should we deprecate yield in favor of content_for - # and reserve yield for cases where there is a yield into a real block? - if @_content_for.key?(names.first) || !block_given? - return @_content_for[names.first || :layout] - else - return yield(names) - end + return yield end end @@ -93,7 +83,7 @@ module ActionView template = Template.new(options[:inline], "inline #{options[:inline].inspect}", handler, {}) locals = options[:locals] || {} content = template.render(self, locals) - content = layout.render(self, locals) { content } if layout + content = layout.render(self, locals) {|*name| _layout_for(*name) } if layout content end @@ -134,7 +124,7 @@ module ActionView if layout @_layout = layout.identifier logger.info("Rendering template within #{layout.identifier}") if logger - content = layout.render(self, locals) + content = layout.render(self, locals) {|*name| _layout_for(*name) } end content end diff --git a/actionpack/lib/action_view/template/handler.rb b/actionpack/lib/action_view/template/handler.rb index 7311e5e12f..4bf58b9fa8 100644 --- a/actionpack/lib/action_view/template/handler.rb +++ b/actionpack/lib/action_view/template/handler.rb @@ -29,10 +29,6 @@ module ActionView raise "Need to implement #{self.class.name}#call(template)" end - def initialize(view = nil) - @view = view - end - def render(template, local_assigns) raise "Need to implement #{self.class.name}#render(template, local_assigns)" end diff --git a/actionpack/lib/action_view/template/template.rb b/actionpack/lib/action_view/template/template.rb index f6270e5616..8f23f9b9e3 100644 --- a/actionpack/lib/action_view/template/template.rb +++ b/actionpack/lib/action_view/template/template.rb @@ -28,7 +28,6 @@ module ActionView def render(view, locals, &block) method_name = compile(locals, view) - block ||= proc {|*names| view._layout_for(names) } view.send(method_name, locals, &block) rescue Exception => e if e.is_a?(TemplateError) diff --git a/actionpack/test/controller/render_test.rb b/actionpack/test/controller/render_test.rb index 0c0599679c..9c5b560c2c 100644 --- a/actionpack/test/controller/render_test.rb +++ b/actionpack/test/controller/render_test.rb @@ -486,10 +486,6 @@ class TestController < ActionController::Base render :action => "using_layout_around_block" end - def render_using_layout_around_block_with_args - render :action => "using_layout_around_block_with_args" - end - def render_using_layout_around_block_in_main_layout_and_within_content_for_layout render :action => "using_layout_around_block", :layout => "layouts/block_with_layout" end @@ -1161,11 +1157,6 @@ class RenderTest < ActionController::TestCase assert_equal "Before (Anthony)\nInside from first block in layout\nAfter\nBefore (David)\nInside from block\nAfter\nBefore (Ramm)\nInside from second block in layout\nAfter\n", @response.body end - def test_using_layout_around_block_with_args - get :render_using_layout_around_block_with_args - assert_equal "Before\narg1arg2\nAfter", @response.body - end - def test_partial_only get :partial_only assert_equal "only partial", @response.body diff --git a/actionpack/test/fixtures/test/_customer_with_var.erb b/actionpack/test/fixtures/test/_customer_with_var.erb index c28824936b..00047dd20e 100644 --- a/actionpack/test/fixtures/test/_customer_with_var.erb +++ b/actionpack/test/fixtures/test/_customer_with_var.erb @@ -1 +1 @@ -<%= customer.name %> <%= customer.name %> <%= customer_with_var.name %> \ No newline at end of file +<%= customer.name %> <%= customer.name %> <%= customer.name %> \ No newline at end of file diff --git a/actionpack/test/fixtures/test/_hash_object.erb b/actionpack/test/fixtures/test/_hash_object.erb index 55c03afb27..34a92c6a56 100644 --- a/actionpack/test/fixtures/test/_hash_object.erb +++ b/actionpack/test/fixtures/test/_hash_object.erb @@ -1,2 +1,2 @@ <%= hash_object[:first_name] %> -<%= object[:first_name].reverse %> +<%= hash_object[:first_name].reverse %> diff --git a/actionpack/test/fixtures/test/using_layout_around_block_with_args.html.erb b/actionpack/test/fixtures/test/using_layout_around_block_with_args.html.erb deleted file mode 100644 index 71b1f30ad0..0000000000 --- a/actionpack/test/fixtures/test/using_layout_around_block_with_args.html.erb +++ /dev/null @@ -1 +0,0 @@ -<% render(:layout => "layout_for_block_with_args") do |*args| %><%= args.join %><% end %> \ No newline at end of file diff --git a/actionpack/test/template/render_test.rb b/actionpack/test/template/render_test.rb index 50074b3b9d..ff65404c79 100644 --- a/actionpack/test/template/render_test.rb +++ b/actionpack/test/template/render_test.rb @@ -142,7 +142,7 @@ module RenderTestCases end def test_render_partial_collection_without_as - assert_equal "local_inspector,local_inspector_counter,object", + assert_equal "local_inspector,local_inspector_counter", @view.render(:partial => "test/local_inspector", :collection => [ Customer.new("mary") ]) end -- cgit v1.2.3