public
Description: Rails micro-plugin which allows models to observe each other.
Homepage: http://github.com/maxim/stalker
Clone URL: git://github.com/maxim/stalker.git
name age message
file README.rdoc Loading commit data...
file init.rb
directory lib/
directory shoulda_macros/
README.rdoc

Stalker

Rails micro-plugin (38 lines) which allows models to observe each other.

Install

  script/plugin install git://github.com/maxim/stalker.git

Usage

Simple.

  class Celebrity < ActiveRecord::Base
  end

  class Idiot < ActiveRecord::Base
    stalk Celebrity

    class << self
      def before_celebrity_save(celebrity)
        puts "Idiot: 'Click'. Haha, got your picture!"
      end
    end
  end

Just like with observers, you have to edit environment.rb to tell rails to load Idiot first. (Idiot should be on the scene when Celebrity shows up.)

  config.active_record.observers = [:idiot]

Now let’s see what happens.

  % britney = Celebrity.new
  % britney.save
  % Idiot: 'Click'. Haha, got your picture!

What happened?

The stalker class (Idiot) now has magical class methods (such as before_[stalked_class]_save(stalked_instance), after_[stalked_class]_validate(stalked_instance), etc) for reacting to stalked class’s callbacks. Keep in mind they’re class methods, hence the class << self thingie. As an argument these methods get the reference to the instance of the stalked class. This way you can alter the object or just extract information from it.

Group your victims!

If you want to stalk multiple models with same responses, it’s easy to do using :group option.

  class Celebrity < ActiveRecord::Base
  end

  class Politician < ActiveRecord::Base
  end

  class Idiot < ActiveRecord::Base
    stalk Celebrity, Politician, :group => :victim

    def self.before_victim_save(victim)
      if victim.is_a?(Politician)
        poke(victim)
      else
        bite(victim)
      end
    end
  end

Why?

Sometimes you want model A to notify model B if something happens to it, so that model B could react. It’s useful if it’s concern of model B, like it has to update its records. If you have Product model that needs to decrement count of products when Order has been placed, you’d want Order model to notify Product model about the fact that it’s been placed. Product can then decrement item count in itself.

Can Idiot prevent Celebrity from saving?

Yes. If Idiot defines method before_celebrity_save and return false - it will halt the callback chain for Celebrity as usual.

But rails already has observers!

They’re useful when you want to have separate class for reacting to certain model’s callbacks. What if you want other existing models to react? That’s extra manual work that could be eliminated.

TODO

Write tests!!!

License

Copyright © 2009 Maxim Chernyak

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.