public
Description: An easy way to add common finders to your Rails controllers.
Homepage: http://matthewbass.com/2008/08/08/finder_filter-gem-released/
Clone URL: git://github.com/pelargir/finder_filter.git
Matthew Bass (author)
Sat Nov 08 09:39:10 -0800 2008
commit  38334c1226df858f91b09a1fffd48d55e19b62cf
tree    c0185f33753e9ad4abcee241e197212ef3275efc
parent  97551021cd85be72f109e3d0aba6a0ffba05665c
name age message
file .gitignore Fri Aug 08 19:42:26 -0700 2008 initial import [Matthew Bass]
file CHANGELOG Wed Aug 27 11:28:00 -0700 2008 Added automatic from_param support. Needs test ... [ffmike]
file MIT-LICENSE Sun Aug 10 11:24:29 -0700 2008 added license [Matthew Bass]
file README Sat Nov 08 09:39:10 -0800 2008 updated readme [Matthew Bass]
file Rakefile Fri Aug 08 19:42:26 -0700 2008 initial import [Matthew Bass]
directory db/ Thu Oct 30 13:13:20 -0700 2008 fixed tests and added sqlite3 dependency [Matthew Bass]
file finder_filter.gemspec Thu Oct 30 13:13:20 -0700 2008 fixed tests and added sqlite3 dependency [Matthew Bass]
file init.rb Thu Aug 14 18:33:16 -0700 2008 added init.rb [Matthew Bass]
directory lib/ Thu Oct 30 13:13:20 -0700 2008 fixed tests and added sqlite3 dependency [Matthew Bass]
directory test/ Thu Oct 30 13:13:20 -0700 2008 fixed tests and added sqlite3 dependency [Matthew Bass]
README
= Synthesis

An easy way to add common finders to your Rails controllers.

== Installation

Install the gem directly:

  sudo gem install pelargir-finder_filter --source=http://gems.github.com
  
Or install the gem in your Rails project:
  
  script/plugin install git://github.com/pelargir/finder_filter.git

Or clone the project:

  git clone git://github.com/pelargir/finder_filter.git

== 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 :person, :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])

You can specify that prepend_before_filter is used:

  finder_filter :user, :only => [:show, :edit], :prepend => true
    # generates:
    #    prepend_before_filter :find_user, :only => [:show, :edit]

The standard Rails :only and :except options can also be used:

  before_filter :find_user, :only => [:show, :edit]
  before_filter :find_user, :except => [:index]

== Resource Nesting

If your controller is a nested resource, you might want the find
to be performed on the parent model. For example:

  class PostsController < ActionController::Base
    before_filter :find_post

    def find_post
      @topic = Topic.find(params[:topic_id])
      @post = @topic.posts.find(params[:id])
    end
  end
  
This can be easily handled using finder_filter:

  finder_filter :nested => :topic

Nested resources will only do a find on the parent model if the parent id is supplied.
This allows you to handle routing setups like this:

map.resources :posts
map.resources :topics do |topic|
  topic.resources :posts
end

With this setup, both /posts/1 and /topics/1/posts/2 will be valid URLs, and will do the 
right thing if you include 

finder_filter :nested => :topic

in your Posts controller.

== from_param

If you have Michael Bleigh's from_param (http://github.com/mbleigh/from_param/tree/master)
installed, finder_filter will work transparently with it. This gives you the dual benefit
of SEO-friendly URLs and DRY controller code.

== Tests

To run the tests, you must have the mocha, test-spec, and sqlite3 gems installed.

  rake test

== Dependencies

actionpack > 2.0.1

== Author

  Matthew Bass
    email: pelargir at gmail dot com
    blog: http://matthewbass.com

== Contributors

Kevin Smith, Steve Mohapi-Banks