aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2004-12-20 13:21:01 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2004-12-20 13:21:01 +0000
commit8537d5eb93dae418c5c866b313ca1362990957ca (patch)
treedb6f7bce54f3e3ab370f665e9d950d5f8f088e4e /activerecord/lib
parentef489ca2ad9f3dd65563803a95d99cc53b2a5a0a (diff)
downloadrails-8537d5eb93dae418c5c866b313ca1362990957ca.tar.gz
rails-8537d5eb93dae418c5c866b313ca1362990957ca.tar.bz2
rails-8537d5eb93dae418c5c866b313ca1362990957ca.zip
Added a require_association hook on const_missing that makes it possible to use any model class without requiring it first. Added that Active Record associations are now reloaded instead of cleared to work with the new const_missing hook in Active Record.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@233 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib')
-rwxr-xr-xactiverecord/lib/active_record/associations.rb29
1 files changed, 27 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index ba7f34d729..a66a6c9a5c 100755
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -3,8 +3,28 @@ require 'active_record/associations/has_many_association'
require 'active_record/associations/has_and_belongs_to_many_association'
require 'active_record/deprecated_associations'
-unless Object.respond_to?(:require_association)
- Object.send(:define_method, :require_association) { |file_name| ActiveRecord::Base.require_association(file_name) }
+
+class << Object #:nodoc:
+ # Make require_association available as a bare method.
+ unless respond_to?(:require_association)
+ def require_association(file_name)
+ ActiveRecord::Base.require_association(file_name)
+ end
+ end
+
+ # Use const_missing to autoload associations so we don't have to
+ # require_association when using single-table inheritance.
+ unless respond_to?(:pre_association_const_missing)
+ alias_method :pre_association_const_missing, :const_missing
+
+ def const_missing(class_id)
+ begin
+ require_association(Inflect.underscore(Inflector.demodulize(class_id.to_s)))
+ rescue LoadError
+ pre_association_const_missing(class_id)
+ end
+ end
+ end
end
module ActiveRecord
@@ -464,6 +484,11 @@ module ActiveRecord
def reset_associations_loaded
self.associations_loaded = []
end
+
+ # Reload all the associations that have already been loaded once.
+ def reload_associations_loaded
+ associations_loaded.each { |file_name| silence_warnings { load("#{file_name}.rb") } }
+ end
private
# Raises an exception if an invalid option has been specified to prevent misspellings from slipping through