As I use Emacs for more programming-related tasks, I search for equivalents of some of my Vim plugins. Rails support is getting more important as I have some Rails applications that need maintenance.

The plugin vim-rails is one of the best things I’ve encountered while developing Rails applications. It gives a lot of useful commands and features and I’ve dearly missed this plugin while trying to develop Rails applications with Emacs.

One of the best features is the ability to type :Rcontroller and vim-rails shows me all the available controllers to jump to. Similar functions exist for almost every aspect of an Rails application.

Rinari seems to fit into the niche of vim-rails as it offers a similar featureset. If rinari is activated, one can jump to different parts of the Rails application with some quick keystrokes. Selecting a controller is as easy as typing C-c ; f c and you will be presented a list of all the controllers in your Rails application. So far, so good until now. But rinari has one “drawback” if you come from vim-rails. Vim-rails keeps being active if you edit any file inside of the Rails application, rinari does not. I had to activate rinari every time I started to edit a view or something else. This bugged me to no end.

But I found a solution to this with the help of #emacs@irc.freenode.net (Hello guys! You rock!).

The elisp function

locate-dominating-file FILE NAME

searches from FILE on upwards inside of all parent directories for NAME. If NAME is found, it returns the name of the directory where it found NAME. Great! Now how to automate this behaviour?

Simple:


#!cl
(require 'rinari)
(defun enable-rinari-hook()
(if
(and
(locate-dominating-file default-directory "config/application.rb")
(locate-dominating-file default-directory "Gemfile")
(locate-dominating-file default-directory "Rakefile")
(locate-dominating-file default-directory "db")
(locate-dominating-file default-directory "app"))
(rinari-launch)))
(add-hook 'find-file-hook 'enable-rinari-hook)

This hook searches for some files which typically appear alltogether in a new Rails application. If all of these are found, we are somewhere in the Rails root directory and can enable rinari safely and automatically. Due to the nature of

(and ...)

we do not continue checking for the remaining files if one of the files is not found.

Files can also be folders, as seen in the example (“db”). This hook will be called everytime we open a new file and now we are done. Works great for me.