Skip to content

Commit

Permalink
Made the location of the routes file configurable with config.routes_…
Browse files Browse the repository at this point in the history
…configuration_file (Scott Fleckenstein) [#88 state:resolved]
  • Loading branch information
dhh committed May 1, 2008
1 parent e931394 commit 926f464
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
12 changes: 6 additions & 6 deletions actionpack/lib/action_controller/routing/route_set.rb
Expand Up @@ -189,7 +189,7 @@ def #{selector}(*args)
end end
end end


attr_accessor :routes, :named_routes attr_accessor :routes, :named_routes, :configuration_file


def initialize def initialize
self.routes = [] self.routes = []
Expand Down Expand Up @@ -238,8 +238,8 @@ def load!
alias reload! load! alias reload! load!


def reload def reload
if @routes_last_modified && defined?(RAILS_ROOT) if @routes_last_modified && configuration_file
mtime = File.stat("#{RAILS_ROOT}/config/routes.rb").mtime mtime = File.stat(configuration_file).mtime
# if it hasn't been changed, then just return # if it hasn't been changed, then just return
return if mtime == @routes_last_modified return if mtime == @routes_last_modified
# if it has changed then record the new time and fall to the load! below # if it has changed then record the new time and fall to the load! below
Expand All @@ -249,9 +249,9 @@ def reload
end end


def load_routes! def load_routes!
if defined?(RAILS_ROOT) && defined?(::ActionController::Routing::Routes) && self == ::ActionController::Routing::Routes if configuration_file
load File.join("#{RAILS_ROOT}/config/routes.rb") load configuration_file
@routes_last_modified = File.stat("#{RAILS_ROOT}/config/routes.rb").mtime @routes_last_modified = File.stat(configuration_file).mtime
else else
add_route ":controller/:action/:id" add_route ":controller/:action/:id"
end end
Expand Down
10 changes: 10 additions & 0 deletions actionpack/test/controller/routing_test.rb
Expand Up @@ -2341,11 +2341,13 @@ class RouteLoadingTest < Test::Unit::TestCase
def setup def setup
routes.instance_variable_set '@routes_last_modified', nil routes.instance_variable_set '@routes_last_modified', nil
silence_warnings { Object.const_set :RAILS_ROOT, '.' } silence_warnings { Object.const_set :RAILS_ROOT, '.' }
ActionController::Routing::Routes.configuration_file = File.join(RAILS_ROOT, 'config', 'routes.rb')


@stat = stub_everything @stat = stub_everything
end end


def teardown def teardown
ActionController::Routing::Routes.configuration_file = nil
Object.send :remove_const, :RAILS_ROOT Object.send :remove_const, :RAILS_ROOT
end end


Expand Down Expand Up @@ -2386,6 +2388,14 @@ def test_adding_inflections_forces_reload


Inflector.inflections { |inflect| inflect.uncountable('equipment') } Inflector.inflections { |inflect| inflect.uncountable('equipment') }
end end

def test_load_with_configuration
routes.configuration_file = "foobarbaz"
File.expects(:stat).returns(@stat)
routes.expects(:load).with("foobarbaz")

routes.reload
end


private private
def routes def routes
Expand Down
2 changes: 2 additions & 0 deletions railties/CHANGELOG
@@ -1,5 +1,7 @@
*SVN* *SVN*


* Made the location of the routes file configurable with config.routes_configuration_file (Scott Fleckenstein) [#88]

* Rails Edge info returns the latest git commit hash [Francesc Esplugas] * Rails Edge info returns the latest git commit hash [Francesc Esplugas]


* Added Rails.public_path to control where HTML and assets are expected to be loaded from (defaults to Rails.root + "/public") #11581 [nicksieger] * Added Rails.public_path to control where HTML and assets are expected to be loaded from (defaults to Rails.root + "/public") #11581 [nicksieger]
Expand Down
10 changes: 10 additions & 0 deletions railties/lib/initializer.rb
Expand Up @@ -386,6 +386,7 @@ def initialize_framework_views
def initialize_routing def initialize_routing
return unless configuration.frameworks.include?(:action_controller) return unless configuration.frameworks.include?(:action_controller)
ActionController::Routing.controller_paths = configuration.controller_paths ActionController::Routing.controller_paths = configuration.controller_paths
ActionController::Routing::Routes.configuration_file = configuration.routes_configuration_file
ActionController::Routing::Routes.reload ActionController::Routing::Routes.reload
end end


Expand Down Expand Up @@ -503,6 +504,10 @@ class Configuration
# The path to the database configuration file to use. (Defaults to # The path to the database configuration file to use. (Defaults to
# <tt>config/database.yml</tt>.) # <tt>config/database.yml</tt>.)
attr_accessor :database_configuration_file attr_accessor :database_configuration_file

# The path to the routes configuration file to use. (Defaults to
# <tt>config/routes.rb</tt>.)
attr_accessor :routes_configuration_file


# The list of rails framework components that should be loaded. (Defaults # The list of rails framework components that should be loaded. (Defaults
# to <tt>:active_record</tt>, <tt>:action_controller</tt>, # to <tt>:active_record</tt>, <tt>:action_controller</tt>,
Expand Down Expand Up @@ -635,6 +640,7 @@ def initialize
self.plugin_locators = default_plugin_locators self.plugin_locators = default_plugin_locators
self.plugin_loader = default_plugin_loader self.plugin_loader = default_plugin_loader
self.database_configuration_file = default_database_configuration_file self.database_configuration_file = default_database_configuration_file
self.routes_configuration_file = default_routes_configuration_file
self.gems = default_gems self.gems = default_gems


for framework in default_frameworks for framework in default_frameworks
Expand Down Expand Up @@ -775,6 +781,10 @@ def default_database_configuration_file
File.join(root_path, 'config', 'database.yml') File.join(root_path, 'config', 'database.yml')
end end


def default_routes_configuration_file
File.join(root_path, 'config', 'routes.rb')
end

def default_view_path def default_view_path
File.join(root_path, 'app', 'views') File.join(root_path, 'app', 'views')
end end
Expand Down

0 comments on commit 926f464

Please sign in to comment.