1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Continue reworking the partial path.

* TODO: Review ActionController calling render_template for certain partials.
    Might we be able to save logic by always delegating to AV's render_partial?
This commit is contained in:
Yehuda Katz 2009-08-07 06:32:17 -03:00
parent 493d84ce2f
commit d94ba11295
2 changed files with 22 additions and 47 deletions

View file

@ -185,10 +185,10 @@ module ActionView
def render_partial(options) def render_partial(options)
@assigns_added = false @assigns_added = false
_render_partial_unknown_type(options) _render_partial(options)
end end
def _render_partial_unknown_type(options, &block) #:nodoc: def _render_partial(options, &block) #:nodoc:
options[:locals] ||= {} options[:locals] ||= {}
path = partial = options[:partial] path = partial = options[:partial]
@ -200,21 +200,10 @@ module ActionView
path = _partial_path(object) path = _partial_path(object)
end end
_render_partial_path(path, options, &block) _render_partial_object(_pick_partial_template(path), options, &block)
end end
private private
def _render_partial_path(path, options, &block)
return '' if path.nil?
parts = [path, {:formats => formats}]
parts.push path.include?(?/) ? nil : controller_path
parts.push true
template = find_by_parts(*parts)
_render_partial_object(template, options, &block)
end
def _partial_path(object) def _partial_path(object)
self.class._partial_names[[controller.class, object.class]] ||= begin self.class._partial_names[[controller.class, object.class]] ||= begin
name = object.class.model_name name = object.class.model_name
@ -226,62 +215,45 @@ module ActionView
end end
end end
def _render_partial(layout, options, &block) def _render_partial_template(template, locals, object, options = {}, &block)
if block_given? options[:_template] = template
concat(_render_partial_unknown_type(options.merge(:partial => layout), &block)) locals[:object] = locals[template.variable_name] = object
else locals[options[:as]] = object if options[:as]
if layout
prefix = layout.include?(?/) ? nil : controller_path _render_single_template(template, locals, &block)
layout = find_by_parts(layout, {:formats => formats}, prefix, true)
end
_render_content(_render_partial_unknown_type(options), layout, options[:locals])
end
end end
def _render_partial_object(template, options, &block) def _render_partial_object(template, options, &block)
object = options[:object]
if options.key?(:collection) if options.key?(:collection)
_render_partial_collection(options.delete(:collection), options, template, &block) _render_partial_collection(options.delete(:collection), options, template, &block)
else else
locals = (options[:locals] ||= {}) locals = (options[:locals] ||= {})
object ||= locals[:object] || locals[template.variable_name] object = options[:object] || locals[:object] || locals[template.variable_name]
_set_locals(object, locals, template, options) _render_partial_template(template, locals, object, options, &block)
options[:_template] = template
_render_single_template(template, locals, &block)
end end
end end
def _set_locals(object, locals, template, options)
locals[:object] = locals[template.variable_name] = object
locals[options[:as]] = object if options[:as]
end
def _render_partial_collection(collection, options = {}, template = nil, &block) #:nodoc: def _render_partial_collection(collection, options = {}, template = nil, &block) #:nodoc:
return nil if collection.blank? return nil if collection.blank?
spacer = _render_partial_path(options[:spacer_template], {}) if options.key?(:spacer_template)
spacer = _render_partial(:partial => options[:spacer_template])
end
locals, index = options[:locals] || {}, 0 locals, index = options[:locals] || {}, 0
collection.map do |object| collection.map do |object|
tmp = template || _pick_partial_template(_partial_path(object)) tmp = template || _pick_partial_template(_partial_path(object))
options[:_template] = tmp
_set_locals(object, locals, tmp, options)
locals[tmp.counter_name] = index locals[tmp.counter_name] = index
index += 1 index += 1
_render_single_template(tmp, locals, &block) _render_partial_template(tmp, locals, object, options, &block)
end.join(spacer) end.join(spacer)
end end
def _pick_partial_template(partial_path) #:nodoc: def _pick_partial_template(partial_path) #:nodoc:
prefix = controller_path unless partial_path.include?('/') prefix = controller_path unless partial_path.include?(?/)
find_by_parts(partial_path, {:formats => formats}, prefix, true) find_by_parts(partial_path, {:formats => formats}, prefix, true)
end end
end end

View file

@ -13,12 +13,15 @@ module ActionView
def render(options = {}, locals = {}, &block) #:nodoc: def render(options = {}, locals = {}, &block) #:nodoc:
case options case options
when String, NilClass when String, NilClass
_render_partial_unknown_type(:partial => options, :locals => locals || {}) _render_partial(:partial => options, :locals => locals || {})
when Hash when Hash
layout = options[:layout] layout = options[:layout]
if options.key?(:partial) || block_given? if block_given?
return _render_partial(layout, options, &block) return concat(_render_partial(options.merge(:partial => layout), &block))
elsif options.key?(:partial)
layout = _pick_partial_template(layout) if layout
return _render_content(_render_partial(options), layout, options[:locals])
end end
layout = find_by_parts(layout, {:formats => formats}) if layout layout = find_by_parts(layout, {:formats => formats}) if layout