Refactor ActiveRecord::Base.reset_subclasses to #reset, and add global observer resetting. [Rick Olson]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4683 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Rick Olson
2006-08-06 02:08:29 +00:00
parent a7f1586404
commit e8f0b1ddd9
3 changed files with 22 additions and 2 deletions

View File

@@ -1,5 +1,7 @@
*SVN*
* Refactor ActiveRecord::Base.reset_subclasses to #reset, and add global observer resetting. [Rick Olson]
* Formally deprecate the deprecated finders. [Koz]
* Formally deprecate rich associations. [Koz]

View File

@@ -272,6 +272,10 @@ module ActiveRecord #:nodoc:
super
end
def self.reset
reset_subclasses
end
def self.reset_subclasses #:nodoc:
nonreloadables = []
subclasses.each do |klass|

View File

@@ -4,10 +4,18 @@ require 'set'
module ActiveRecord
module Observing # :nodoc:
def self.included(base)
base.extend(ClassMethods)
class << base
include ClassMethods
alias_method_chain :reset, :observers
end
end
module ClassMethods
def reset_with_observers # :nodoc:
reset_without_observers
instantiate_observers
end
# Activates the observers assigned. Examples:
#
# # Calls PersonObserver.instance
@@ -19,7 +27,13 @@ module ActiveRecord
# # Same as above, just using explicit class references
# ActiveRecord::Base.observers = Cacher, GarbageCollector
def observers=(*observers)
observers.flatten.each do |observer|
@observers = observers.flatten
end
# Instantiate the global ActiveRecord observers
def instantiate_observers
return if @observers.blank?
@observers.each do |observer|
if observer.respond_to?(:to_sym) # Symbol or String
observer.to_s.camelize.constantize.instance
elsif observer.respond_to?(:instance)