2008-03-02 23:01:35 -05:00
|
|
|
module ActionView #:nodoc:
|
|
|
|
class PartialTemplate < Template #:nodoc:
|
2008-07-02 11:38:50 -04:00
|
|
|
attr_reader :variable_name, :object, :as
|
2008-06-17 02:59:22 -04:00
|
|
|
|
2008-07-02 11:38:50 -04:00
|
|
|
def initialize(view, partial_path, object = nil, locals = {}, as = nil)
|
2008-06-17 02:59:22 -04:00
|
|
|
@view_controller = view.controller if view.respond_to?(:controller)
|
2008-07-02 11:38:50 -04:00
|
|
|
@as = as
|
2008-06-17 02:59:22 -04:00
|
|
|
set_path_and_variable_name!(partial_path)
|
2008-07-02 22:38:58 -04:00
|
|
|
super(view, @path, nil, locals)
|
2008-03-02 23:01:35 -05:00
|
|
|
add_object_to_local_assigns!(object)
|
2008-03-31 20:50:09 -04:00
|
|
|
|
2008-03-02 23:01:35 -05:00
|
|
|
# This is needed here in order to compile template with knowledge of 'counter'
|
2008-06-17 02:59:22 -04:00
|
|
|
initialize_counter!
|
|
|
|
|
2008-03-02 23:01:35 -05:00
|
|
|
# Prepare early. This is a performance optimization for partial collections
|
|
|
|
prepare!
|
|
|
|
end
|
2008-06-17 02:59:22 -04:00
|
|
|
|
2008-03-02 23:01:35 -05:00
|
|
|
def render
|
2008-06-15 22:22:27 -04:00
|
|
|
ActionController::Base.benchmark("Rendered #{@path.path_without_format_and_extension}", Logger::DEBUG, false) do
|
2008-07-03 14:06:00 -04:00
|
|
|
super
|
2008-03-02 23:01:35 -05:00
|
|
|
end
|
|
|
|
end
|
2008-06-17 02:59:22 -04:00
|
|
|
|
2008-03-02 23:01:35 -05:00
|
|
|
def render_member(object)
|
2008-07-02 12:19:41 -04:00
|
|
|
@locals[:object] = @locals[@variable_name] = object
|
|
|
|
@locals[as] = object if as
|
2008-06-17 02:59:22 -04:00
|
|
|
|
2008-05-02 05:45:08 -04:00
|
|
|
template = render_template
|
2008-06-05 15:48:42 -04:00
|
|
|
@locals[@counter_name] += 1
|
2008-07-02 11:38:50 -04:00
|
|
|
@locals.delete(as)
|
2008-05-02 05:45:08 -04:00
|
|
|
@locals.delete(@variable_name)
|
|
|
|
@locals.delete(:object)
|
2008-06-17 02:59:22 -04:00
|
|
|
|
2008-05-02 05:45:08 -04:00
|
|
|
template
|
2008-03-02 23:01:35 -05:00
|
|
|
end
|
2008-06-17 02:59:22 -04:00
|
|
|
|
2008-03-31 20:50:09 -04:00
|
|
|
def counter=(num)
|
|
|
|
@locals[@counter_name] = num
|
|
|
|
end
|
|
|
|
|
2008-03-02 23:01:35 -05:00
|
|
|
private
|
2008-06-17 02:59:22 -04:00
|
|
|
def add_object_to_local_assigns!(object)
|
|
|
|
@locals[:object] ||=
|
2008-06-30 14:39:22 -04:00
|
|
|
@locals[@variable_name] ||= object || @view_controller.instance_variable_get("@#{variable_name}")
|
2008-07-02 11:38:50 -04:00
|
|
|
@locals[as] ||= @locals[:object] if as
|
2008-06-17 02:59:22 -04:00
|
|
|
end
|
2008-03-31 20:50:09 -04:00
|
|
|
|
2008-06-17 02:59:22 -04:00
|
|
|
def set_path_and_variable_name!(partial_path)
|
|
|
|
if partial_path.include?('/')
|
|
|
|
@variable_name = File.basename(partial_path)
|
|
|
|
@path = "#{File.dirname(partial_path)}/_#{@variable_name}"
|
|
|
|
elsif @view_controller
|
|
|
|
@variable_name = partial_path
|
|
|
|
@path = "#{@view_controller.class.controller_path}/_#{@variable_name}"
|
|
|
|
else
|
|
|
|
@variable_name = partial_path
|
|
|
|
@path = "_#{@variable_name}"
|
|
|
|
end
|
|
|
|
|
|
|
|
@variable_name = @variable_name.sub(/\..*$/, '').to_sym
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize_counter!
|
|
|
|
@counter_name ||= "#{@variable_name}_counter".to_sym
|
|
|
|
@locals[@counter_name] = 0
|
2008-03-02 23:01:35 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|