finder_filter gem released

I’m at the Ruby Hoedown in Huntsville this weekend. Being around so many brilliant geeks encouraged me to release a gem I’ve had sitting in the hopper for several weeks.

finder_filter encapsulates a pattern I find myself using quite frequently in Rails. Namely, looking up an instance variable before an action. For example:

class UsersController < ActionController::Base
  before_filter :find_user, :only => [:show, :edit]
  def show
    # do something with @user
  def edit
    # do something with @user
  def find_user
    @user = User.find(params[:id)

Sticking the finder in a before filter keeps the code DRY, but it still takes several lines to do this. finder_filter reduces this to a single line of code:

class UsersController < ActionController::Base
  finder_filter :only => [:show, :edit]
  def show; end
  def edit; end

There are other options to customize the column and param used in the lookup. Check out the README for full details.

To install the gem as a plugin in your Rails project:

sudo gem install pelargir-finder_filter --source=

Then open environment.rb in your Rails app and add the gem as a dependency in your initializer block: do |config| 
  config.gem "pelargir-finder_filter"

If you have any comments or suggestions, I’d love to hear from you. Contact me through the finder_filter project on GitHub.

3 thoughts on “finder_filter gem released

  1. Pingback: A Fresh Cup » Blog Archive » Double Shot #266

  2. Pingback: Scott Motte » Blog Archive » DRYing up your controllers in rails with find_filter plugin

  3. def load_objects
    params.match(/id$/).each do |k,v|
    the_model_name = (k.to_sym == :id) ? self.class.model_name : k.to_s.gsub(/_id/, '')
    instance_variable_set(the_model_name.to_sym.iv, the_model_name.classify.constantize.find(v))
    rescue ActiveRecord::RecordNotFound
    redirect_to root_url
    rescue NoMethodError, NameError

    Been using this for a while. Just set it as a before filter in application controller. It always does the right thing.

Comments are closed.