Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added a require_association hook on const_missing that makes it possi…

…ble 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
  • Loading branch information...
commit 8537d5eb93dae418c5c866b313ca1362990957ca 1 parent ef489ca
@dhh dhh authored
View
13 activerecord/CHANGELOG
@@ -1,5 +1,18 @@
*SVN*
+* Added a require_association hook on const_missing that makes it possible to use any model class without requiring it first. This makes STI look like:
+
+ before:
+ require_association 'person'
+ class Employee < Person
+ end
+
+ after:
+ class Employee < Person
+ end
+
+ This also reduces the usefulness of Controller.model in Action Pack to currently only being for documentation purposes.
+
* Added that Base.update_all and Base.delete_all return an integer of the number of affected rows #341
* Changed the interface on AbstractAdapter to require that adapters return the number of affected rows on delete and update operations.
View
29 activerecord/lib/active_record/associations.rb
@@ -3,8 +3,28 @@
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 @@ def require_association(file_name)
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
View
2  railties/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Added that Active Record associations are now reloaded instead of cleared to work with the new const_missing hook in Active Record.
+
* Added graceful handling of an inaccessible log file by redirecting output to STDERR with a warning #330 [rainmkr]
* Added support for a -h/--help parameter in the generator #331 [Ulysses]
View
2  railties/lib/dispatcher.rb
@@ -43,7 +43,7 @@ def self.dispatch(cgi = CGI.new, session_options = ActionController::CgiRequest:
if ActionController::Base.reload_dependencies
Object.send(:remove_const, "ApplicationController") if Object.const_defined?(:ApplicationController)
Object.send(:remove_const, controller_class_name(controller_name)) if Object.const_defined?(controller_class_name(controller_name))
- ActiveRecord::Base.reset_associations_loaded
+ ActiveRecord::Base.reload_associations_loaded
ActiveRecord::Base.reset_column_information_and_inheritable_attributes_for_all_subclasses
end
Please sign in to comment.
Something went wrong with that request. Please try again.