diff options
author | Mikel Lindsaar <raasdnil@gmail.com> | 2010-04-25 17:07:55 +1000 |
---|---|---|
committer | Mikel Lindsaar <raasdnil@gmail.com> | 2010-04-25 17:07:55 +1000 |
commit | e1b255aca456a5b456111b09237805fd32d15111 (patch) | |
tree | a87c8b8989449a9dbdf1c6897901f6957ec021dc /activerecord/lib/active_record/observer.rb | |
parent | 60ab54113fa833a1258d687673561b9474964149 (diff) | |
parent | a5955196f2ed8a69c49a1a8c0c617ab91cb8d716 (diff) | |
download | rails-e1b255aca456a5b456111b09237805fd32d15111.tar.gz rails-e1b255aca456a5b456111b09237805fd32d15111.tar.bz2 rails-e1b255aca456a5b456111b09237805fd32d15111.zip |
Merge branch 'master' of github.com:lifo/docrails
Diffstat (limited to 'activerecord/lib/active_record/observer.rb')
-rw-r--r-- | activerecord/lib/active_record/observer.rb | 26 |
1 files changed, 19 insertions, 7 deletions
diff --git a/activerecord/lib/active_record/observer.rb b/activerecord/lib/active_record/observer.rb index 0fbb1f0261..ed0f039597 100644 --- a/activerecord/lib/active_record/observer.rb +++ b/activerecord/lib/active_record/observer.rb @@ -96,7 +96,8 @@ module ActiveRecord end def self.method_added(method) - self.observed_methods += [method] if ActiveRecord::Callbacks::CALLBACKS.include?(method.to_sym) + method = method.to_sym + observed_methods << method if ActiveRecord::Callbacks::CALLBACKS.include?(method) end protected @@ -104,16 +105,27 @@ module ActiveRecord observed_classes.sum([]) { |klass| klass.send(:subclasses) } end + def observe_callbacks? + self.class.observed_methods.any? + end + def add_observer!(klass) super + define_callbacks klass if observe_callbacks? + end + + def define_callbacks(klass) + existing_methods = klass.instance_methods.map(&:to_sym) + observer = self + observer_name = observer.class.name.underscore.gsub('/', '__') - # Check if a notifier callback was already added to the given class. If - # it was not, add it. self.class.observed_methods.each do |method| - callback = :"_notify_observers_for_#{method}" - if (klass.instance_methods & [callback, callback.to_s]).empty? - klass.class_eval "def #{callback}; notify_observers(:#{method}); end" - klass.send(method, callback) + callback = :"_notify_#{observer_name}_for_#{method}" + unless existing_methods.include? callback + klass.send(:define_method, callback) do # def _notify_user_observer_for_before_save + observer.update(method, self) # observer.update(:before_save, self) + end # end + klass.send(method, callback) # before_save :_notify_user_observer_for_before_save end end end |