tylerhunt / find-param forked from jseifer/find-param

The simplest and most pointless rails plugin ever.

This URL has Read+Write access

Tyler Hunt (author)
Thu May 29 13:35:15 -0700 2008
commit  56089a231925226bd9bbd04c4a104b5533430fcd
tree    64c0d84dff5fe947b09d0ee9b07ff9a7f6ff62ea
parent  60c17dfec0c579c58fe9df045cbab87e33651d6f
name age message
file README Loading commit data...
file init.rb Thu May 29 11:13:48 -0700 2008 Added support for automatic initialization usin... [Tyler Hunt]
directory lib/
directory test/
README
Find-Param
==========

This is a simple plugin to let you easily define a parameter to use for
#to_param, and also define a finder to access that same parameter.

This plugin is most useful for situations where you want to override the usage
of ID as the default parameter value. Using a slug creates URLs that can be
friendlier for humans and bots alike.


Getting Started
---------------

Here we define the find param to be a slug:

  class Post < ActiveRecord::Base
    find_param :slug
  end

This will set the #to_param to be #slug and add a finder to the model called
#find_by_param that can be used in your controllers to fetch records using
params[:id].

  class PostController
    before_filter :find_post, :only => %(show edit update destroy)

    def find_post
      find_by_param(params[:id])
    end
  end


Options
-------

To have your find param automatically populated on record creation, use the
:initialize_with option.

  class Post < ActiveRecord::Base
    find_param :slug, :initialize_with => :title
  end

This will use the title property to populate the slug. By default this value is
lowercased and all whitespace and special characters are replaced by hyphens.
To specify your own formatted for the find param, there a :using option that
accepts a proc.

  class Post < ActiveRecord::Base
    find_param :slug, :initialize_with => :title, :using => Proc.new { |value| value.upcase }
  end

You can also pass :raise_on_not_found => true to have it raise an
ActiveRecord::RecordNotFound erorr when the result set is empty.