This repository is private.
All pages are served over SSL and all pushing and pulling is done over SSH.
No one may fork, clone, or view it unless they are added as a member.
Every repository with this icon (
) is private.
Every repository with this icon (
This repository is public.
Anyone may fork, clone, or view it.
Every repository with this icon (
) is public.
Every repository with this icon (
commit 187a65224fc102626cdb86b1e0e3441951e15e2d
tree 9b2a8b82049b9009b5d21dc9ac43ab830acb5a8e
parent 151e34f642c7ca79029d8d7637e1bf35194e4508
tree 9b2a8b82049b9009b5d21dc9ac43ab830acb5a8e
parent 151e34f642c7ca79029d8d7637e1bf35194e4508
| name | age | message | |
|---|---|---|---|
| |
.gitignore | Sat Apr 26 20:10:57 -0700 2008 | |
| |
MIT-LICENSE | ||
| |
README | ||
| |
Rakefile | ||
| |
from_param.gemspec | ||
| |
init.rb | ||
| |
lib/ | ||
| |
rails/ | ||
| |
spec/ |
README
FromParam
=========
This plugin is an addition to ActiveRecord::Base that establishes a better
convention for finding records based on parameters. It adds a "from_param" class
method to ActiveRecord::Base as a convention for fetching a model from a URL
parameter. By default it will find a record based on the to_i of the passed in
parameter, but storing a parameter in a column is where it becomes especially useful.
If you create a 'param' column in your table (or any other column using set_param_column),
the to_param of your record will automatically be saved to that column whenever
you save the record, and you will be able to retrieve it using just a simple
Model.from_param call with the generated to_param. It is probably wise to add an index
to the param column if you use one.
It's time to move away from generated-key-dependence, and this plugin is an attempt
to make that easy, painless, and work easily within the existing systems.
Example
=======
# default behavior
class User < ActiveRecord::Base
def to_param
"#{id}-#{first_name.downcase}-#{last_name.downcase}"
end
end
class UsersController < ApplicationController
# GET /users/1-bobby-mcfarin
def show
@user = User.from_param(params[:id]) # => <User id=1 first_name="Bobby" last_name="McFarin">
end
end
# using a 'param' column, in this case 'slug'. Note the use of String.urlize to make the title URL-friendly.
# On Rails 2.2+, String.urlize is an alias for Rails built-in Inflector.parameterize.
class Post < ActiveRecord::Base
set_param_column "slug" # defaults to "param"
def to_param
"#{created_at.strftime("%Y-%m-%d")}-#{title.urlize}"
end
end
class PostsController < ApplicationController
# GET /posts/2008-04-26-from-param-plugin-released
@post = Post.from_param(params[:id]) # => <Post title="From Param: Plugin Released" created_at="2008-04-26">
end
Note: You will need to have the rspec gem installed to run the tests.
Resources
=========
GitHub: http://github.com/mbleigh/from_param
Lighthouse: http://mbleigh.lighthouseapp.com/projects/10478-from-param
Copyright (c) 2007 Michael Bleigh (http://mbleigh.com/) and Intridea Inc. (http://intridea.com/), released under the MIT
license








