From d82f73ecabe71fc3814eff0bd26f4f431f690266 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Tue, 30 Nov 2004 18:52:20 +0000 Subject: Abolished ActionController::Base.require_or_load in favor of require_dependency and ActiveRecord::Base.require_or_load in favor of require_association. Both methods are now also available in the global namespace when you need to require dependencies or associations outside of whats done automatically. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@32 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/lib/action_controller/base.rb | 4 +++- actionpack/lib/action_controller/dependencies.rb | 2 +- actionpack/lib/action_controller/helpers.rb | 2 +- activerecord/lib/active_record/associations.rb | 2 +- activerecord/lib/active_record/base.rb | 14 +++++++++++--- railties/environments/shared.rb | 2 +- railties/environments/shared_for_gem.rb | 2 +- railties/lib/dispatcher.rb | 4 ++-- 8 files changed, 21 insertions(+), 11 deletions(-) diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index 22eb7dc0a3..1f4798b2a0 100755 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -256,9 +256,11 @@ module ActionController #:nodoc: end # Loads the file_name if reload_dependencies is true or requires if it's false. - def require_or_load(file_name) + def require_dependency(file_name) reload_dependencies ? silence_warnings { load("#{file_name}.rb") } : require(file_name) end + + Object.send(:define_method, :require_dependency) { |file_name| ActiveRecord::Base.require_dependency(file_name) } end public diff --git a/actionpack/lib/action_controller/dependencies.rb b/actionpack/lib/action_controller/dependencies.rb index 6f092500d1..f087354c63 100644 --- a/actionpack/lib/action_controller/dependencies.rb +++ b/actionpack/lib/action_controller/dependencies.rb @@ -38,7 +38,7 @@ module ActionController #:nodoc: def require_dependencies(layer, dependencies) dependencies.flatten.each do |dependency| begin - require_or_load(dependency.to_s) + require_dependency(dependency.to_s) rescue LoadError raise LoadError, "Missing #{layer} #{dependency}.rb" end diff --git a/actionpack/lib/action_controller/helpers.rb b/actionpack/lib/action_controller/helpers.rb index 9c88582288..3def790cc6 100644 --- a/actionpack/lib/action_controller/helpers.rb +++ b/actionpack/lib/action_controller/helpers.rb @@ -54,7 +54,7 @@ module ActionController #:nodoc: file_name = Inflector.underscore(arg.to_s.downcase) + '_helper' class_name = Inflector.camelize(file_name) begin - require_or_load(file_name) + require_dependency(file_name) rescue LoadError raise LoadError, "Missing helper file helpers/#{file_name}.rb" end diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 6e9dd3ac47..b015699fba 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -562,7 +562,7 @@ module ActiveRecord def require_association_class(class_name) begin - require_or_load(Inflector.underscore(class_name)) + require_association(Inflector.underscore(class_name)) rescue LoadError # Failed to load the associated class -- let's hope the developer is doing the requiring himself. end diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 7238f97067..6d3b07a435 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -546,13 +546,15 @@ module ActiveRecord #:nodoc: end # Loads the file_name if reload_associations is true or requires if it's false. - def require_or_load(file_name) + def require_association(file_name) if !associations_loaded.include?(file_name) associations_loaded << file_name reload_associations ? silence_warnings { load("#{file_name}.rb") } : require(file_name) end end + Object.send(:define_method, :require_association) { |file_name| ActiveRecord::Base.require_association(file_name) } + # Resets the list of dependencies loaded (typically to be called by the end of a request), so when require_or_load is # called for that dependency it'll be loaded anew. def reset_associations_loaded @@ -782,10 +784,16 @@ module ActiveRecord #:nodoc: self.class.column_methods_hash[method.to_sym] || respond_to_without_attributes?(method) end - def require_or_load(file_name) - self.class.require_or_load(file_name) + # Loads the file_name if reload_associations is true or requires if it's false. + def require_association(file_name) + if !associations_loaded.include?(file_name) + associations_loaded << file_name + reload_associations ? silence_warnings { load("#{file_name}.rb") } : require(file_name) + end end + Object.send(:define_method, :require_association) { |file_name| ActiveRecord::Base.require_association(file_name) } + private def create_or_update if new_record? then create else update end diff --git a/railties/environments/shared.rb b/railties/environments/shared.rb index 0e30f1368b..f47586ebcc 100644 --- a/railties/environments/shared.rb +++ b/railties/environments/shared.rb @@ -35,7 +35,7 @@ require 'action_mailer' # Environment-specific configuration. -ActionController::Base.require_or_load "environments/#{RAILS_ENV}" +require_dependency "environments/#{RAILS_ENV}" ActiveRecord::Base.configurations = YAML::load(File.open("#{RAILS_ROOT}/config/database.yml")) ActiveRecord::Base.establish_connection diff --git a/railties/environments/shared_for_gem.rb b/railties/environments/shared_for_gem.rb index 0316522258..c8aa36a505 100644 --- a/railties/environments/shared_for_gem.rb +++ b/railties/environments/shared_for_gem.rb @@ -32,7 +32,7 @@ require_gem 'rails' # Environment-specific configuration. -ActionController::Base.require_or_load "environments/#{RAILS_ENV}" +require_dependency "environments/#{RAILS_ENV}" ActiveRecord::Base.configurations = YAML::load(File.open("#{RAILS_ROOT}/config/database.yml")) ActiveRecord::Base.establish_connection diff --git a/railties/lib/dispatcher.rb b/railties/lib/dispatcher.rb index c765a79443..4130f009f8 100644 --- a/railties/lib/dispatcher.rb +++ b/railties/lib/dispatcher.rb @@ -31,8 +31,8 @@ class Dispatcher controller_name, module_name = controller_name(request.parameters), module_name(request.parameters) - ActionController::Base.require_or_load("abstract_application") - ActionController::Base.require_or_load(controller_path(controller_name, module_name)) + require_dependency("abstract_application") + require_dependency(controller_path(controller_name, module_name)) controller_class(controller_name).process(request, response).out rescue Object => e -- cgit v1.2.3