diff options
author | Jose and Yehuda <wycats@gmail.com> | 2012-04-25 16:06:20 -0500 |
---|---|---|
committer | Yehuda Katz <wycats@gmail.com> | 2012-04-25 16:07:17 -0500 |
commit | 6acebb38bc0637bc05c19d87f8767f16ce79189b (patch) | |
tree | e37fa39f516e16c1d36e3b5202e9231c56aa4305 /railties | |
parent | 3c100cfe8ed5a875b0bbdc8fa4e8f2b0cbf78676 (diff) | |
download | rails-6acebb38bc0637bc05c19d87f8767f16ce79189b.tar.gz rails-6acebb38bc0637bc05c19d87f8767f16ce79189b.tar.bz2 rails-6acebb38bc0637bc05c19d87f8767f16ce79189b.zip |
Allow loading external route files from the router
This feature enables the ability to load an
external routes file from the router via:
draw :filename
External routes files go in +config/routes+. This
feature works in both engines and applications.
Diffstat (limited to 'railties')
-rw-r--r-- | railties/lib/rails/engine.rb | 3 | ||||
-rw-r--r-- | railties/lib/rails/engine/configuration.rb | 3 | ||||
-rw-r--r-- | railties/lib/rails/paths.rb | 12 | ||||
-rw-r--r-- | railties/test/application/paths_test.rb | 2 | ||||
-rw-r--r-- | railties/test/application/routing_test.rb | 24 | ||||
-rw-r--r-- | railties/test/paths_test.rb | 2 |
6 files changed, 43 insertions, 3 deletions
diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index 43ee396cbe..3b302b23a7 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -487,6 +487,7 @@ module Rails def routes @routes ||= ActionDispatch::Routing::RouteSet.new + @routes.draw_paths.concat paths["config/routes"].paths @routes.append(&Proc.new) if block_given? @routes end @@ -544,7 +545,7 @@ module Rails end initializer :add_routing_paths do |app| - paths = self.paths["config/routes"].existent + paths = self.paths["config/routes.rb"].existent if routes? || paths.any? app.routes_reloader.paths.unshift(*paths) diff --git a/railties/lib/rails/engine/configuration.rb b/railties/lib/rails/engine/configuration.rb index d7405cb519..9f7c5f8704 100644 --- a/railties/lib/rails/engine/configuration.rb +++ b/railties/lib/rails/engine/configuration.rb @@ -52,7 +52,8 @@ module Rails paths.add "config/environments", :glob => "#{Rails.env}.rb" paths.add "config/initializers", :glob => "**/*.rb" paths.add "config/locales", :glob => "*.{rb,yml}" - paths.add "config/routes", :with => "config/routes.rb" + paths.add "config/routes.rb" + paths.add "config/routes", :glob => "**/*.rb" paths.add "db" paths.add "db/migrate" paths.add "db/seeds", :with => "db/seeds.rb" diff --git a/railties/lib/rails/paths.rb b/railties/lib/rails/paths.rb index dd1790299d..fced6f8896 100644 --- a/railties/lib/rails/paths.rb +++ b/railties/lib/rails/paths.rb @@ -1,3 +1,5 @@ +require "pathname" + module Rails module Paths # This object is an extended hash that behaves as root of the <tt>Rails::Paths</tt> system. @@ -114,7 +116,7 @@ module Rails class Path include Enumerable - attr_reader :path + attr_reader :path, :root attr_accessor :glob def initialize(root, current, paths, options = {}) @@ -180,6 +182,14 @@ module Rails @paths end + def paths + raise "You need to set a path root" unless @root.path + + map do |p| + Pathname.new(@root.path).join(p) + end + end + # Expands all paths against the root and return all unique values. def expanded raise "You need to set a path root" unless @root.path diff --git a/railties/test/application/paths_test.rb b/railties/test/application/paths_test.rb index 4029984ce9..e0893f53be 100644 --- a/railties/test/application/paths_test.rb +++ b/railties/test/application/paths_test.rb @@ -50,6 +50,8 @@ module ApplicationTests assert_path @paths["config/locales"], "config/locales/en.yml" assert_path @paths["config/environment"], "config/environment.rb" assert_path @paths["config/environments"], "config/environments/development.rb" + assert_path @paths["config/routes.rb"], "config/routes.rb" + assert_path @paths["config/routes"], "config/routes" assert_equal root("app", "controllers"), @paths["app/controllers"].expanded.first end diff --git a/railties/test/application/routing_test.rb b/railties/test/application/routing_test.rb index 204f43a442..1dac086119 100644 --- a/railties/test/application/routing_test.rb +++ b/railties/test/application/routing_test.rb @@ -166,6 +166,30 @@ module ApplicationTests assert_equal 'WIN', last_response.body end + test "routes drawing from config/routes" do + app_file 'config/routes.rb', <<-RUBY + AppTemplate::Application.routes.draw do + draw :external + end + RUBY + + app_file 'config/routes/external.rb', <<-RUBY + get ':controller/:action' + RUBY + + controller :success, <<-RUBY + class SuccessController < ActionController::Base + def index + render :text => "success!" + end + end + RUBY + + app 'development' + get '/success/index' + assert_equal 'success!', last_response.body + end + {"development" => "baz", "production" => "bar"}.each do |mode, expected| test "reloads routes when configuration is changed in #{mode}" do controller :foo, <<-RUBY diff --git a/railties/test/paths_test.rb b/railties/test/paths_test.rb index d334034e7d..aa04cad033 100644 --- a/railties/test/paths_test.rb +++ b/railties/test/paths_test.rb @@ -29,6 +29,7 @@ class PathsTest < ActiveSupport::TestCase test "creating a root level path" do @root.add "app" assert_equal ["/foo/bar/app"], @root["app"].to_a + assert_equal [Pathname.new("/foo/bar/app")], @root["app"].paths end test "creating a root level path with options" do @@ -191,6 +192,7 @@ class PathsTest < ActiveSupport::TestCase @root["app"] = "/app" @root["app"].glob = "*.rb" assert_equal "*.rb", @root["app"].glob + assert_equal [Pathname.new("/app")], @root["app"].paths end test "it should be possible to override a path's default glob without assignment" do |