public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Performance: PartialTemplate#initialize
jeremy (author)
Mon Jun 16 23:59:22 -0700 2008
commit  d7b3c3395fd7debc05923dba1cbea69d30899827
tree    61b185dea2e28c797ed5f298aa78651e0d4ae3cf
parent  8190bce8bc7249b7b9f3680195336eb3ca9508ee
...
1
2
3
4
5
 
6
7
 
 
8
9
10
11
12
13
 
 
14
15
16
17
 
18
19
20
21
22
23
 
24
25
26
 
27
28
29
30
31
 
32
33
34
 
35
36
37
38
39
 
 
 
 
 
 
 
 
 
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
62
63
64
65
66
67
68
69
70
...
1
2
 
3
 
4
5
 
6
7
8
9
10
11
 
 
12
13
14
15
16
 
17
18
19
20
21
22
 
23
24
25
 
26
27
28
29
30
 
31
32
33
 
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
 
 
 
 
 
 
 
69
70
0
@@ -1,70 +1,70 @@
0
 module ActionView #:nodoc:
0
   class PartialTemplate < Template #:nodoc:
0
-    
0
     attr_reader :variable_name, :object
0
-    
0
+
0
     def initialize(view, partial_path, object = nil, locals = {})
0
-      @path, @variable_name = extract_partial_name_and_path(view, partial_path)
0
+      @view_controller = view.controller if view.respond_to?(:controller)
0
+      set_path_and_variable_name!(partial_path)
0
       super(view, @path, true, locals)
0
       add_object_to_local_assigns!(object)
0
 
0
       # This is needed here in order to compile template with knowledge of 'counter'
0
-      initialize_counter
0
-      
0
+      initialize_counter!
0
+
0
       # Prepare early. This is a performance optimization for partial collections
0
       prepare!
0
     end
0
-    
0
+
0
     def render
0
       ActionController::Base.benchmark("Rendered #{@path}", Logger::DEBUG, false) do
0
         @handler.render(self)
0
       end
0
     end
0
-    
0
+
0
     def render_member(object)
0
       @locals[:object] = @locals[@variable_name] = object
0
-      
0
+
0
       template = render_template
0
       @locals[@counter_name] += 1
0
       @locals.delete(@variable_name)
0
       @locals.delete(:object)
0
-      
0
+
0
       template
0
     end
0
-    
0
+
0
     def counter=(num)
0
       @locals[@counter_name] = num
0
     end
0
 
0
     private
0
+      def add_object_to_local_assigns!(object)
0
+        @locals[:object] ||=
0
+          @locals[@variable_name] ||=
0
+            if object.is_a?(ActionView::Base::ObjectWrapper)
0
+              object.value
0
+            else
0
+              object
0
+            end || @view_controller.instance_variable_get("@#{variable_name}")
0
+      end
0
 
0
-    def add_object_to_local_assigns!(object)
0
-      @locals[:object] ||=
0
-        @locals[@variable_name] ||=
0
-          if object.is_a?(ActionView::Base::ObjectWrapper)
0
-            object.value
0
-          else
0
-            object
0
-          end || @view.controller.instance_variable_get("@#{variable_name}")
0
-    end
0
-    
0
-    def extract_partial_name_and_path(view, partial_path)
0
-      path, partial_name = partial_pieces(view, partial_path)
0
-      [File.join(path, "_#{partial_name}"), partial_name.split('/').last.split('.').first.to_sym] 
0
-    end
0
-    
0
-    def partial_pieces(view, partial_path)
0
-      if partial_path.include?('/')
0
-        return File.dirname(partial_path), File.basename(partial_path)
0
-      else
0
-        return view.controller.class.controller_path, partial_path
0
+      def set_path_and_variable_name!(partial_path)
0
+        if partial_path.include?('/')
0
+          @variable_name = File.basename(partial_path)
0
+          @path = "#{File.dirname(partial_path)}/_#{@variable_name}"
0
+        elsif @view_controller
0
+          @variable_name = partial_path
0
+          @path = "#{@view_controller.class.controller_path}/_#{@variable_name}"
0
+        else
0
+          @variable_name = partial_path
0
+          @path = "_#{@variable_name}"
0
+        end
0
+
0
+        @variable_name = @variable_name.sub(/\..*$/, '').to_sym
0
+      end
0
+
0
+      def initialize_counter!
0
+        @counter_name ||= "#{@variable_name}_counter".to_sym
0
+        @locals[@counter_name] = 0
0
       end
0
-    end
0
-    
0
-    def initialize_counter
0
-      @counter_name ||= "#{@variable_name}_counter".to_sym
0
-      @locals[@counter_name] = 0
0
-    end
0
-    
0
   end
0
 end

Comments