From ad9cabd77c26cf58c7179a19caa9efb7e5e0c831 Mon Sep 17 00:00:00 2001 From: Nicholas Seckar Date: Fri, 3 Feb 2006 20:29:39 +0000 Subject: Remove LoadingModule git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3526 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/lib/action_controller/routing.rb | 26 +++-- activesupport/CHANGELOG | 2 + activesupport/lib/active_support/dependencies.rb | 130 --------------------- .../test/loading_module/admin/access_controller.rb | 2 - .../loading_module/admin/content_controller.rb | 3 - .../test/loading_module/admin/user_controller.rb | 2 - .../test/loading_module/content_controller.rb | 3 - .../test/loading_module/resource_controller.rb | 2 - .../list/list_controller.rb | 2 - activesupport/test/loading_module_tests.rb | 124 -------------------- railties/lib/dispatcher.rb | 3 +- railties/lib/initializer.rb | 4 +- 12 files changed, 25 insertions(+), 278 deletions(-) delete mode 100644 activesupport/test/loading_module/admin/access_controller.rb delete mode 100644 activesupport/test/loading_module/admin/content_controller.rb delete mode 100644 activesupport/test/loading_module/admin/user_controller.rb delete mode 100644 activesupport/test/loading_module/content_controller.rb delete mode 100644 activesupport/test/loading_module/resource_controller.rb delete mode 100644 activesupport/test/loading_module_components/list/list_controller.rb delete mode 100644 activesupport/test/loading_module_tests.rb diff --git a/actionpack/lib/action_controller/routing.rb b/actionpack/lib/action_controller/routing.rb index f6489cdb14..7693cd0711 100644 --- a/actionpack/lib/action_controller/routing.rb +++ b/actionpack/lib/action_controller/routing.rb @@ -215,12 +215,12 @@ module ActionController class << self def assign_controller(g, controller) - expr = "::Controllers::#{controller.split('/').collect {|c| c.camelize}.join('::')}Controller" + expr = "::#{controller.split('/').collect {|c| c.camelize}.join('::')}Controller" g.result :controller, expr, true end def traverse_to_controller(segments, start_at = 0) - mod = ::Controllers + mod = ::Object length = segments.length index = start_at mod_name = controller_name = segment = nil @@ -228,13 +228,25 @@ module ActionController while index < length return nil unless /^[A-Za-z][A-Za-z\d_]*$/ =~ (segment = segments[index]) index += 1 - + mod_name = segment.camelize controller_name = "#{mod_name}Controller" - - return eval("mod::#{controller_name}", nil, 'routing.rb', __LINE__), (index - start_at) if mod.const_available?(controller_name) - return nil unless mod.const_available?(mod_name) - mod = eval("mod::#{mod_name}", nil, 'routing.rb', __LINE__) + + suppress(NameError) do + controller = mod.const_get(controller_name) + # Detect the case when const_get returns an object from a parent namespace. + if mod == Object || controller.name == "#{mod.name}::#{controller_name}" + return controller, (index - start_at) + end + end + + mod = suppress(NameError) do + next_mod = mod.const_get(mod_name) + # Check that we didn't get a module from a parent namespace + (mod == Object || next_mod.name == "#{mod.name}::#{controller_name}") ? next_mod : nil + end + + raise RoutingError, "Cannot find controller: Dropped out at #{segments[start_at..index] * '/'}" unless mod end end end diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index bd001f3196..89ec622abc 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Remove LoadingModule. [Nicholas Seckar] + * Add documentation for Reloadable::OnlySubclasses. [Nicholas Seckar] * Add Reloadable::OnlySubclasses which handles the common case where a base class should not be reloaded, but its subclasses should be. [Nicholas Seckar] diff --git a/activesupport/lib/active_support/dependencies.rb b/activesupport/lib/active_support/dependencies.rb index 0bedbf093c..6d5adb6605 100644 --- a/activesupport/lib/active_support/dependencies.rb +++ b/activesupport/lib/active_support/dependencies.rb @@ -68,136 +68,6 @@ module Dependencies #:nodoc: # Record history *after* loading so first load gets warnings. history << file_name end - - # LoadingModules implement namespace-safe dynamic loading. - # They support automatic loading via const_missing, allowing contained items to be automatically - # loaded when required. No extra syntax is required, as expressions such as Controller::Admin::UserController - # load the relavent files automatically. - # - # Ruby-style modules are supported, as a folder named 'submodule' will load 'submodule.rb' when available. - class LoadingModule < Module #:nodoc: - attr_reader :path - attr_reader :root - - class << self - def root(*load_paths) - RootLoadingModule.new(*load_paths) - end - end - - def initialize(root, path=[]) - @path = path.clone.freeze - @root = root - end - - def root?() self.root == self end - def load_paths() self.root.load_paths end - - # Load missing constants if possible. - def const_missing(name) - const_load!(name) ? const_get(name) : super(name) - end - - # Load the controller class or a parent module. - def const_load!(name, file_name = nil) - file_name ||= 'application' if root? && name.to_s == 'ApplicationController' - path = self.path + [file_name || name] - - load_paths.each do |load_path| - fs_path = load_path.filesystem_path(path) - next unless fs_path - - case - when File.directory?(fs_path) - new_module = LoadingModule.new(self.root, self.path + [name]) - self.const_set name, new_module - if self.root? - if Object.const_defined?(name) - msg = "Cannot load module #{name}: Object::#{name} is set to #{Object.const_get(name).inspect}" - raise NameError, msg - end - Object.const_set(name, new_module) - end - break - when File.file?(fs_path) - loaded_file = self.root.load_file!(fs_path) - - # Import the loaded constant from Object provided we are the root node. - self.const_set(name, Object.const_get(name)) if self.root? && Object.const_defined?(name) - - # Throw an error if we load the file but we don't find the Object we expect - if loaded_file and not self.const_defined?(name) - msg = "Already loaded file '#{fs_path}' but '#{name.to_s}' was not set, perhaps you need to rename '#{fs_path}'?" - raise LoadError, msg - end - break - end - end - - self.const_defined?(name) - end - - # Is this name present or loadable? - # This method is used by Routes to find valid controllers. - def const_available?(name) - self.const_defined?(name) || load_paths.any? {|lp| lp.filesystem_path(path + [name])} - end - - # Erase all items in this module - def clear! - constants.each do |name| - Object.send(:remove_const, name) if Object.const_defined?(name) && Object.const_get(name).object_id == self.const_get(name).object_id - self.send(:remove_const, name) - end - end - end - - class RootLoadingModule < LoadingModule #:nodoc: - attr_reader :load_paths - - def initialize(*paths) - @load_paths = paths.flatten.collect {|p| p.kind_of?(ConstantLoadPath) ? p : ConstantLoadPath.new(p)} - end - - def root() self end - - def path() [] end - - # Load the source file at the given file path - def load_file!(file_path) - require_dependency(file_path) - end - end - - # This object defines a path from which Constants can be loaded. - class ConstantLoadPath #:nodoc: - # Create a new load path with the filesystem path - def initialize(root) @root = root end - - # Return nil if the path does not exist, or the path to a directory - # if the path leads to a module, or the path to a file if it leads to an object. - def filesystem_path(path, allow_module=true) - fs_path = [@root] - fs_path += path[0..-2].map {|name| const_name_to_module_name name} - - if allow_module - result = File.join(fs_path, const_name_to_module_name(path.last)) - return result if File.directory? result # Return the module path if one exists - end - - result = File.join(fs_path, const_name_to_file_name(path.last)) - - File.file?(result) ? result : nil - end - - def const_name_to_file_name(name) - name.to_s.underscore + '.rb' - end - - def const_name_to_module_name(name) - name.to_s.underscore - end - end end Object.send(:define_method, :require_or_load) { |file_name| Dependencies.require_or_load(file_name) } unless Object.respond_to?(:require_or_load) diff --git a/activesupport/test/loading_module/admin/access_controller.rb b/activesupport/test/loading_module/admin/access_controller.rb deleted file mode 100644 index ddcbda8132..0000000000 --- a/activesupport/test/loading_module/admin/access_controller.rb +++ /dev/null @@ -1,2 +0,0 @@ -class Admin::AccessController -end diff --git a/activesupport/test/loading_module/admin/content_controller.rb b/activesupport/test/loading_module/admin/content_controller.rb deleted file mode 100644 index 454dbac690..0000000000 --- a/activesupport/test/loading_module/admin/content_controller.rb +++ /dev/null @@ -1,3 +0,0 @@ -class Admin::ContentController - def identifier() :inner end -end diff --git a/activesupport/test/loading_module/admin/user_controller.rb b/activesupport/test/loading_module/admin/user_controller.rb deleted file mode 100644 index f265f1597a..0000000000 --- a/activesupport/test/loading_module/admin/user_controller.rb +++ /dev/null @@ -1,2 +0,0 @@ -class Admin::UserController -end diff --git a/activesupport/test/loading_module/content_controller.rb b/activesupport/test/loading_module/content_controller.rb deleted file mode 100644 index 78317b0518..0000000000 --- a/activesupport/test/loading_module/content_controller.rb +++ /dev/null @@ -1,3 +0,0 @@ -class ContentController - def identifier() :outer end -end diff --git a/activesupport/test/loading_module/resource_controller.rb b/activesupport/test/loading_module/resource_controller.rb deleted file mode 100644 index d948f366bf..0000000000 --- a/activesupport/test/loading_module/resource_controller.rb +++ /dev/null @@ -1,2 +0,0 @@ -class ResourceController -end diff --git a/activesupport/test/loading_module_components/list/list_controller.rb b/activesupport/test/loading_module_components/list/list_controller.rb deleted file mode 100644 index 415db868b2..0000000000 --- a/activesupport/test/loading_module_components/list/list_controller.rb +++ /dev/null @@ -1,2 +0,0 @@ -class List::ListController -end \ No newline at end of file diff --git a/activesupport/test/loading_module_tests.rb b/activesupport/test/loading_module_tests.rb deleted file mode 100644 index 22fc00db7e..0000000000 --- a/activesupport/test/loading_module_tests.rb +++ /dev/null @@ -1,124 +0,0 @@ -require 'test/unit' -require File.dirname(__FILE__) + '/../lib/active_support/core_ext.rb' -require File.dirname(__FILE__) + '/../lib/active_support/dependencies.rb' - -STAGING_DIRECTORY = File.join(File.dirname(__FILE__), 'loading_module') -COMPONENTS_DIRECTORY = File.join(File.dirname(__FILE__), 'loading_module_components') - -class LoadingModuleTests < Test::Unit::TestCase - def setup - @loading_module = Dependencies::LoadingModule.root(STAGING_DIRECTORY) - Object.send(:remove_const, :Controllers) if Object.const_defined?(:Controllers) - Object.const_set(:Controllers, @loading_module) - end - def teardown - Object.send :remove_const, :Controllers - @loading_module.clear! - Dependencies.clear - end - - def test_setup - assert_kind_of Dependencies::LoadingModule, @loading_module - end - - def test_const_available - assert @loading_module.const_available?(:Admin) - assert @loading_module.const_available?(:ResourceController) - assert @loading_module.const_available?(:ContentController) - assert @loading_module.const_available?("ContentController") - - assert_equal false, @loading_module.const_available?(:AdminController) - assert_equal false, @loading_module.const_available?(:RandomName) - end - - def test_nested_const_available - assert @loading_module::Admin.const_available?(:AccessController) - assert @loading_module::Admin.const_available?(:UserController) - assert @loading_module::Admin.const_available?(:ContentController) - assert ! @loading_module::Admin.const_available?(:ResourceController) - end - - def test_nested_module_export - @loading_module::Admin - assert_equal @loading_module::Admin.object_id, Object::Admin.object_id - assert_equal @loading_module::Admin.object_id, Controllers::Admin.object_id - end - - def test_const_load_module - assert @loading_module.const_load!(:Admin) - assert_kind_of Module, @loading_module::Admin - assert_kind_of Dependencies::LoadingModule, @loading_module::Admin - end - - def test_const_load_controller - assert @loading_module.const_load!(:ContentController) - assert_kind_of Class, @loading_module::ContentController - end - - def test_const_load_nested_controller - assert @loading_module.const_load!(:Admin) - assert_kind_of Dependencies::LoadingModule, @loading_module::Admin - assert @loading_module::Admin.const_available?(:UserController) - assert_kind_of Class, @loading_module::Admin::UserController - end - - def test_pretty_access - assert_kind_of Module, @loading_module::Admin - assert_kind_of Dependencies::LoadingModule, @loading_module::Admin - - assert_kind_of Class, @loading_module::Admin::UserController - assert_kind_of Class, @loading_module::Admin::AccessController - assert_kind_of Class, @loading_module::ResourceController - assert_kind_of Class, @loading_module::ContentController - end - - def test_missing_name - assert_raises(NameError) {@loading_module::PersonController} - assert_raises(NameError) {@loading_module::Admin::FishController} - end - - def test_name_clash - assert ! @loading_module::const_defined?(:ContentController) - assert_equal :outer, @loading_module::ContentController.new.identifier - assert ! @loading_module::Admin.const_defined?(:ContentController) - assert_equal :inner, @loading_module::Admin::ContentController.new.identifier - assert @loading_module::ContentController.object_id != @loading_module::Admin::ContentController.object_id - end - - def test_name_clash_other_way - assert ! @loading_module::Admin.const_defined?(:ContentController) - assert_equal :inner, @loading_module::Admin::ContentController.new.identifier - assert ! @loading_module::const_defined?(:ContentController) - assert_equal :outer, @loading_module::ContentController.new.identifier - assert @loading_module::ContentController.object_id != @loading_module::Admin::ContentController.object_id - end - - def test_access_to_controller_with_numbers - assert @loading_module.const_available?(:Area51Controller) - assert_not_nil @loading_module::Area51Controller - end -end - -class LoadingModuleMultiPathTests < Test::Unit::TestCase - def setup - @loading_module = Dependencies::LoadingModule.root(STAGING_DIRECTORY, COMPONENTS_DIRECTORY) - Object.const_set(:Controllers, @loading_module) - end - def teardown - Object.send :remove_const, :Controllers - @loading_module.clear! - Dependencies.clear - end - - def test_access_from_first - assert_kind_of Module, @loading_module::Admin - assert_kind_of Dependencies::LoadingModule, @loading_module::Admin - assert_kind_of Class, @loading_module::Admin::UserController - end - def test_access_from_second - assert_kind_of Module, @loading_module::List - assert_kind_of Dependencies::LoadingModule, @loading_module::List - assert @loading_module::List.const_load!(:ListController) - assert_kind_of Class, @loading_module::List::ListController - end -end diff --git a/railties/lib/dispatcher.rb b/railties/lib/dispatcher.rb index 6bc61889d1..98ae774cea 100644 --- a/railties/lib/dispatcher.rb +++ b/railties/lib/dispatcher.rb @@ -50,7 +50,6 @@ class Dispatcher # mailers, and so forth. This allows them to be loaded again without having # to restart the server (WEBrick, FastCGI, etc.). def reset_application! - Controllers.clear! Dependencies.clear ActiveRecord::Base.reset_subclasses Class.remove_class(*Reloadable.reloadable_classes) @@ -67,7 +66,7 @@ class Dispatcher def prepare_application ActionController::Routing::Routes.reload if Dependencies.load? prepare_breakpoint - Controllers.const_load!(:ApplicationController, "application") unless Controllers.const_defined?(:ApplicationController) + require_dependency('application.rb') unless Object.const_defined?(:ApplicationController) end def reset_after_dispatch diff --git a/railties/lib/initializer.rb b/railties/lib/initializer.rb index 68b14b9fe0..e1b058f3b9 100644 --- a/railties/lib/initializer.rb +++ b/railties/lib/initializer.rb @@ -225,7 +225,6 @@ module Rails def initialize_routing return unless configuration.frameworks.include?(:action_controller) ActionController::Routing::Routes.reload - Object.const_set "Controllers", Dependencies::LoadingModule.root(*configuration.controller_paths) end # Sets the dependency loading mechanism based on the value of @@ -457,6 +456,9 @@ module Rails def default_load_paths paths = ["#{root_path}/test/mocks/#{environment}"] + + # Add the app's controller directory + paths.concat(Dir["#{root_path}/app/controllers/"]) # Then model subdirectories. # TODO: Don't include .rb models as load paths -- cgit v1.2.3