diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2005-09-25 08:26:29 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2005-09-25 08:26:29 +0000 |
commit | 948be2c998c9e3e56da742b38bc0ab57b75c8d2d (patch) | |
tree | 9b078f37ccefd4c0943c1c0080c5a1a572118d4d | |
parent | 2346f5716f7b08168b74b29e0279d51c51557b08 (diff) | |
download | rails-948be2c998c9e3e56da742b38bc0ab57b75c8d2d.tar.gz rails-948be2c998c9e3e56da742b38bc0ab57b75c8d2d.tar.bz2 rails-948be2c998c9e3e56da742b38bc0ab57b75c8d2d.zip |
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
-rw-r--r-- | actionpack/CHANGELOG | 6 | ||||
-rw-r--r-- | activerecord/CHANGELOG | 16 | ||||
-rwxr-xr-x | activerecord/lib/active_record.rb | 1 | ||||
-rw-r--r-- | activerecord/lib/active_record/observer.rb | 33 | ||||
-rwxr-xr-x | activerecord/test/lifecycle_test.rb | 4 | ||||
-rw-r--r-- | railties/environments/environment.rb | 6 |
6 files changed, 55 insertions, 11 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index e84b2e4613..d8ba20306d 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -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] diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 1983a18e50..393bca06a1 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -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 ] + +* 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. +* 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] diff --git a/activerecord/lib/active_record.rb b/activerecord/lib/active_record.rb index 8d50df6758..27ec3a6791 100755 --- a/activerecord/lib/active_record.rb +++ b/activerecord/lib/active_record.rb @@ -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 diff --git a/activerecord/lib/active_record/observer.rb b/activerecord/lib/active_record/observer.rb index 36a9ec5d3a..36fcf021cd 100644 --- a/activerecord/lib/active_record/observer.rb +++ b/activerecord/lib/active_record/observer.rb @@ -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 diff --git a/activerecord/test/lifecycle_test.rb b/activerecord/test/lifecycle_test.rb index d6c017672d..a373471f19 100755 --- a/activerecord/test/lifecycle_test.rb +++ b/activerecord/test/lifecycle_test.rb @@ -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? diff --git a/railties/environments/environment.rb b/railties/environments/environment.rb index 59ac96343e..60d02785ed 100644 --- a/railties/environments/environment.rb +++ b/railties/environments/environment.rb @@ -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 |