mirror of https://github.com/rails/rails
Added new symbol-driven approach to activating observers with Base#observer [DHH]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2326 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
2346f5716f
commit
948be2c998
|
@ -1,8 +1,10 @@
|
|||
*SVN*
|
||||
|
||||
* Fixed that auto_discovery_link_tag couldn't take a string as the URL
|
||||
* Fix open/save dialog in IE not opening files send with send_file/send_data, #2279 [Thomas Fuchs]
|
||||
|
||||
* Fixed problem with send_file and WEBrick using stdout #1812
|
||||
* Fixed that auto_discovery_link_tag couldn't take a string as the URL [DHH]
|
||||
|
||||
* Fixed problem with send_file and WEBrick using stdout #1812 [DHH]
|
||||
|
||||
* Optimized tag_options to not sort keys, which is no longer necessary when assert_dom_equal and friend is available #1995 [skae]
|
||||
|
||||
|
|
|
@ -1,10 +1,20 @@
|
|||
*SVN*
|
||||
|
||||
* Wrap :conditions in parentheses to prevent problems with OR's #1871
|
||||
* Added new symbol-driven approach to activating observers with Base#observer [DHH]. Example:
|
||||
|
||||
* Allow the postgresql adapter to work with the SchemaDumper.
|
||||
ActiveRecord::Base.observer(:cacher, :garbage_collector)
|
||||
|
||||
...which is the same as doing:
|
||||
|
||||
[ Cacher.instance, GarbageCollector.instance ]
|
||||
|
||||
* Add ActiveRecord::SchemaDumper for dumping a DB schema to a pure-ruby file, making it easier to consolidate large migration lists and port database schemas between databases.
|
||||
* 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]
|
||||
|
||||
* Wrap :conditions in parentheses to prevent problems with OR's #1871 [Jamis Buck]
|
||||
|
||||
* Allow the postgresql adapter to work with the SchemaDumper. [Jamis Buck]
|
||||
|
||||
* Add ActiveRecord::SchemaDumper for dumping a DB schema to a pure-ruby file, making it easier to consolidate large migration lists and port database schemas between databases. [Jamis Buck]
|
||||
|
||||
* Fixed migrations for Windows when using more than 10 [David Naseby]
|
||||
|
||||
|
|
|
@ -54,6 +54,7 @@ ActiveRecord::Base.class_eval do
|
|||
include ActiveRecord::Validations
|
||||
include ActiveRecord::Locking
|
||||
include ActiveRecord::Callbacks
|
||||
include ActiveRecord::Observing
|
||||
include ActiveRecord::Timestamp
|
||||
include ActiveRecord::Associations
|
||||
include ActiveRecord::Aggregations
|
||||
|
|
|
@ -1,6 +1,36 @@
|
|||
require 'singleton'
|
||||
|
||||
module ActiveRecord
|
||||
module Observing # :nodoc:
|
||||
def self.append_features(base)
|
||||
super
|
||||
base.extend(ClassMethods)
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
# Activates the observers assigned. Examples:
|
||||
#
|
||||
# # Calls PersonObserver.instance and returns the instance of that observer
|
||||
# ActiveRecord::Base.observer(:person_observer)
|
||||
#
|
||||
# # Calls Cacher.instance and GarbageCollector.instance
|
||||
# # and returns an array with instances of both
|
||||
# ActiveRecord::Base.observer(:cacher, :garbage_collector)
|
||||
#
|
||||
# # Same as above, just using explicit class references
|
||||
# ActiveRecord::Base.observer(Cacher, GarbageCollector)
|
||||
def observer(*observers)
|
||||
observers = [ observers ].flatten.collect do |observer|
|
||||
observer.is_a?(Symbol) ?
|
||||
observer.to_s.camelize.constantize.instance :
|
||||
observer.instance
|
||||
end
|
||||
|
||||
observers.size > 1 ? observers : observers.first
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Observer classes respond to lifecycle callbacks to implement trigger-like
|
||||
# behavior outside the original class. This is a great way to reduce the
|
||||
# clutter that normally comes when the model class is burdened with
|
||||
|
@ -48,7 +78,8 @@ module ActiveRecord
|
|||
# == Triggering Observers
|
||||
#
|
||||
# In order to activate an observer, you need to call Observer.instance. In Rails, this can be done in controllers
|
||||
# using the short-hand of for example observer :comment_observer.
|
||||
# using the short-hand of for example observer :comment_observer. Or directly from Active Record, with
|
||||
# ActiveRecord::Base.observer(:comment_observer).
|
||||
class Observer
|
||||
include Singleton
|
||||
|
||||
|
|
|
@ -65,7 +65,7 @@ class LifecycleTest < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
def test_after_save
|
||||
topic_observer = TopicManualObserver.instance
|
||||
topic_observer = ActiveRecord::Base.observer(:topic_manual_observer)
|
||||
|
||||
topic = Topic.find(1)
|
||||
topic.title = "hello"
|
||||
|
@ -76,7 +76,7 @@ class LifecycleTest < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
def test_observer_update_on_save
|
||||
topic_observer = TopicManualObserver.instance
|
||||
topic_observer = ActiveRecord::Base.observer(TopicManualObserver)
|
||||
|
||||
topic = Topic.find(1)
|
||||
assert topic_observer.has_been_notified?
|
||||
|
|
|
@ -18,9 +18,6 @@ Rails::Initializer.run do |config|
|
|||
# (by default production uses :info, the others :debug)
|
||||
# config.log_level = :debug
|
||||
|
||||
# Only include the connection adapters you're actually going to use
|
||||
# config.connection_adapters = %w( mysql postgresql sqlite sqlserver db2 oci )
|
||||
|
||||
# Use the database for sessions instead of the file system
|
||||
# (create the session table with 'rake create_sessions_table')
|
||||
# config.action_controller.session_store = :active_record_store
|
||||
|
@ -29,6 +26,9 @@ Rails::Initializer.run do |config|
|
|||
# (remember to create the caching directory and make it readable to the application)
|
||||
# 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
|
||||
|
||||
# Make Active Record use UTC-base instead of local time
|
||||
# config.active_record.default_timezone = :utc
|
||||
|
||||
|
|
Loading…
Reference in New Issue