Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding rdoc to observer, start refactor of spec

  • Loading branch information...
commit 2f77710e6d56b430308c9d254596f2b1cdfe1965 1 parent f5b39a1
@durran durran authored
Showing with 73 additions and 22 deletions.
  1. +45 −14 lib/mongoid/observer.rb
  2. +28 −8 spec/unit/mongoid/observer_spec.rb
View
59 lib/mongoid/observer.rb
@@ -1,34 +1,65 @@
# encoding: utf-8
module Mongoid #:nodoc:
+
+ # Mongoid observers hook into the lifecycle of documents.
class Observer < ActiveModel::Observer
+
+ # Instantiate the new observer. Will add all child observers as well.
+ #
+ # @example Instantiate the observer.
+ # Mongoid::Observer.new
+ #
+ # @since 2.0.0
def initialize
- super
- observed_descendants.each { |klass| add_observer!(klass) }
+ super and observed_descendants.each { |klass| add_observer!(klass) }
end
protected
+ # Get all the child observers.
+ #
+ # @example Get the children.
+ # observer.observed_descendants
+ #
+ # @return [ Array<Class> ] The children.
+ #
+ # @since 2.0.0
def observed_descendants
observed_classes.sum([]) { |klass| klass.descendants }
end
+ # Adds the specified observer to the class.
+ #
+ # @example Add the observer.
+ # observer.add_observer!(Document)
+ #
+ # @param [ Class ] klass The child observer to add.
+ #
+ # @since 2.0.0
def add_observer!(klass)
- super
- define_callbacks klass
+ super and define_callbacks(klass)
end
+ # Defines all the callbacks for each observer of the model.
+ #
+ # @example Define all the callbacks.
+ # observer.define_callbacks(Document)
+ #
+ # @param [ Class ] klass The model to define them on.
+ #
+ # @since 2.0.0
def define_callbacks(klass)
- observer = self
- observer_name = observer.class.name.underscore.gsub('/', '__')
-
- Mongoid::Callbacks::CALLBACKS.each do |callback|
- next unless respond_to?(callback)
- callback_meth = :"_notify_#{observer_name}_for_#{callback}"
- unless klass.respond_to?(callback_meth)
- klass.send(:define_method, callback_meth) do |&block|
- observer.send(callback, self, &block)
+ tap do |observer|
+ observer_name = observer.class.name.underscore.gsub('/', '__')
+ Mongoid::Callbacks::CALLBACKS.each do |callback|
+ next unless respond_to?(callback)
+ callback_meth = :"_notify_#{observer_name}_for_#{callback}"
+ unless klass.respond_to?(callback_meth)
+ klass.send(:define_method, callback_meth) do |&block|
+ observer.send(callback, self, &block)
+ end
+ klass.send(callback, callback_meth)
end
- klass.send(callback, callback_meth)
end
end
end
View
36 spec/unit/mongoid/observer_spec.rb
@@ -1,17 +1,37 @@
require "spec_helper"
describe Mongoid::Observer do
- after { CallbackRecorder.instance.reset }
- it { ActorObserver.instance.should be_a_kind_of(ActiveModel::Observer) }
+ before do
+ [ Actor, Actress ].each(&:delete_all)
+ end
+
+ after do
+ CallbackRecorder.instance.reset
+ end
+
+ it "is an instance of an active model observer" do
+ ActorObserver.instance.should be_a_kind_of(ActiveModel::Observer)
+ end
+
+ context "when the observer has descendants" do
- it "observes descendent classes" do
- actor_observer = ActorObserver.instance
- actor = Actor.create!(:name => "Johnny Depp")
- actor_observer.last_after_create_record.try(:name).should == actor.name
+ let!(:observer) do
+ ActorObserver.instance
+ end
+
+ let(:actor) do
+ Actor.create!(:name => "Johnny Depp")
+ end
- actress = Actress.create!(:name => "Tina Fey")
- actor_observer.last_after_create_record.try(:name).should == actress.name
+ let(:actress) do
+ Actress.create!(:name => "Tina Fey")
+ end
+
+ it "observes descendent class" do
+ actor and observer.last_after_create_record.try(:name).should == actor.name
+ actress and observer.last_after_create_record.try(:name).should == actress.name
+ end
end
it "observes after_initialize" do
Please sign in to comment.
Something went wrong with that request. Please try again.