Skip to content

Commit

Permalink
Bring observer definition code inline with Rails 3.0 stable branch.
Browse files Browse the repository at this point in the history
* Observers will no longer be fired multiple times for descendent models
* Anonymous observers are no longer supported
  • Loading branch information
alindeman committed Feb 23, 2011
1 parent 3df8369 commit 83dbd4c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
10 changes: 8 additions & 2 deletions lib/no_peeping_toms.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'active_record'
require 'active_support/core_ext/class/attribute_accessors'
require 'active_support/core_ext/string/inflections'

module NoPeepingToms
extend ActiveSupport::Concern
Expand Down Expand Up @@ -52,11 +53,16 @@ module InstanceMethods
# way so far
def define_callbacks_with_enabled_check(klass)
observer = self
observer_name = observer.class.name.underscore.gsub('/', '__')

ActiveRecord::Callbacks::CALLBACKS.each do |callback|
next unless respond_to?(callback)
klass.send(callback) do |record|
observer.send(callback, record) if observer.observer_enabled?
callback_meth = :"_notify_#{observer_name}_for_#{callback}"
unless klass.respond_to?(callback_meth)
klass.send(:define_method, callback_meth) do
observer.send(callback, self) if observer.observer_enabled?
end
klass.send(callback, callback_meth)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/no_peeping_toms/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module NoPeepingToms
VERSION = "2.0.1"
VERSION = "2.1.0"
end
32 changes: 16 additions & 16 deletions spec/no_peeping_toms_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

module NoPeepingTomsSpec
class Person < ActiveRecord::Base; end
class SpecialPerson < Person; end

class PersonObserver < ActiveRecord::Observer
cattr_accessor :called

def before_create(person)
self.class.called = true
self.class.called ||= 0
self.class.called += 1
end
end

Expand All @@ -16,7 +18,8 @@ class AnotherObserver < ActiveRecord::Observer
cattr_accessor :called

def before_create(person)
self.class.called = true
self.class.called ||= 0
self.class.called += 1
end
end

Expand All @@ -32,16 +35,23 @@ def before_create(person)

it "runs default observers when default observers are enabled" do
ActiveRecord::Observer.enable_observers
PersonObserver.called = false
PersonObserver.called = 0
Person.create!
PersonObserver.called.should be_true
PersonObserver.called.should == 1
end

it "does not run default observers when default observers are disabled" do
ActiveRecord::Observer.disable_observers
PersonObserver.called = false
PersonObserver.called = 0
Person.create!
PersonObserver.called.should be_false
PersonObserver.called.should == 0
end

it "only runs observers once on descendent classes" do
ActiveRecord::Observer.enable_observers
PersonObserver.called = 0
SpecialPerson.create!
PersonObserver.called.should == 1
end
end

Expand Down Expand Up @@ -72,16 +82,6 @@ def before_create(person)
AnotherObserver.called.should be_true
end

it "should accept anonymous observers" do
called = false
observer = Class.new(ActiveRecord::Observer) do
observe NoPeepingTomsSpec::Person
define_method(:before_create) {|person| called = true }
end
ActiveRecord::Observer.with_observers(observer) { Person.create! }
called.should be_true
end

it "should ensure peeping toms are reset after raised exception" do
lambda {
ActiveRecord::Observer.with_observers(NoPeepingTomsSpec::PersonObserver) do
Expand Down

0 comments on commit 83dbd4c

Please sign in to comment.