Emacs tip: running ack in the project root w/rinari

This post covers how to combine the awesomeness of ack with rinari in emacs to run ack in the project root by default. (If you aren’t using ack, definitely check it out for searching your codebase. If you aren’t using emacs, this post isn’t going to convince you to switch).

I’ve been using Kim van Wyk’s ack.el for a few months now, but was annoyed that I had to tell it the directory where to run ack, when in most cases I wanted it to run in the root of my Rails app. So I copied the ack function from ack.el and made one that only asks for the search pattern, then runs in the root of the app (provided by rinari’s rinari-root function). I then bound that function to C-c f a.

Here is the code:

Note: you will need rinari and ack.el loaded, and ack will need to be in your path.

Refreshing an individual gemspec

I’m in the process of upgrading a rails app from 2.1 to 2.3, and 2.3 wants me to refresh some of the gemspecs for my vendored gems. Unfortunately, when I run rake gems:refresh_specs I get the dreaded:

rake aborted!
can't activate , already activated json-1.1.9

I was able to refresh the spec in question outside of rake with (in the console):

>> Rails::GemDependency.from_directory_name('vendor/gems/gem_name-0.1.0', false).refresh

Hopefully that helps someone else out.

Notify a Campfire room on cap deploy

We use Campfire for internal communication, and I’ve lately been pushing notifications there for all to see (Zendesk ticket updates, git repo pushes, etc). We decided it would be useful for the support team to know when a deployment occurs. I googled a bit to see if anyone had shared a Capistrano recipe, and did not find one. So I’m sharing mine.

Using the tinder gem, its super easy:


RAILS_ROOT/app Organization

As an app grows, so does the amount of files in app/models. Here is a simple tip to help reduce the clutter: move mailers, observers, and sweepers out of app/models into app/mailers, app/observers, and app/sweepers, respectively. For rails to still load them, you will need to modify config/environment.rb:

# clean up app/models a bit
%w{mailers observers sweepers}.each do |dir|
    config.load_paths << "#{RAILS_ROOT}/app/#{dir}"

As a bonus, you can now automatically record your observers with ActiveRecord instead of having to add them individually in environment.rb:

config.active_record.observers = Dir.glob("#{RAILS_ROOT}/app/observers/*.rb").collect do |filename|
One issue with this reorganization is that the generators will continue to create mailers, observers, and sweepers in app/models. For me, that is not a big deal – I don’t create them often, and manually move them after generation. I suspect it would be trivial to adjust the generators to use the new path, but have not looked in to it.