Created
January 15, 2011 11:06
-
-
Save brainopia/780839 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From 038eb65be7d071755288b0ab95de27e97350d8c2 Mon Sep 17 00:00:00 2001 | |
From: brainopia <[email protected]> | |
Date: Sat, 15 Jan 2011 13:49:55 +0300 | |
Subject: [PATCH 1/3] Add observer_path method to load observers matching given pattern | |
--- | |
activemodel/lib/active_model/observing.rb | 37 ++++++++++++++++++++++++++++- | |
activemodel/test/cases/observing_test.rb | 13 ++++++++++ | |
2 files changed, 49 insertions(+), 1 deletions(-) | |
diff --git a/activemodel/lib/active_model/observing.rb b/activemodel/lib/active_model/observing.rb | |
index 0d2dd36..1c8b658 100644 | |
--- a/activemodel/lib/active_model/observing.rb | |
+++ b/activemodel/lib/active_model/observing.rb | |
@@ -26,6 +26,41 @@ module ActiveModel | |
# +instantiate_observers+ is called during startup, and before | |
# each development request. | |
def observers=(*values) | |
+ @observer_path = nil | |
+ observer(*values) | |
+ end | |
+ | |
+ # Sets location for default observers | |
+ # which are going to be activated automatically. | |
+ # | |
+ # ActiveRecord::Base.observer_path 'app/models/*_observer.rb' | |
+ # | |
+ # Note: #observers= method explicitely sets observers, | |
+ # clobbering default ones. | |
+ # If you need to use custom observers along with default ones | |
+ # then use #observer_path method in combination with #observer method. | |
+ def observer_path(new_path=nil) | |
+ @observer_path = new_path || @observer_path | |
+ end | |
+ | |
+ # Return default observers matching #observer_path | |
+ def default_observers | |
+ return [] unless @observer_path | |
+ Dir[@observer_path].map do |file| | |
+ File.basename file, '.rb' | |
+ end | |
+ end | |
+ | |
+ # Activates the observers assigned. | |
+ # | |
+ # ActiveRecord::Base.observer :cacher, :garbage_collector | |
+ # | |
+ # # same as above using class references | |
+ # ActiveRecord::Base.observer Cacher, GarbageCollector | |
+ # | |
+ # Unlike #observers= writes on top of default observers | |
+ # not replacing them. | |
+ def observer(*values) | |
@observers = values.flatten | |
end | |
@@ -41,7 +76,7 @@ module ActiveModel | |
# Instantiate the global Active Record observers. | |
def instantiate_observers | |
- observers.each { |o| instantiate_observer(o) } | |
+ (observers + default_observers).each { |o| instantiate_observer(o) } | |
end | |
def add_observer(observer) | |
diff --git a/activemodel/test/cases/observing_test.rb b/activemodel/test/cases/observing_test.rb | |
index 6368684..9072913 100644 | |
--- a/activemodel/test/cases/observing_test.rb | |
+++ b/activemodel/test/cases/observing_test.rb | |
@@ -61,6 +61,19 @@ class ObservingTest < ActiveModel::TestCase | |
ObservedModel.instantiate_observers | |
end | |
+ test "instantiates default observers" do | |
+ ObservedModel.observer_path 'app/models/*_observer.rb' | |
+ Dir.expects(:[]).with('app/models/*_observer.rb').returns(['app/models/foo_observer.rb']) | |
+ FooObserver.expects(:instance) | |
+ ObservedModel.instantiate_observers | |
+ end | |
+ | |
+ test "does not instantiate default observers after explicitly setting observers" do | |
+ ObservedModel.observer_path 'app/models/*_observer.rb' | |
+ ObservedModel.observers = :bar | |
+ assert_nil ObservedModel.observer_path | |
+ end | |
+ | |
test "passes observers to subclasses" do | |
FooObserver.instance | |
bar = Class.new(Foo) | |
-- | |
1.7.3.5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From 6bce686b0328d17db92067c386a3fbd6a4b11a49 Mon Sep 17 00:00:00 2001 | |
From: brainopia <[email protected]> | |
Date: Sat, 15 Jan 2011 13:53:17 +0300 | |
Subject: [PATCH 2/3] Replace ActiveRecord references in ActiveModel::Observing comments | |
--- | |
activemodel/lib/active_model/observing.rb | 16 ++++++++++------ | |
1 files changed, 10 insertions(+), 6 deletions(-) | |
diff --git a/activemodel/lib/active_model/observing.rb b/activemodel/lib/active_model/observing.rb | |
index 1c8b658..3da06a3 100644 | |
--- a/activemodel/lib/active_model/observing.rb | |
+++ b/activemodel/lib/active_model/observing.rb | |
@@ -13,14 +13,18 @@ module ActiveModel | |
# | |
# Activates the observers assigned. Examples: | |
# | |
+ # class Model | |
+ # include ActiveModel::Observing | |
+ # end | |
+ # | |
# # Calls PersonObserver.instance | |
- # ActiveRecord::Base.observers = :person_observer | |
+ # Model.observers = :person_observer | |
# | |
# # Calls Cacher.instance and GarbageCollector.instance | |
- # ActiveRecord::Base.observers = :cacher, :garbage_collector | |
+ # Model.observers = :cacher, :garbage_collector | |
# | |
# # Same as above, just using explicit class references | |
- # ActiveRecord::Base.observers = Cacher, GarbageCollector | |
+ # Model.observers = Cacher, GarbageCollector | |
# | |
# Note: Setting this does not instantiate the observers yet. | |
# +instantiate_observers+ is called during startup, and before | |
@@ -33,7 +37,7 @@ module ActiveModel | |
# Sets location for default observers | |
# which are going to be activated automatically. | |
# | |
- # ActiveRecord::Base.observer_path 'app/models/*_observer.rb' | |
+ # Model.observer_path 'app/models/*_observer.rb' | |
# | |
# Note: #observers= method explicitely sets observers, | |
# clobbering default ones. | |
@@ -53,10 +57,10 @@ module ActiveModel | |
# Activates the observers assigned. | |
# | |
- # ActiveRecord::Base.observer :cacher, :garbage_collector | |
+ # Model.observer :cacher, :garbage_collector | |
# | |
# # same as above using class references | |
- # ActiveRecord::Base.observer Cacher, GarbageCollector | |
+ # Model.observer Cacher, GarbageCollector | |
# | |
# Unlike #observers= writes on top of default observers | |
# not replacing them. | |
-- | |
1.7.3.5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From 4d0096e9ce83333c8a4d5275fb56bc4fa86fbb69 Mon Sep 17 00:00:00 2001 | |
From: brainopia <[email protected]> | |
Date: Sat, 15 Jan 2011 14:01:26 +0300 | |
Subject: [PATCH 3/3] Update application.rb template to reflect observer changes | |
--- | |
.../rails/app/templates/config/application.rb | 5 +++-- | |
1 files changed, 3 insertions(+), 2 deletions(-) | |
diff --git a/railties/lib/rails/generators/rails/app/templates/config/application.rb b/railties/lib/rails/generators/rails/app/templates/config/application.rb | |
index 6e51575..589f8b2 100644 | |
--- a/railties/lib/rails/generators/rails/app/templates/config/application.rb | |
+++ b/railties/lib/rails/generators/rails/app/templates/config/application.rb | |
@@ -28,8 +28,9 @@ module <%= app_const_base %> | |
# :all can be used as a placeholder for all plugins not explicitly named. | |
# config.plugins = [ :exception_notification, :ssl_requirement, :all ] | |
- # Activate observers that should always be running. | |
- # config.active_record.observers = :cacher, :garbage_collector, :forum_observer | |
+ # Activate observers that should always be running | |
+ # config.active_record.observer_path 'app/models/*_observer.rb' | |
+ # config.active_record.observer :cacher, :garbage_collector, :forum_observer | |
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. | |
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. | |
-- | |
1.7.3.5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment