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

Performance: PartialTemplate#initialize

This commit is contained in:
Jeremy Kemper 2008-06-16 23:59:22 -07:00
parent 8190bce8bc
commit d7b3c3395f

View file

@ -1,70 +1,70 @@
module ActionView #:nodoc: module ActionView #:nodoc:
class PartialTemplate < Template #:nodoc: class PartialTemplate < Template #:nodoc:
attr_reader :variable_name, :object attr_reader :variable_name, :object
def initialize(view, partial_path, object = nil, locals = {}) def initialize(view, partial_path, object = nil, locals = {})
@path, @variable_name = extract_partial_name_and_path(view, partial_path) @view_controller = view.controller if view.respond_to?(:controller)
set_path_and_variable_name!(partial_path)
super(view, @path, true, locals) super(view, @path, true, locals)
add_object_to_local_assigns!(object) add_object_to_local_assigns!(object)
# This is needed here in order to compile template with knowledge of 'counter' # This is needed here in order to compile template with knowledge of 'counter'
initialize_counter initialize_counter!
# Prepare early. This is a performance optimization for partial collections # Prepare early. This is a performance optimization for partial collections
prepare! prepare!
end end
def render def render
ActionController::Base.benchmark("Rendered #{@path}", Logger::DEBUG, false) do ActionController::Base.benchmark("Rendered #{@path}", Logger::DEBUG, false) do
@handler.render(self) @handler.render(self)
end end
end end
def render_member(object) def render_member(object)
@locals[:object] = @locals[@variable_name] = object @locals[:object] = @locals[@variable_name] = object
template = render_template template = render_template
@locals[@counter_name] += 1 @locals[@counter_name] += 1
@locals.delete(@variable_name) @locals.delete(@variable_name)
@locals.delete(:object) @locals.delete(:object)
template template
end end
def counter=(num) def counter=(num)
@locals[@counter_name] = num @locals[@counter_name] = num
end end
private private
def add_object_to_local_assigns!(object)
def add_object_to_local_assigns!(object) @locals[:object] ||=
@locals[:object] ||= @locals[@variable_name] ||=
@locals[@variable_name] ||= if object.is_a?(ActionView::Base::ObjectWrapper)
if object.is_a?(ActionView::Base::ObjectWrapper) object.value
object.value else
else object
object end || @view_controller.instance_variable_get("@#{variable_name}")
end || @view.controller.instance_variable_get("@#{variable_name}") end
end
def set_path_and_variable_name!(partial_path)
def extract_partial_name_and_path(view, partial_path) if partial_path.include?('/')
path, partial_name = partial_pieces(view, partial_path) @variable_name = File.basename(partial_path)
[File.join(path, "_#{partial_name}"), partial_name.split('/').last.split('.').first.to_sym] @path = "#{File.dirname(partial_path)}/_#{@variable_name}"
end elsif @view_controller
@variable_name = partial_path
def partial_pieces(view, partial_path) @path = "#{@view_controller.class.controller_path}/_#{@variable_name}"
if partial_path.include?('/') else
return File.dirname(partial_path), File.basename(partial_path) @variable_name = partial_path
else @path = "_#{@variable_name}"
return view.controller.class.controller_path, partial_path end
@variable_name = @variable_name.sub(/\..*$/, '').to_sym
end
def initialize_counter!
@counter_name ||= "#{@variable_name}_counter".to_sym
@locals[@counter_name] = 0
end end
end
def initialize_counter
@counter_name ||= "#{@variable_name}_counter".to_sym
@locals[@counter_name] = 0
end
end end
end end