Permalink
Browse files

Added that config/routes.rb files in engine plugins are automatically…

… loaded (and reloaded when they change in dev mode) [DHH]
  • Loading branch information...
1 parent 40b40c4 commit 4999d52e08a02ebba344f6c318f0af4b5b18f0e5 @dhh dhh committed Nov 26, 2008
View
@@ -1,5 +1,7 @@
*2.3.0 [Edge]*
+* Added that config/routes.rb files in engine plugins are automatically loaded (and reloaded when they change in dev mode) [DHH]
+
* Added app/[models|controllers|helpers] to the load path for plugins that has an app directory (go engines ;)) [DHH]
* Add config.preload_frameworks to load all frameworks at startup. Default to false so Rails autoloads itself as it's used. Turn this on for Passenger and JRuby. Also turned on by config.threadsafe! [Jeremy Kemper]
@@ -486,8 +486,13 @@ def initialize_framework_views
# loading module used to lazily load controllers (Configuration#controller_paths).
def initialize_routing
return unless configuration.frameworks.include?(:action_controller)
- ActionController::Routing.controller_paths = configuration.controller_paths
- ActionController::Routing::Routes.configuration_file = configuration.routes_configuration_file
+
+ ActionController::Routing.controller_paths = configuration.controller_paths + plugin_loader.controller_paths
+
+ ([ configuration.routes_configuration_file ] + plugin_loader.routing_files).each do |routing_file|
+ ActionController::Routing::Routes.add_configuration_file(routing_file)
+ end
+
ActionController::Routing::Routes.reload
end
@@ -40,7 +40,7 @@ def load_paths
load_paths << app_paths if has_app_directory?
end.flatten
end
-
+
# Evaluates a plugin's init.rb file.
def load(initializer)
return if loaded?
@@ -60,7 +60,26 @@ def <=>(other_plugin)
def about
@about ||= load_about_information
end
+
+ # Engines are plugins with an app/ directory.
+ def engine?
+ has_app_directory?
+ end
+ # Returns true if the engine ships with a routing file
+ def routed?
+ File.exist?(routing_file)
+ end
+
+ def controller_path
+ File.join(directory, 'app', 'controllers')
+ end
+
+ def routing_file
+ File.join(directory, 'config', 'routes.rb')
+ end
+
+
private
def load_about_information
about_yml_path = File.join(@directory, "about.yml")
@@ -82,7 +101,7 @@ def app_paths
File.join(directory, 'app', 'helpers')
]
end
-
+
def lib_path
File.join(directory, 'lib')
end
@@ -22,6 +22,11 @@ def plugins
@plugins ||= all_plugins.select { |plugin| should_load?(plugin) }.sort { |p1, p2| order_plugins(p1, p2) }
end
+ # Returns the plugins that are in engine-form (have an app/ directory)
+ def engines
+ @engines ||= plugins.select(&:engine?)
+ end
+
# Returns all the plugins that could be found by the current locators.
def all_plugins
@all_plugins ||= locate_plugins
@@ -56,7 +61,17 @@ def add_plugin_load_paths
end
$LOAD_PATH.uniq!
- end
+ end
+
+ # Returns an array of all the controller paths found inside engine-type plugins.
+ def controller_paths
+ engines.collect(&:controller_path)
+ end
+
+ def routing_files
+ engines.select(&:routed?).collect(&:routing_file)
+ end
+
protected
@@ -0,0 +1,2 @@
+class EngineController < ActionController::Base
+end
@@ -0,0 +1,2 @@
+class EngineModel
+end
@@ -0,0 +1,3 @@
+ActionController::Routing::Routes.draw do |map|
+ map.connect '/engine', :controller => "engine"
+end
@@ -0,0 +1,3 @@
+# My app/models dir must be in the load path.
+require 'engine_model'
+raise 'missing model from my app/models dir' unless defined?(EngineModel)
@@ -252,6 +252,7 @@ def test_should_ensure_all_loaded_plugins_load_paths_are_added_to_the_load_path
assert $LOAD_PATH.include?(File.join(plugin_fixture_path('default/acts/acts_as_chunky_bacon'), 'lib'))
end
+
private
def load_plugins!
@@ -47,7 +47,7 @@ def test_should_return_nil_when_calling_create_plugin_with_an_invalid_plugin_dir
end
def test_should_return_all_plugins_found_under_the_set_plugin_paths
- assert_equal ["a", "acts_as_chunky_bacon", "gemlike", "plugin_with_no_lib_dir", "stubby"].sort, @locator.plugins.map(&:name).sort
+ assert_equal ["a", "acts_as_chunky_bacon", "engine", "gemlike", "plugin_with_no_lib_dir", "stubby"].sort, @locator.plugins.map(&:name).sort
end
def test_should_find_plugins_only_under_the_plugin_paths_set_in_configuration

1 comment on commit 4999d52

moklett commented on 4999d52 Feb 4, 2009

So, engines routes are currently placed before main app routes… is thought being given to a way of inserting engine routes at other points in the route set order?

Please sign in to comment.