diff options
author | Michael Koziarski <michael@koziarski.com> | 2008-03-03 04:01:35 +0000 |
---|---|---|
committer | Michael Koziarski <michael@koziarski.com> | 2008-03-03 04:01:35 +0000 |
commit | 51b6619d4e78d9948952dab4709274ab57f41206 (patch) | |
tree | bab7a4d988c9349f4d8241e38e09f30d62b0049b /actionpack/lib/action_view/partial_template.rb | |
parent | 10d9d0b6fe023df1be0d87ca95bb739bb7eb30ba (diff) | |
download | rails-51b6619d4e78d9948952dab4709274ab57f41206.tar.gz rails-51b6619d4e78d9948952dab4709274ab57f41206.tar.bz2 rails-51b6619d4e78d9948952dab4709274ab57f41206.zip |
Refactor partial rendering into a PartialTemplate class. [Pratik]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8976 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/lib/action_view/partial_template.rb')
-rw-r--r-- | actionpack/lib/action_view/partial_template.rb | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/actionpack/lib/action_view/partial_template.rb b/actionpack/lib/action_view/partial_template.rb new file mode 100644 index 0000000000..a470c915cd --- /dev/null +++ b/actionpack/lib/action_view/partial_template.rb @@ -0,0 +1,61 @@ +module ActionView #:nodoc: + class PartialTemplate < Template #:nodoc: + + attr_reader :variable_name, :object + + def initialize(view, partial_path, object = nil, locals = {}) + @path, @variable_name = extract_partial_name_and_path(view, partial_path) + super(view, @path, true, locals) + add_object_to_local_assigns!(object) + + # This is needed here in order to compile template with knowledge of 'counter' + initialize_counter + + # Prepare early. This is a performance optimization for partial collections + prepare! + end + + def render + ActionController::Base.benchmark("Rendered #{@path}", Logger::DEBUG, false) do + @handler.render(self) + end + end + + def render_member(object) + @locals[@counter_name] += 1 + @locals[:object] = @locals[@variable_name] = object + render + end + + private + + def add_object_to_local_assigns!(object) + @locals[:object] ||= + @locals[@variable_name] ||= + if object.is_a?(ActionView::Base::ObjectWrapper) + object.value + else + object + end || @view.controller.instance_variable_get("@#{variable_name}") + end + + def extract_partial_name_and_path(view, partial_path) + path, partial_name = partial_pieces(view, partial_path) + [File.join(path, "_#{partial_name}"), partial_name.split('/').last.split('.').first.to_sym] + end + + def partial_pieces(view, partial_path) + if partial_path.include?('/') + return File.dirname(partial_path), File.basename(partial_path) + else + return view.controller.class.controller_path, partial_path + end + end + + def initialize_counter + @counter_name ||= "#{@variable_name}_counter".to_sym + @locals[@counter_name] = 0 + end + + end +end |