Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Use assignment instead of call
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2327 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
dhh committed Sep 25, 2005
1 parent 948be2c commit 7f86e02
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 19 deletions.
8 changes: 2 additions & 6 deletions activerecord/CHANGELOG
@@ -1,12 +1,8 @@
*SVN*

* Added new symbol-driven approach to activating observers with Base#observer [DHH]. Example:
* Added new symbol-driven approach to activating observers with Base#observers= [DHH]. Example:

ActiveRecord::Base.observer(:cacher, :garbage_collector)

...which is the same as doing:

[ Cacher.instance, GarbageCollector.instance ]
ActiveRecord::Base.observers = :cacher, :garbage_collector

* Added AbstractAdapter#select_value and AbstractAdapter#select_values as convenience methods for selecting single values, instead of hashes, of the first column in a SELECT #2283 [solo@gatelys.com]

Expand Down
11 changes: 5 additions & 6 deletions activerecord/lib/active_record/observer.rb
Expand Up @@ -10,16 +10,15 @@ def self.append_features(base)
module ClassMethods
# Activates the observers assigned. Examples:
#
# # Calls PersonObserver.instance and returns the instance of that observer
# ActiveRecord::Base.observer(:person_observer)
# # Calls PersonObserver.instance
# ActiveRecord::Base.observers = :person_observer
#
# # Calls Cacher.instance and GarbageCollector.instance
# # and returns an array with instances of both
# ActiveRecord::Base.observer(:cacher, :garbage_collector)
# ActiveRecord::Base.observers = :cacher, :garbage_collector
#
# # Same as above, just using explicit class references
# ActiveRecord::Base.observer(Cacher, GarbageCollector)
def observer(*observers)
# ActiveRecord::Base.observers = Cacher, GarbageCollector
def observers=(*observers)
observers = [ observers ].flatten.collect do |observer|
observer.is_a?(Symbol) ?
observer.to_s.camelize.constantize.instance :
Expand Down
12 changes: 6 additions & 6 deletions activerecord/test/lifecycle_test.rb
Expand Up @@ -65,22 +65,22 @@ def test_before_destroy
end

def test_after_save
topic_observer = ActiveRecord::Base.observer(:topic_manual_observer)
ActiveRecord::Base.observers = :topic_manual_observer

topic = Topic.find(1)
topic.title = "hello"
topic.save

assert topic_observer.has_been_notified?
assert_equal :after_save, topic_observer.callbacks.last["callback_method"]
assert TopicManualObserver.instance.has_been_notified?
assert_equal :after_save, TopicManualObserver.instance.callbacks.last["callback_method"]
end

def test_observer_update_on_save
topic_observer = ActiveRecord::Base.observer(TopicManualObserver)
ActiveRecord::Base.observers = TopicManualObserver

topic = Topic.find(1)
assert topic_observer.has_been_notified?
assert_equal :after_find, topic_observer.callbacks.first["callback_method"]
assert TopicManualObserver.instance.has_been_notified?
assert_equal :after_find, TopicManualObserver.instance.callbacks.first["callback_method"]
end

def test_auto_observer
Expand Down
2 changes: 1 addition & 1 deletion railties/environments/environment.rb
Expand Up @@ -27,7 +27,7 @@
# config.action_controller.fragment_cache_store = :file_store, "#{RAILS_ROOT}/cache"

# Activate observers that should always be running
# config.active_record.observer :cacher, :garbage_collector
# config.active_record.observers = :cacher, :garbage_collector

# Make Active Record use UTC-base instead of local time
# config.active_record.default_timezone = :utc
Expand Down

0 comments on commit 7f86e02

Please sign in to comment.