Synthesis
An easy way to add common finders to your Rails controllers.
Installation
gem sources -a http://gems.github.com (you only have to do this once)
sudo gem install pelargir-finder_filter
To use with a Rails 2.1 app, edit the environment.rb file and add an entry for the gem to the Rails::Initializer config block.
Usage
finder_filter is intended to replace one-off before filters that you might commonly write in your Rails controllers. For example:
class UsersController < ActionController::Base
before_filter :find_user, :only => [:show, :edit]
def show
# do something with @user
end
def edit
# do something with @user
end
def find_user
@user = User.find(params[:id)
end
end
finder_filter reduces this pattern to a single line:
class UsersController < ActionController::Base
finder_filter :only => [:show, :edit]
def show; end
def edit; end
end
Or, if you want to specify the model to find:
class UsersController < ActionController::Base
finder_filter :user, :only => [:show, :edit]
def show; end
def edit; end
end
To find based on a column other than ID:
finder_filter :user, :by => :name
# equivalent to:
# @user = User.find_by_name(params[:id])
To find based on a param other than ID:
finder_filter :user, :param => :permalink
# equivalent to:
# @user = User.find(params[:permalink])
The standard Rails :only and :except options can also be used:
before_filter :find_user, :only => [:show, :edit]
before_filter :find_user, :except => [:index]
Tests
To run the tests:
rake test
Dependencies
actionpack > 2.0.1 mocha > 0.5.6 test-spec > 0.4.0
Git
git://github.com/pelargir/finder_filter.git
Contributors
Matthew Bass, Kevin Smith