提交 e8f0b1dd 编写于 作者: R Rick Olson

Refactor ActiveRecord::Base.reset_subclasses to #reset, and add global...

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
上级 a7f15864
*SVN* *SVN*
* Refactor ActiveRecord::Base.reset_subclasses to #reset, and add global observer resetting. [Rick Olson]
* Formally deprecate the deprecated finders. [Koz] * Formally deprecate the deprecated finders. [Koz]
* Formally deprecate rich associations. [Koz] * Formally deprecate rich associations. [Koz]
......
...@@ -272,6 +272,10 @@ def self.inherited(child) #:nodoc: ...@@ -272,6 +272,10 @@ def self.inherited(child) #:nodoc:
super super
end end
def self.reset
reset_subclasses
end
def self.reset_subclasses #:nodoc: def self.reset_subclasses #:nodoc:
nonreloadables = [] nonreloadables = []
subclasses.each do |klass| subclasses.each do |klass|
......
...@@ -4,10 +4,18 @@ ...@@ -4,10 +4,18 @@
module ActiveRecord module ActiveRecord
module Observing # :nodoc: module Observing # :nodoc:
def self.included(base) def self.included(base)
base.extend(ClassMethods) class << base
include ClassMethods
alias_method_chain :reset, :observers
end
end end
module ClassMethods module ClassMethods
def reset_with_observers # :nodoc:
reset_without_observers
instantiate_observers
end
# Activates the observers assigned. Examples: # Activates the observers assigned. Examples:
# #
# # Calls PersonObserver.instance # # Calls PersonObserver.instance
...@@ -19,7 +27,13 @@ module ClassMethods ...@@ -19,7 +27,13 @@ module ClassMethods
# # Same as above, just using explicit class references # # Same as above, just using explicit class references
# ActiveRecord::Base.observers = Cacher, GarbageCollector # ActiveRecord::Base.observers = Cacher, GarbageCollector
def observers=(*observers) 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 if observer.respond_to?(:to_sym) # Symbol or String
observer.to_s.camelize.constantize.instance observer.to_s.camelize.constantize.instance
elsif observer.respond_to?(:instance) elsif observer.respond_to?(:instance)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册