# \[ANN\] Switchboard - Easy to use global ActiveRecord event listeners

**URL:** <https://www.ruby-forum.com/t/ann-switchboard-easy-to-use-global-activerecord-event-listeners/176243>\
**Category:** Rails\
**Created:** [September 27, 2009, 10:09pm UTC](https://www.ruby-forum.com/t/ann-switchboard-easy-to-use-global-activerecord-event-listeners/176243 "2009-09-27T22:09:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![zilkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e19b73/32.png) [@zilkey](https://www.ruby-forum.com/u/zilkey)\
**Post date:** [September 27, 2009, 10:09pm UTC](https://www.ruby-forum.com/t/ann-switchboard-easy-to-use-global-activerecord-event-listeners/176243/1 "2009-09-27T22:09:22Z")

</div>

Switchboard is a simple, event-observing framework for ActiveRecord.  
It’s designed to make it easy to add observers for all models in your  
app, and to easily turn them on and off selectively.

Intallation

gem sources -a [http://gems.github.com](http://gems.github.com)  
sudo gem install zilkey-switchboard

Usage

First, require switchboard above your rails initializer:

# environment.rb

require ‘switchboard’  
Rails::Initializer.run do |config|  
# …  
end

Add the listeners to the Switchboard in an initializer:

# config/initializers/switchboard.rb

Switchboard.listeners \<\< ActivityFeedListener

Then, create a listener class that defines methods for after\_create,  
after\_update and after\_destroy:

class ActivityFeedListener  
class \<\< self  
def after\_create(record)  
description = “#{record.class.name} was created”  
publish\_activity\_feed\_items record, description  
end

```
def after_update(record)
  description = "#{record.class.name} was updated"
  publish_activity_feed_items record, description
end

def after_destroy(record)
  description = "#{record.class.name} was deleted"
  publish_activity_feed_items record, description
end

def publish_activity_feed_items(record, description)
  record.activity_feed_item_subscribers.each do |subscriber|
    ActivityFeedItem.create :user => subscriber, :description =>

```

description  
end  
end

```
private :publish_activity_feed_items

```

end  
end

Notice how these classes are almost identical to  
ActiveRecord::Observer subclasses, designed so that it’s easy to  
refactor from one to the other.

When unit testing if your listeners are all firing your unit tests  
become integration tests. To avoid this, you can easily turn off  
listeners for all specs all the time:

Spec::Runner.configure do |config|  
config.before(:each) do  
Switchboard.listeners.clear  
end  
end

Then, when you want them back on again, you can either turn them back  
on for a spec:

describe “Integrating with listeners” do  
before do  
Switchboard.listeners \<\< FooListener  
end  
end

When doing data imports, migrations or certain actions that need to  
only use certain listeners, you can easily specify which ones you’d  
like to use:

Switchboard.with\_listeners AuditListener, ActivityListener do  
Article.create! :title =\> “foo”  
end

After the block runs, the original listeners are restored.

If you want to run some code with no listeners, you can do so with:

Switchboard.without\_listeners do  
Article.create! :title =\> “foo”  
end

To contribute:

[http://github.com/zilkey/switchboard](http://github.com/zilkey/switchboard)

---

<div class="post-metadata">

**Author:** ![zilkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e19b73/32.png) [@zilkey](https://www.ruby-forum.com/u/zilkey)\
**Post date:** [September 28, 2009, 7:57pm UTC](https://www.ruby-forum.com/t/ann-switchboard-easy-to-use-global-activerecord-event-listeners/176243/2 "2009-09-28T19:57:07Z")

</div>

On Sep 27, 4:04 pm, zilkey [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> First, require switchboard above your rails initializer:
> 
> # environment.rb
> 
> require ‘switchboard’  
> Rails::Initializer.run do |config|  
> # …  
> end

Github is flaking out ATM, so I can’t check the source, but is there a  
reason that you’re wanting people to load your gem like this? It  
breaks the ability of ‘rake gems’ and friends to unpack/install/verify  
the gem.

–Matt J.
