Skip to content
This repository
Browse code

Revert "Allow loading external route files from the router"

This reverts commit 6acebb3.

Usage of this feature did not reveal any improvement in existing apps.

Conflicts:

	actionpack/lib/action_dispatch/routing/mapper.rb
	guides/source/routing.textile
	railties/lib/rails/engine.rb
	railties/lib/rails/paths.rb
	railties/test/paths_test.rb
  • Loading branch information...
commit 5e7d6bba79393de0279917f93b82f3b7b176f4b5 1 parent 50b6110
José Valim authored June 29, 2012
17  actionpack/lib/action_dispatch/routing/mapper.rb
@@ -1316,22 +1316,6 @@ def shallow?
1316 1316
           parent_resource.instance_of?(Resource) && @scope[:shallow]
1317 1317
         end
1318 1318
 
1319  
-        def draw(name)
1320  
-          path = @draw_paths.find do |_path|
1321  
-            File.exists? "#{_path}/#{name}.rb"
1322  
-          end
1323  
-
1324  
-          unless path
1325  
-            msg  = "Your router tried to #draw the external file #{name}.rb,\n" \
1326  
-                   "but the file was not found in:\n\n"
1327  
-            msg += @draw_paths.map { |_path| " * #{_path}" }.join("\n")
1328  
-            raise ArgumentError, msg
1329  
-          end
1330  
-
1331  
-          route_path = "#{path}/#{name}.rb"
1332  
-          instance_eval(File.read(route_path), route_path.to_s)
1333  
-        end
1334  
-
1335 1319
         # match 'path' => 'controller#action'
1336 1320
         # match 'path', to: 'controller#action'
1337 1321
         # match 'path', 'otherpath', on: :member, via: :get
@@ -1581,7 +1565,6 @@ def name_for_action(as, action) #:nodoc:
1581 1565
 
1582 1566
       def initialize(set) #:nodoc:
1583 1567
         @set = set
1584  
-        @draw_paths = set.draw_paths
1585 1568
         @scope = { :path_names => @set.resources_path_names }
1586 1569
       end
1587 1570
 
2  actionpack/lib/action_dispatch/routing/route_set.rb
@@ -237,7 +237,6 @@ def optimized_helper(route)
237 237
       attr_accessor :formatter, :set, :named_routes, :default_scope, :router
238 238
       attr_accessor :disable_clear_and_finalize, :resources_path_names
239 239
       attr_accessor :default_url_options, :request_class, :valid_conditions
240  
-      attr_accessor :draw_paths
241 240
 
242 241
       alias :routes :set
243 242
 
@@ -249,7 +248,6 @@ def initialize(request_class = ActionDispatch::Request)
249 248
         self.named_routes = NamedRouteCollection.new
250 249
         self.resources_path_names = self.class.default_resources_path_names.dup
251 250
         self.default_url_options = {}
252  
-        self.draw_paths = []
253 251
 
254 252
         self.request_class = request_class
255 253
         @valid_conditions = { :controller => true, :action => true }
18  guides/source/routing.textile
Source Rendered
@@ -845,24 +845,6 @@ end
845 845
 
846 846
 This will create routing helpers such as +magazine_periodical_ads_url+ and +edit_magazine_periodical_ad_path+.
847 847
 
848  
-h3. Breaking Up a Large Route File
849  
-
850  
-If you have a large route file that you would like to break up into multiple files, you can use the +#draw+ method in your router:
851  
-
852  
-<ruby>
853  
-draw :admin
854  
-</ruby>
855  
-
856  
-Then, create a file called +config/routes/admin.rb+. Name the file the same as the symbol passed to the +draw+ method. You can then use the normal routing DSL inside that file:
857  
-
858  
-<ruby>
859  
-# in config/routes/admin.rb
860  
-
861  
-namespace :admin do
862  
-  resources :posts
863  
-end
864  
-</ruby>
865  
-
866 848
 h3. Inspecting and Testing Routes
867 849
 
868 850
 Rails offers facilities for inspecting and testing your routes.
13  railties/lib/rails/application/routes_reloader.rb
@@ -3,13 +3,12 @@
3 3
 module Rails
4 4
   class Application
5 5
     class RoutesReloader
6  
-      attr_reader :route_sets, :paths, :external_routes
  6
+      attr_reader :route_sets, :paths
7 7
       delegate :execute_if_updated, :execute, :updated?, :to => :updater
8 8
 
9 9
       def initialize
10  
-        @paths           = []
11  
-        @route_sets      = []
12  
-        @external_routes = []
  10
+        @paths      = []
  11
+        @route_sets = []
13 12
       end
14 13
 
15 14
       def reload!
@@ -24,11 +23,7 @@ def reload!
24 23
 
25 24
       def updater
26 25
         @updater ||= begin
27  
-          dirs = @external_routes.each_with_object({}) do |dir, hash|
28  
-            hash[dir.to_s] = %w(rb)
29  
-          end
30  
-
31  
-          updater = ActiveSupport::FileUpdateChecker.new(paths, dirs) { reload! }
  26
+          updater = ActiveSupport::FileUpdateChecker.new(paths) { reload! }
32 27
           updater.execute
33 28
           updater
34 29
         end
7  railties/lib/rails/engine.rb
@@ -508,10 +508,7 @@ def env_config
508 508
     # Defines the routes for this engine. If a block is given to
509 509
     # routes, it is appended to the engine.
510 510
     def routes
511  
-      @routes ||= ActionDispatch::Routing::RouteSet.new.tap do |routes|
512  
-        routes.draw_paths.concat paths["config/routes"].paths
513  
-      end
514  
-
  511
+      @routes ||= ActionDispatch::Routing::RouteSet.new
515 512
       @routes.append(&Proc.new) if block_given?
516 513
       @routes
517 514
     end
@@ -555,12 +552,10 @@ def load_seed
555 552
 
556 553
     initializer :add_routing_paths do |app|
557 554
       paths = self.paths["config/routes.rb"].existent
558  
-      external_paths = self.paths["config/routes"].paths
559 555
 
560 556
       if routes? || paths.any?
561 557
         app.routes_reloader.paths.unshift(*paths)
562 558
         app.routes_reloader.route_sets << routes
563  
-        app.routes_reloader.external_routes.unshift(*external_paths)
564 559
       end
565 560
     end
566 561
 
1  railties/lib/rails/engine/configuration.rb
@@ -53,7 +53,6 @@ def paths
53 53
           paths.add "config/initializers", :glob => "**/*.rb"
54 54
           paths.add "config/locales",      :glob => "*.{rb,yml}"
55 55
           paths.add "config/routes.rb"
56  
-          paths.add "config/routes",       :glob => "**/*.rb"
57 56
           paths.add "db"
58 57
           paths.add "db/migrate"
59 58
           paths.add "db/seeds.rb"
10  railties/lib/rails/paths.rb
@@ -114,7 +114,7 @@ def filter_by(constraint)
114 114
     class Path
115 115
       include Enumerable
116 116
 
117  
-      attr_reader :path, :root
  117
+      attr_reader :path
118 118
       attr_accessor :glob
119 119
 
120 120
       def initialize(root, current, paths, options = {})
@@ -180,14 +180,6 @@ def to_ary
180 180
         @paths
181 181
       end
182 182
 
183  
-      def paths
184  
-        raise "You need to set a path root" unless @root.path
185  
-
186  
-        map do |p|
187  
-          File.join @root.path, p
188  
-        end
189  
-      end
190  
-
191 183
       # Expands all paths against the root and return all unique values.
192 184
       def expanded
193 185
         raise "You need to set a path root" unless @root.path
2  railties/test/application/paths_test.rb
@@ -50,8 +50,6 @@ def assert_not_in_load_path(*path)
50 50
       assert_path @paths["config/locales"],      "config/locales/en.yml"
51 51
       assert_path @paths["config/environment"],  "config/environment.rb"
52 52
       assert_path @paths["config/environments"], "config/environments/development.rb"
53  
-      assert_path @paths["config/routes.rb"],    "config/routes.rb"
54  
-      assert_path @paths["config/routes"],       "config/routes"
55 53
 
56 54
       assert_equal root("app", "controllers"), @paths["app/controllers"].expanded.first
57 55
     end
83  railties/test/application/routing_test.rb
@@ -178,90 +178,7 @@ def index
178 178
       assert_equal 'WIN', last_response.body
179 179
     end
180 180
 
181  
-    test "routes drawing from config/routes" do
182  
-      app_file 'config/routes.rb', <<-RUBY
183  
-        AppTemplate::Application.routes.draw do
184  
-          draw :external
185  
-        end
186  
-      RUBY
187  
-
188  
-      app_file 'config/routes/external.rb', <<-RUBY
189  
-        get ':controller/:action'
190  
-      RUBY
191  
-
192  
-      controller :success, <<-RUBY
193  
-        class SuccessController < ActionController::Base
194  
-          def index
195  
-            render :text => "success!"
196  
-          end
197  
-        end
198  
-      RUBY
199  
-
200  
-      app 'development'
201  
-      get '/success/index'
202  
-      assert_equal 'success!', last_response.body
203  
-    end
204  
-
205 181
     {"development" => "baz", "production" => "bar"}.each do |mode, expected|
206  
-      test "reloads routes when external configuration is changed in #{mode}" do
207  
-        controller :foo, <<-RUBY
208  
-          class FooController < ApplicationController
209  
-            def bar
210  
-              render :text => "bar"
211  
-            end
212  
-
213  
-            def baz
214  
-              render :text => "baz"
215  
-            end
216  
-          end
217  
-        RUBY
218  
-
219  
-        app_file 'config/routes.rb', <<-RUBY
220  
-          AppTemplate::Application.routes.draw do
221  
-            draw :external
222  
-          end
223  
-        RUBY
224  
-
225  
-        app_file 'config/routes/external.rb', <<-RUBY
226  
-          get 'foo', :to => 'foo#bar'
227  
-        RUBY
228  
-
229  
-        app(mode)
230  
-
231  
-        get '/foo'
232  
-        assert_equal 'bar', last_response.body
233  
-
234  
-        app_file 'config/routes/external.rb', <<-RUBY
235  
-          get 'foo', :to => 'foo#baz'
236  
-        RUBY
237  
-
238  
-        sleep 0.1
239  
-
240  
-        get '/foo'
241  
-        assert_equal expected, last_response.body
242  
-
243  
-        app_file 'config/routes.rb', <<-RUBY
244  
-          AppTemplate::Application.routes.draw do
245  
-            draw :external
246  
-            draw :other_external
247  
-          end
248  
-        RUBY
249  
-
250  
-        app_file 'config/routes/other_external.rb', <<-RUBY
251  
-          get 'win', :to => 'foo#baz'
252  
-        RUBY
253  
-
254  
-        sleep 0.1
255  
-
256  
-        get '/win'
257  
-
258  
-        if mode == "development"
259  
-          assert_equal expected, last_response.body
260  
-        else
261  
-          assert_equal 404, last_response.status
262  
-        end
263  
-      end
264  
-
265 182
       test "reloads routes when configuration is changed in #{mode}" do
266 183
         controller :foo, <<-RUBY
267 184
           class FooController < ApplicationController
2  railties/test/paths_test.rb
@@ -29,7 +29,6 @@ def setup
29 29
   test "creating a root level path" do
30 30
     @root.add "app"
31 31
     assert_equal ["/foo/bar/app"], @root["app"].to_a
32  
-    assert_equal ["/foo/bar/app"], @root["app"].paths
33 32
   end
34 33
 
35 34
   test "creating a root level path with options" do
@@ -192,7 +191,6 @@ def setup
192 191
     @root["app"] = "/app"
193 192
     @root["app"].glob = "*.rb"
194 193
     assert_equal "*.rb", @root["app"].glob
195  
-    assert_equal ["/foo/bar/app"], @root["app"].paths
196 194
   end
197 195
 
198 196
   test "it should be possible to override a path's default glob without assignment" do

12 notes on commit 5e7d6bb

Henrik Nyh

@josevalim What do you mean by "Usage of this feature did not reveal any improvement in existing apps."? Compared to doing it manually, without special framework support?

I've found it useful to split routes into multiple files on larger projects, but IIRC it was easy enough with just multiple My::Application.routes.draw or instance_eval or something.

Rafael Mendonça França
Owner

This was reverted in favor of https://github.com/rails/routing_concerns, that we think is a better approach.

Henrik Nyh

@rafaelfranca Thanks! I don't see from the README how routing_concerns does anything to help split up routes, though – how would you go about that?

Steve Klabnik
Collaborator

The point of 'splitting up routes' was to make the routes file easier to read. routing_concerns will (hopefully) DRY up your routes so that you don't need to split them up anymore.

Henrik Nyh

@steveklabnik Alright, thanks!

Kurakin Alexander

But Rails makes possible to split much: locales, migrations... It's useful for spliting-up-by-components very much!

Kurakin Alexander

@steveklabnik amn't I right (see previous message)?

Steve Klabnik
Collaborator

I don't know if you're right or not. Personally, I never tried this feature. @dhh didn't find it very helpful.

Alexander Trauzzi

Are there any examples of this new approach to defining multiple routes? Rails 4 docs make it look like concerns can only be used to modify existing routes...?

Robin Dupret
Collaborator

Rails 4 docs make it look like concerns can only be used to modify existing routes...?

Actually concern routes are provided for DRY concerns, this is not really an alternative to the reverted patch here. I guess there is no equivalent at the time being but I may be wrong. It looks like this is not so hard to implement such feature inside your application.

David Heinemeier Hansson
Owner
dhh commented on 5e7d6bb March 30, 2014
Alexander Trauzzi

Agreed, especially if the language already supports it, no need to try and over-systemize it. I think I've got something figured out.

Please sign in to comment.
Something went wrong with that request. Please try again.