public
Description: rails plugin that enables inheritance of views along a controller class heirachy
Homepage: http://ianwhite.github.com/inherit_views
Clone URL: git://github.com/ianwhite/inherit_views.git
Some small refactoring using :delegate, and inline docs
ianwhite (author)
Sat Sep 20 20:54:37 -0700 2008
commit  19e89116da925afdddeb8383dca53a2de932d471
tree    96c1450cb5b73b8af463023f05a042861ba83e9f
parent  ce7bb6baa4aca976a9a726c4f052cdd416460b37
...
66
67
68
69
 
70
71
 
72
73
74
...
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
...
165
166
167
168
 
169
170
171
172
173
174
175
176
177
 
 
 
178
179
 
 
180
181
182
...
66
67
68
 
69
70
 
71
72
73
74
...
101
102
103
 
 
 
 
 
 
 
 
 
 
 
 
104
105
106
107
 
 
 
 
 
 
108
109
110
111
112
113
 
114
115
116
...
146
147
148
 
149
150
151
152
153
154
 
 
 
 
155
156
157
158
 
159
160
161
162
163
0
@@ -66,9 +66,9 @@ module Ardes#:nodoc:
0
       # or the current controller's default path if no argument is given
0
       def inherit_views(*paths)
0
         class_eval do
0
-          unless included_modules.include? ::Ardes::InheritViews::ActionController::InstanceMethods
0
+          unless respond_to?(:inherit_views?)
0
             extend ClassMethods
0
-            include InstanceMethods
0
+            delegate :inherit_views?, :inherit_view_paths, :to => 'self.class'
0
           end
0
           self.inherit_views = true
0
           self.inherit_view_paths = paths if paths.size > 0
0
@@ -101,35 +101,16 @@ module Ardes#:nodoc:
0
           instance_variable_set('@inherit_view_paths', [controller_path] + ((paths - [controller_path]) + inherited))
0
         end
0
       end
0
-      
0
-      module InstanceMethods
0
-        # Return true if the controller is inheriting views
0
-        def inherit_views?
0
-          self.class.inherit_views?
0
-        end
0
-        
0
-        # Return the inherit view paths, in order of self to ancestor
0
-        def inherit_view_paths
0
-          self.class.inherit_view_paths
0
-        end
0
-      end
0
     end
0
     
0
     # Mixin for ActionView to enable inherit views functionality.  This module is
0
     # included into ActionView::Base
0
-    #
0
-    # Those familiar with the internals of ActionView will know that the <tt>@first_render</tt>
0
-    # instance var is used to keep track of what is the 'root' template being rendered.  A similar
0
-    # variable <tt>@current_render</tt> is introduced to keep track of the filename of the currently rendered
0
-    # file.  This enables +render_parent+ to know which to file render.
0
-    #
0
     module ActionView
0
       extend ActiveSupport::Memoizable
0
       
0
       def self.included(base)# :nodoc:
0
         base.class_eval do
0
           alias_method_chain :render, :inherit_views
0
-          
0
           alias_method :_orig_pick_template, :_pick_template
0
           
0
           def _pick_template(template_path)
0
@@ -165,18 +146,18 @@ module Ardes#:nodoc:
0
       def _pick_inherited_template_for_controller(template_path, controller)
0
         _orig_pick_template(template_path)
0
       rescue ::ActionView::MissingTemplate
0
-        if (controller.inherit_views? rescue false)
0
+        if controller.respond_to?(:inherit_views?) && controller.inherit_views?
0
           _pick_inherited_template(template_path, controller.inherit_view_paths)
0
         end
0
       end  
0
       
0
       def _pick_inherited_template(template_path, inherit_view_paths)
0
-        starting_path = inherit_view_paths.detect {|p| template_path =~ /^#{p}\//}
0
-        
0
-        if starting_path 
0
-          inherit_paths_above_starting_path = inherit_view_paths.slice(inherit_view_paths.index(starting_path)+1..-1)
0
+        # first, we grab the inherited paths that are 'above' the given template_path
0
+        if starting_path = inherit_view_paths.detect {|path| template_path =~ /^#{path}\//}
0
+          paths_above_template_path = inherit_view_paths.slice(inherit_view_paths.index(starting_path)+1..-1)
0
           
0
-          inherit_paths_above_starting_path.each do |path|
0
+          # then, search through each one, substibuting the inherited path
0
+          paths_above_template_path.each do |path|
0
             inherited_template = begin
0
               _orig_pick_template(template_path.sub(/^#{starting_path}/, path))
0
             rescue ::ActionView::MissingTemplate

Comments