aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/render/partials.rb
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2010-10-10 11:03:18 +0200
committerJosé Valim <jose.valim@gmail.com>2010-10-10 12:43:26 +0200
commitb88f4ca93bcaef9a6bfd21d95acc8f432a3c8e5c (patch)
tree94ca53a9d78f749b18ab6b325afc54a1c991f1ca /actionpack/lib/action_view/render/partials.rb
parentab2f2b22a6d0656f719c294d40e35d21c752ba8c (diff)
downloadrails-b88f4ca93bcaef9a6bfd21d95acc8f432a3c8e5c.tar.gz
rails-b88f4ca93bcaef9a6bfd21d95acc8f432a3c8e5c.tar.bz2
rails-b88f4ca93bcaef9a6bfd21d95acc8f432a3c8e5c.zip
Clean up the house before moving in the new furniture.
This commit moves all the template rendering logic that was hanging around AV::Base to renderer objects.
Diffstat (limited to 'actionpack/lib/action_view/render/partials.rb')
-rw-r--r--actionpack/lib/action_view/render/partials.rb177
1 files changed, 4 insertions, 173 deletions
diff --git a/actionpack/lib/action_view/render/partials.rb b/actionpack/lib/action_view/render/partials.rb
index 4e03d43358..844c3e9572 100644
--- a/actionpack/lib/action_view/render/partials.rb
+++ b/actionpack/lib/action_view/render/partials.rb
@@ -210,181 +210,12 @@ module ActionView
# <%- end -%>
# <% end %>
module Partials
- extend ActiveSupport::Concern
-
- class PartialRenderer
- PARTIAL_NAMES = Hash.new {|h,k| h[k] = {} }
-
- def initialize(view_context, options, block)
- @view = view_context
- @partial_names = PARTIAL_NAMES[@view.controller.class.name]
- setup(options, block)
- end
-
- def setup(options, block)
- partial = options[:partial]
-
- @options = options
- @locals = options[:locals] || {}
- @block = block
-
- if String === partial
- @object = options[:object]
- @path = partial
- @collection = collection
- else
- @object = partial
-
- if @collection = collection_from_object || collection
- paths = @collection_data = @collection.map { |o| partial_path(o) }
- @path = paths.uniq.size == 1 ? paths.first : nil
- else
- @path = partial_path
- end
- end
-
- if @path
- @variable, @variable_counter = retrieve_variable(@path)
- else
- paths.map! { |path| retrieve_variable(path).unshift(path) }
- end
- end
-
- def retrieve_variable(path)
- variable = @options[:as] || path[%r'_?(\w+)(\.\w+)*$', 1].to_sym
- variable_counter = :"#{variable}_counter" if @collection
- [variable, variable_counter]
- end
-
- def render
- identifier = ((@template = find_partial) ? @template.identifier : @path)
-
- if @collection
- ActiveSupport::Notifications.instrument("render_collection.action_view",
- :identifier => identifier || "collection", :count => @collection.size) do
- render_collection
- end
- else
- if !@block && (layout = @options[:layout])
- layout = find_template(layout)
- end
-
- content = ActiveSupport::Notifications.instrument("render_partial.action_view",
- :identifier => identifier) do
- render_partial
- end
-
- content = @view._render_layout(layout, @locals){ content } if layout
- content
- end
- end
-
- def render_collection
- return nil if @collection.blank?
-
- if @options.key?(:spacer_template)
- spacer = find_template(@options[:spacer_template]).render(@view, @locals)
- end
-
- result = @template ? collection_with_template : collection_without_template
- result.join(spacer).html_safe
- end
-
- def collection_with_template
- segments, locals, template = [], @locals, @template
- as, counter = @variable, @variable_counter
-
- locals[counter] = -1
-
- @collection.each do |object|
- locals[counter] += 1
- locals[as] = object
- segments << template.render(@view, locals)
- end
-
- segments
- end
-
- def collection_without_template
- segments, locals, collection_data = [], @locals, @collection_data
- index, template, cache = -1, nil, {}
- keys = @locals.keys
-
- @collection.each_with_index do |object, i|
- path, *data = collection_data[i]
- template = (cache[path] ||= find_template(path, keys + data))
- locals[data[0]] = object
- locals[data[1]] = (index += 1)
- segments << template.render(@view, locals)
- end
-
- @template = template
- segments
- end
-
- def render_partial
- locals, view = @locals, @view
- object, as = @object, @variable
-
- object ||= locals[as]
- locals[as] = object
-
- @template.render(view, locals) do |*name|
- view._layout_for(*name, &@block)
- end
- end
-
- private
-
- def collection
- if @options.key?(:collection)
- @options[:collection] || []
- end
- end
-
- def collection_from_object
- if @object.respond_to?(:to_ary)
- @object
- end
- end
-
- def find_partial
- if path = @path
- locals = @locals.keys
- locals << @variable
- locals << @variable_counter if @collection
- find_template(path, locals)
- end
- end
-
- def find_template(path=@path, locals=@locals.keys)
- prefix = @view.controller_path unless path.include?(?/)
- @view.find_template(path, prefix, true, locals)
- end
-
- def partial_path(object = @object)
- @partial_names[object.class.name] ||= begin
- object = object.to_model if object.respond_to?(:to_model)
-
- object.class.model_name.partial_path.dup.tap do |partial|
- path = @view.controller_path
- partial.insert(0, "#{File.dirname(path)}/") if partial.include?(?/) && path.include?(?/)
- end
- end
- end
- end
-
def _render_partial(options, &block) #:nodoc:
- _wrap_formats(options[:partial]) do
- if defined?(@renderer)
- @renderer.setup(options, block)
- else
- @renderer = PartialRenderer.new(self, options, block)
- end
-
- @renderer.render
- end
+ _partial_renderer.setup(options, block).render
end
+ def _partial_renderer #:nodoc:
+ @_partial_renderer ||= PartialRenderer.new(self)
+ end
end
end