Rails application-wide events/hooks for plugin architecture

Go To StackoverFlow.com


I am building a Ruby on Rails application and I want to be able to write custom plugins that respond to application-wide events.

For example, a user can check-in to a place in my application. When this occurs, I may want to be able to

  • Send an e-mail notification
  • Send a push notification
  • Award some points to the user
  • Log the event to the database

The point is, I want the above actions to be decoupled from the check-in code. I want to be able to enable or disable these different code snippets. I should be able to add other actions to the code without touching the check-in code base.

Does Ruby on Rails have any mechanisms for doing something like this?


2012-04-04 18:15
by Venkat D.


While I am sure there are plenty of other publish/subscribe type systems, it's probably worth at least looking at ActiveSupport::Notifications which is part of rails

It makes it pretty easy to publish events to subscribers and for interested 3rd parties to subscribe to them. The runtime summary in your application log file is implemented using this: rails publishes events with the runtime info and also provides a subscriber that writes the info to the logfile. This is also how activerecord query logging is implemented.

2012-04-04 19:10
by Frederick Cheung


What you are describing is essentially a generic post/subscribe mechanism. There are a thousand ways to do this, and probably as many libraries availble on github and rubygems.

They key question is, how do you want to change the behaviour? Are you happy just to call some functions at startup to define the behavior? In ruby this is absurdly simple:

#! /usr/bin/ruby

@hooks = []

def run_hooks(*args)
    @hooks.each { |hook| hook.call(*args) }

def on_trigger(&hook)
    @hooks << hook

on_trigger { |*args| puts "This is one trigger: #{args.inspect}" }

on_trigger { puts "This is another trigger" }


Or do you want something more sophistocated?

2012-04-04 19:01
by Michael Slade