public
Rubygem
Description: Ramaze is a simple, light and modular open-source web application framework written in Ruby.
Homepage: http://ramaze.net
Clone URL: git://github.com/manveru/ramaze.git
Click here to lend your support to: ramaze and make a donation at www.pledgie.com !
Allow controller subclasses to pick up template mappings defined in 
superclasses.

I noticed that if I define a template mapping with the template method, 
subclasses of the defining controller do not respect that mapping. This only 
really applies to the template :foo, :bar usage, where no explicit controller is 
specified. With these modifications, template mappings such as that will assume 
you mean the 'current' controller. Specs have been added to test this new 
functionality.
Sam Carr (author)
Mon Jul 21 02:23:30 -0700 2008
manveru (committer)
Mon Jul 21 04:52:54 -0700 2008
commit  8ebf919d6042009df8617479d0388b6142dcf092
tree    315f2e878caed5589d48ebcc8a521388cc5a2811
parent  155b9a4fc0c1b3a7b0972ee59f9dc47d8ed0915d
...
218
219
220
221
222
223
 
 
 
 
 
 
 
 
 
224
225
226
...
218
219
220
 
 
 
221
222
223
224
225
226
227
228
229
230
231
232
0
@@ -218,9 +218,15 @@ module Ramaze
0
           end
0
           trait "#{this}_template" => info
0
         else
0
-          controller, action, *ignored = argv
0
-          controller, action = self, controller unless action
0
-          trait "#{this}_template" => {:controller => controller, :action => action}
0
+          # Only explicitly set the controller to use, if it was explicitly given.
0
+          # This helps ensure that template mappings still work in subclasses
0
+          # of this controller.
0
+          first, second, *ignored = argv
0
+          if second
0
+            trait "#{this}_template" => {:controller => first, :action => second}
0
+          else
0
+            trait "#{this}_template" => {:action => first}
0
+          end
0
         end
0
       end
0
 
...
104
105
106
107
 
 
108
109
110
 
 
111
112
113
...
104
105
106
 
107
108
109
110
111
112
113
114
115
116
0
@@ -104,10 +104,13 @@ module Ramaze
0
 
0
       def resolve_action(path, *parameter)
0
         path, parameter = path.to_s, parameter.map{|e| e.to_s}
0
-        if info = trait["#{path}_template"]
0
+        # Use ancestral_trait so if template is set in superclass, it is still found.
0
+        if info = ancestral_trait["#{path}_template"]
0
           template = info[:file]
0
           unless template
0
             controller, action = info.values_at :controller, :action
0
+            # Controller may not have been explicitly set, in which case use self.
0
+            controller ||= self
0
             template = controller.resolve_template(action)
0
           end
0
         end
...
4
5
6
 
 
 
7
8
9
...
18
19
20
 
 
 
 
 
 
 
 
 
 
 
 
21
...
4
5
6
7
8
9
10
11
12
...
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
0
@@ -4,6 +4,9 @@
0
 require 'spec/helper'
0
 
0
 class BaseController < Ramaze::Controller
0
+  template :foo, :bar
0
+  template :one, self, :another
0
+
0
   def test() 'test' end
0
 end
0
 
0
@@ -18,4 +21,16 @@ describe 'Controller' do
0
   it 'should allow sub-classing MainController' do
0
     get('/test').body.should == 'test'
0
   end
0
+
0
+  it 'should respect template mappings set in superclass, with no explicit controller' do
0
+    # The template file it should use is view/bar.xhtml, as the template mapping doesn't
0
+    # specify a controller, so it will be implicitly relative to MainController.
0
+    get('/foo').body.should == 'bar'
0
+  end
0
+
0
+  it 'should respect template mappings set in superclass, with an explicit controller' do
0
+    # Note that the template file it should use is view/base/another.xhtml, because
0
+    # BaseController explicitly specifies the template mapping in relation to self.
0
+    get('/one').body.should == 'another'
0
+  end
0
 end

Comments