public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Added support for multiple routes files and made draw not clear the map so they 
can be additive
dhh (author)
Wed Nov 26 06:57:36 -0800 2008
commit  40b40c487040d9c721d486e8ec8cfbc53a8cd79a
tree    942716155ba5aec22e70daff47498e4c3a7302c5
parent  63d8f56774dcb1ea601928c3eb6c119d359fae10
...
200
201
202
203
 
204
205
 
 
206
207
208
...
216
217
218
219
220
221
222
...
240
241
242
 
 
 
 
 
 
 
 
 
 
 
 
 
 
243
244
 
245
246
247
...
250
251
252
253
254
255
256
257
258
 
 
 
 
 
 
259
 
260
261
262
263
264
265
266
 
 
 
267
268
269
270
 
 
 
 
 
 
 
 
 
 
 
 
 
 
271
272
273
...
200
201
202
 
203
204
205
206
207
208
209
210
...
218
219
220
 
221
222
223
...
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
 
259
260
261
262
...
265
266
267
 
 
 
 
 
 
268
269
270
271
272
273
274
275
276
277
278
279
 
 
 
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
0
@@ -200,9 +200,11 @@ module ActionController
0
           end
0
       end
0
 
0
-      attr_accessor :routes, :named_routes, :configuration_file
0
+      attr_accessor :routes, :named_routes, :configuration_files
0
 
0
       def initialize
0
+        self.configuration_files = []
0
+
0
         self.routes = []
0
         self.named_routes = NamedRouteCollection.new
0
 
0
@@ -216,7 +218,6 @@ module ActionController
0
       end
0
 
0
       def draw
0
-        clear!
0
         yield Mapper.new(self)
0
         install_helpers
0
       end
0
@@ -240,8 +241,22 @@ module ActionController
0
         routes.empty?
0
       end
0
 
0
+      def add_configuration_file(path)
0
+        self.configuration_files << path
0
+      end
0
+
0
+      # Deprecated accessor
0
+      def configuration_file=(path)
0
+        add_configuration_file(path)
0
+      end
0
+      
0
+      # Deprecated accessor
0
+      def configuration_file
0
+        configuration_files
0
+      end
0
+
0
       def load!
0
-        Routing.use_controllers! nil # Clear the controller cache so we may discover new ones
0
+        Routing.use_controllers!(nil) # Clear the controller cache so we may discover new ones
0
         clear!
0
         load_routes!
0
       end
0
@@ -250,24 +265,39 @@ module ActionController
0
       alias reload! load!
0
 
0
       def reload
0
-        if @routes_last_modified && configuration_file
0
-          mtime = File.stat(configuration_file).mtime
0
-          # if it hasn't been changed, then just return
0
-          return if mtime == @routes_last_modified
0
-          # if it has changed then record the new time and fall to the load! below
0
-          @routes_last_modified = mtime
0
+        if configuration_files.any? && @routes_last_modified
0
+          if routes_changed_at == @routes_last_modified
0
+            return # routes didn't change, don't reload
0
+          else
0
+            @routes_last_modified = routes_changed_at
0
+          end
0
         end
0
+
0
         load!
0
       end
0
 
0
       def load_routes!
0
-        if configuration_file
0
-          load configuration_file
0
-          @routes_last_modified = File.stat(configuration_file).mtime
0
+        if configuration_files.any?
0
+          configuration_files.each { |config| load(config) }
0
+          @routes_last_modified = routes_changed_at
0
         else
0
           add_route ":controller/:action/:id"
0
         end
0
       end
0
+      
0
+      def routes_changed_at
0
+        routes_changed_at = nil
0
+        
0
+        configuration_files.each do |config|
0
+          config_changed_at = File.stat(config).mtime
0
+
0
+          if routes_changed_at.nil? || config_changed_at > routes_changed_at
0
+            routes_changed_at = config_changed_at 
0
+          end
0
+        end
0
+        
0
+        routes_changed_at
0
+      end
0
 
0
       def add_route(path, options = {})
0
         route = builder.build(path, options)
...
747
748
749
750
751
752
753
 
 
 
 
754
755
 
756
757
758
...
769
770
771
 
772
773
774
...
1002
1003
1004
 
 
1005
1006
1007
...
1155
1156
1157
 
1158
1159
1160
1161
 
1162
1163
1164
...
2399
2400
2401
2402
 
2403
2404
2405
2406
2407
2408
 
2409
2410
2411
...
2448
2449
2450
2451
 
 
2452
2453
2454
2455
2456
 
 
 
 
 
 
 
 
 
 
 
2457
2458
2459
...
747
748
749
 
750
751
752
753
754
755
756
757
758
759
760
761
762
...
773
774
775
776
777
778
779
...
1007
1008
1009
1010
1011
1012
1013
1014
...
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
...
2408
2409
2410
 
2411
2412
2413
2414
2415
2416
 
2417
2418
2419
2420
...
2457
2458
2459
 
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
0
@@ -747,12 +747,16 @@ uses_mocha 'LegacyRouteSet, Route, RouteSet and RouteLoading' do
0
       ActionController::Base.optimise_named_routes = true
0
 
0
       @rs = ::ActionController::Routing::RouteSet.new
0
-      @rs.draw {|m| m.connect ':controller/:action/:id' }
0
 
0
       ActionController::Routing.use_controllers! %w(content admin/user admin/news_feed)
0
     end
0
+    
0
+    def teardown
0
+      @rs.clear!
0
+    end
0
 
0
     def test_default_setup
0
+      @rs.draw {|m| m.connect ':controller/:action/:id' }
0
       assert_equal({:controller => "content", :action => 'index'}, rs.recognize_path("/content"))
0
       assert_equal({:controller => "content", :action => 'list'}, rs.recognize_path("/content/list"))
0
       assert_equal({:controller => "content", :action => 'show', :id => '10'}, rs.recognize_path("/content/show/10"))
0
@@ -769,6 +773,7 @@ uses_mocha 'LegacyRouteSet, Route, RouteSet and RouteLoading' do
0
     end
0
 
0
     def test_ignores_leading_slash
0
+      @rs.clear!
0
       @rs.draw {|m| m.connect '/:controller/:action/:id'}
0
       test_default_setup
0
     end
0
@@ -1002,6 +1007,8 @@ uses_mocha 'LegacyRouteSet, Route, RouteSet and RouteLoading' do
0
     end
0
 
0
     def test_changing_controller
0
+      @rs.draw {|m| m.connect ':controller/:action/:id' }
0
+
0
       assert_equal '/admin/stuff/show/10', rs.generate(
0
         {:controller => 'stuff', :action => 'show', :id => 10},
0
         {:controller => 'admin/user', :action => 'index'}
0
@@ -1155,10 +1162,12 @@ uses_mocha 'LegacyRouteSet, Route, RouteSet and RouteLoading' do
0
     end
0
 
0
     def test_action_expiry
0
+      @rs.draw {|m| m.connect ':controller/:action/:id' }
0
       assert_equal '/content', rs.generate({:controller => 'content'}, {:controller => 'content', :action => 'show'})
0
     end
0
 
0
     def test_recognition_with_uppercase_controller_name
0
+      @rs.draw {|m| m.connect ':controller/:action/:id' }
0
       assert_equal({:controller => "content", :action => 'index'}, rs.recognize_path("/Content"))
0
       assert_equal({:controller => "content", :action => 'list'}, rs.recognize_path("/ConTent/list"))
0
       assert_equal({:controller => "content", :action => 'show', :id => '10'}, rs.recognize_path("/CONTENT/show/10"))
0
@@ -2399,13 +2408,13 @@ uses_mocha 'LegacyRouteSet, Route, RouteSet and RouteLoading' do
0
     def setup
0
       routes.instance_variable_set '@routes_last_modified', nil
0
       silence_warnings { Object.const_set :RAILS_ROOT, '.' }
0
-      ActionController::Routing::Routes.configuration_file = File.join(RAILS_ROOT, 'config', 'routes.rb')
0
+      routes.add_configuration_file(File.join(RAILS_ROOT, 'config', 'routes.rb'))
0
 
0
       @stat = stub_everything
0
     end
0
 
0
     def teardown
0
-      ActionController::Routing::Routes.configuration_file = nil
0
+      ActionController::Routing::Routes.configuration_files.clear
0
       Object.send :remove_const, :RAILS_ROOT
0
     end
0
 
0
@@ -2448,12 +2457,24 @@ uses_mocha 'LegacyRouteSet, Route, RouteSet and RouteLoading' do
0
     end
0
 
0
     def test_load_with_configuration
0
-      routes.configuration_file = "foobarbaz"
0
+      routes.configuration_files.clear
0
+      routes.add_configuration_file("foobarbaz")
0
       File.expects(:stat).returns(@stat)
0
       routes.expects(:load).with("foobarbaz")
0
 
0
       routes.reload
0
     end
0
+    
0
+    def test_load_multiple_configurations
0
+      routes.add_configuration_file("engines.rb")
0
+      
0
+      File.expects(:stat).at_least_once.returns(@stat)
0
+
0
+      routes.expects(:load).with('./config/routes.rb')
0
+      routes.expects(:load).with('engines.rb')
0
+
0
+      routes.reload
0
+    end
0
 
0
     private
0
       def routes

Comments

wkonkel Wed Nov 26 12:08:26 -0800 2008

Here’s another approach (which is additive to this commit, not contradictory) for routing from plugins:

http://blog.hungrymachine.com/2008/11/26/acts_as_routing-modular-routing-in-rails-and-merb