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
All specs passing except some to do with render parent
ianwhite (author)
Sat Sep 20 20:44:27 -0700 2008
commit  ce7bb6baa4aca976a9a726c4f052cdd416460b37
tree    ef8b35cb8ec3005ace5cac3d5055e846031eacd7
parent  32f6e24e8e0b83ca40a30262d257556adc9b3609
...
124
125
126
 
 
127
128
129
...
131
132
133
134
 
135
136
137
...
139
140
141
142
143
144
145
146
147
148
149
150
151
 
 
 
 
 
 
152
153
154
...
160
161
162
163
 
164
165
166
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
167
168
169
...
124
125
126
127
128
129
130
131
...
133
134
135
 
136
137
138
139
...
141
142
143
 
 
 
 
 
 
 
 
 
 
144
145
146
147
148
149
150
151
152
...
158
159
160
 
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
0
@@ -124,6 +124,8 @@ module Ardes#:nodoc:
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
@@ -131,7 +133,7 @@ module Ardes#:nodoc:
0
           alias_method :_orig_pick_template, :_pick_template
0
           
0
           def _pick_template(template_path)
0
-            _orig_pick_template(template_path)
0
+            _pick_inherited_template_for_controller(template_path, controller)
0
           end
0
         end
0
       end
0
@@ -139,16 +141,12 @@ module Ardes#:nodoc:
0
       # Renders the parent template for the current template
0
       # takes normal rendering options (:layout, :locals, etc)
0
       def render_parent(options = {})
0
-        #raise ArgumentError, 'render_parent requires that controller inherit_views' unless (controller.inherit_views? rescue false)
0
-        #
0
-        #if @current_render
0
-        #  if @current_render[:file] && (file = _pick_template(@current_render[:file], include_self = false))
0
-        #    return render(options.merge(:file => file))
0
-        #  elsif @current_render[:partial] && (partial = _pick_template(_pick_partial_template(@current_render[:partial]), include_self = false))
0
-        #    return render(options.merge(:partial => partial))
0
-        #  end
0
-        #end
0
-        #raise InheritedFileNotFound, "no parent for #{@current_render.inspect} found"
0
+        raise ArgumentError, 'render_parent requires that controller inherit_views' unless (controller.inherit_views? rescue false)
0
+        
0
+        if @current_render && @current_render[:file] && (file = _pick_inherited_template(@current_render[:file], controller.inherit_view_paths))
0
+          return render(options.merge(:file => file))
0
+        end
0
+        raise InheritedFileNotFound, "no parent for #{@current_render[:file]} found"
0
       end
0
 
0
       # Find an inherited template path prior to rendering, if appropriate.
0
@@ -160,10 +158,37 @@ module Ardes#:nodoc:
0
     
0
       # Find an inherited template path for a controller context
0
       def inherited_template_path(template_path, controller_class = controller.class)
0
-        #_pick_template(template_path, true, controller_class.inherit_view_paths)
0
+        _pick_inherited_template_for_controller(template_path, controller).to_s
0
       end
0
     
0
     private
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
+          _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
+          
0
+          inherit_paths_above_starting_path.each do |path|
0
+            inherited_template = begin
0
+              _orig_pick_template(template_path.sub(/^#{starting_path}/, path))
0
+            rescue ::ActionView::MissingTemplate
0
+              nil
0
+            end
0
+            
0
+            return inherited_template if inherited_template
0
+          end
0
+        end
0
+      end
0
+      memoize :_pick_inherited_template
0
+      
0
       def _with_current_render_of(options, &block)
0
         orig, @current_render = @current_render, options
0
         yield
...
40
41
42
43
44
45
46
 
 
47
48
49
50
...
40
41
42
 
 
 
 
43
44
45
46
47
48
0
@@ -40,10 +40,8 @@ describe AController, " < TestController; inherit_views" do
0
     end
0
   
0
     it "GET :inherited_template_path should render its contents" do
0
-      pending do
0
-        get :inherited_template_path
0
-        response.body.should == 'b/in_ab.html.erb'
0
-      end
0
+      get :inherited_template_path
0
+      response.body.should == 'b/in_ab.html.erb'
0
     end
0
   end
0
 end
0
\ No newline at end of file
...
40
41
42
43
44
45
46
 
 
47
48
49
...
40
41
42
 
 
 
 
43
44
45
46
47
0
@@ -40,10 +40,8 @@ describe BController, " < TestController; inherit_views 'a'" do
0
     end
0
 
0
     it "GET :render_parent should render a/render_parent inside b/render_parent" do
0
-      pending do 
0
-        get :render_parent
0
-        response.body.should == "b:render_parent(a:render_parent)"
0
-      end
0
+      get :render_parent
0
+      response.body.should == "b:render_parent(a:render_parent)"
0
     end
0
   
0
     it "GET :bad_render_parent should raise ActionView::TemplateError as there is no parent to render" do

Comments