Skip to content

Commit

Permalink
Fixed bug introduced in PR rails#39.
Browse files Browse the repository at this point in the history
  • Loading branch information
mvastola committed Feb 5, 2016
1 parent 455a75e commit 722267d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
45 changes: 35 additions & 10 deletions lib/rails/observers/active_model/observing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ module Observing

included do
extend ActiveSupport::DescendantsTracker
def self.inherited(subclass)
super
ActiveRecord::Base.instantiate_observers
end
end


module ClassMethods
# Activates the observers assigned.
#
Expand Down Expand Up @@ -74,7 +79,7 @@ def observers
#
# Foo.observer_instances # => [#<FooObserver:0x007fc212c40820>]
def observer_instances
@observer_instances ||= []
@observer_instances ||= Set.new
end

# Instantiate the global observers.
Expand Down Expand Up @@ -293,10 +298,22 @@ def notify_observers(method, *extra_args)
# sure to read about the necessary configuration in the documentation for
# ActiveRecord::Observer.
class Observer
include Singleton
extend ActiveSupport::DescendantsTracker

class << self

# Provides simplistic, singleton-like functionality for
# this class.
#
# Will only instantiate a new object upon first invocation.
# Thereafter, returns previous object.
#
def instance
@instance ||= new
@instance.send(:add_observers!)
@instance
end
protected :new, :allocate

# Attaches the observer to the supplied model classes.
#
# class AuditObserver < ActiveModel::Observer
Expand All @@ -322,7 +339,7 @@ def observe(*models)
# end
# end
def observed_classes
Array(observed_class)
Array(observed_class).compact
end

# Returns the class observed by default. It's inferred from the observer's
Expand All @@ -331,14 +348,18 @@ def observed_classes
# PersonObserver.observed_class # => Person
# AccountObserver.observed_class # => Account
def observed_class
name[/(.*)Observer/, 1].try :constantize
return nil unless observed_class_loaded?
observed_class_name.try(:constantize)
end

def observed_class_name #:nodoc:
name[/(.*)Observer/, 1]
end
end

# Start observing the declared classes and their subclasses.
# Called automatically by the instance method.
def initialize #:nodoc:
observed_classes.each { |klass| add_observer!(klass) }
def observed_class_loaded? #:nodoc:
return nil if observed_class_name.blank?
ActiveSupport::Dependencies.qualified_const_defined?(observed_class_name)
end
end

def observed_classes #:nodoc:
Expand All @@ -360,6 +381,10 @@ def observed_class_inherited(subclass) #:nodoc:
end

protected
def add_observers! #:nodoc:
observed_classes.each { |klass| add_observer!(klass) }
end

def add_observer!(klass) #:nodoc:
klass.add_observer(self)
end
Expand Down
4 changes: 2 additions & 2 deletions lib/rails/observers/activerecord/observer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ def define_callbacks(klass)

ActiveRecord::Callbacks::CALLBACKS.each do |callback|
next unless respond_to?(callback)
callback_meth = :"_notify_#{observer_name}_for_#{callback}"
unless klass.respond_to?(callback_meth)
callback_meth = :"_notify_#{observer_name}_for_#{callback}".to_sym
unless klass.instance_methods.include?(callback_meth)
klass.send(:define_method, callback_meth) do |&block|
observer.update(callback, self, &block)
end
Expand Down
1 change: 1 addition & 0 deletions lib/rails/observers/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Railtie < ::Rails::Railtie

config.after_initialize do |app|
ActiveSupport.on_load(:active_record) do
ActiveRecord::Base.instantiate_observers
ActionDispatch::Reloader.to_prepare do
ActiveRecord::Base.instantiate_observers
end
Expand Down

0 comments on commit 722267d

Please sign in to comment.