What's New in Edge Rails: Observer Generators

Posted by ryan
at 6:29 PM on Tuesday, May 23, 2006

I like to keep a close eye on the changes being committed to edge Rails as it’s a great learning experience – and thought I might as well post a summary of them as they roll in. This will definitely not be an exhaustive effort – just something as I see fit or have the time. So here goes the first one:

Rails Changeset 4365; Add generator files…

This changeset simply adds the files necessary to use the script/generator utility to auto-create observer stubs. What’s an observer you ask? It’s a way to monitor lifecycle events of a model object outside of the model itself and let’s you avoid cluttering the model class with logic that’s not core to the model.

So how does one use this new observer generator? Observe:


ruby script/generate observer User

Yeah, that’s really it. This will create two files for you, the observer class itself at app/models/user_observer.rb:


class UserObserver < ActiveRecord::Observer
end

and the unit test at test/unit/user_observer_test.rb:


require File.dirname(__FILE__) + '/../test_helper'

class UserObserverTest < Test::Unit::TestCase
  fixtures :users

  # Replace this with your real tests.
  def test_truth
    assert true
  end
end

You can then edit the observer class with the appropriate callback methods:


class UserObserver < ActiveRecord::Observer
  def after_save(comment)
    # Do something great
  end
end

On a side note, I feel like the generator utility has been getting a bad rap recently from people that frown upon auto-generation of code. Fair enough, but think of them more as best practices templates and learning tools rather than some sort of coding robot. They’re really great tools for seeing how things should be done, and how things are expected to be done. If you don’t want to use them, don’t – but if you’re not quite sure how to go about creating an observer, or controller, or model they’re of great help.

So there you have it – the new observer generator functionality in edge Rails.

tags: rubyonrails, generators

Comments

Leave a response

  1. AdamSeptember 13, 2006 @ 11:51 AM
    Do you know how the Observer testing will work?
  2. Ryan DaigleSeptember 13, 2006 @ 03:21 PM
    Adam, an observer is just a normal Ruby class - so you should be able to invoke it and test it directly:
    
    o = UserObserver.new
    u = User.new
    o.after_update(u)
    assert "some test on u or other side effect"
    
    
    You can also just say:
    
    u = User.new.update
    assert "some test on u or other side effect"
    
    
    and as long as your observer is registered in @environment.rb@ you should be good to go: @config.active_record.observers = :comment_observer, :signup_observer@
  3. Akhil BansalOctober 02, 2006 @ 11:06 PM
    Hi Ryan, I have a problem while using observer on Rails 1.1.6. Here it is: I have a model Article which, acts_as_taggable. I created a ArticleObserver class in article_observer.rb in model dir. Also I registered my observer in enviroment.rb Now when I restarted my server the error was: ./script/../config/../vendor/rails/activerecord/lib/active_record/base.rb:1129:in `method_missing': undefined local variable or method `acts_as_taggable' for Article:Class (NameError) If I remove updates from enviroment.rb then it works except observer. Have you any idea. Thanks
  4. Ryan DaigleOctober 03, 2006 @ 06:33 AM
    Akhil: Hmmm, try putting this into your environment.rb: @require 'acts_as_taggable'@ ?