Killing observers

  • decreases startup time by not loading all your models
  • makes it possible to preload config/environment.rb and still test models -> spin/zeus
  • makes dependencies obvious
  • replaces ActiveRecord magic with ruby
  • makes your app Rails 4 ready

Before:

# config/environment.rb
config.observers = [:foo_observer] 

# app/observers/foo_observer.rb
class FooObserver < ActiveRecord::Observer
  observes :user

  def after_save(user)
    ....
  end
end

After:

# app/models/user.rb
class User < ActiveRecord::Base
  include FooObserver
end

# app/observers/foo_observer.rb
module FooObserver
  class << self
    def included(base)
      this = self
      base.after_save{|user| this.more_descriptive_name(user) }
    end

    def more_descriptive_name(user)
      ...
    end
  end
end
About these ads