public
Description: Rails plugin: allows for checking the params hash in a cleaner fashion by using methods instead of the hash directly.
Homepage: http://rpheath.com/posts/326-rails-plugin-has-params
Clone URL: git://github.com/rpheath/has_params.git
name age message
file MIT-LICENSE Wed Jun 18 08:52:17 -0700 2008 initial commit [rpheath]
file README.textile Wed Jun 18 14:24:37 -0700 2008 fixed typo in README [rpheath]
file Rakefile Wed Jun 18 08:52:17 -0700 2008 initial commit [rpheath]
file init.rb Wed Jun 18 08:52:42 -0700 2008 mixing into actioncontroller::base [rpheath]
file install.rb Wed Jun 18 08:52:17 -0700 2008 initial commit [rpheath]
directory lib/ Fri Jun 20 11:19:41 -0700 2008 Merge branch 'master' of git@github.com:rpheath... [rpheath]
directory spec/ Wed Jun 18 08:59:59 -0700 2008 added a few specs [rpheath]
directory tasks/ Wed Jun 18 08:52:17 -0700 2008 initial commit [rpheath]
file uninstall.rb Wed Jun 18 08:52:17 -0700 2008 initial commit [rpheath]
README.textile

HasParams

This plugin allows you to stop checking parameters via the params hash, in favor of a much cleaner alternative.

What’s the point?

Really, there is no point. And I wouldn’t be surprised if I were the only one to think this is worthy of using, but here it is anyway. Have you ever written a line of code similar to this?

Project.new(params[:project]) unless params[:project].blank?

To me, the params[:project].blank? part stinks up that line. So, I got rid of it. With has_params, you can do this instead:

Project.new(params[:project]) unless blank_project_params?

Much better.

Example(s)

You get a few ways to check for params with this plugin. Rather than explain them, I’ll just list the old way vs. the has_params way.

The “old way”


  User.new(params[:user]) unless params[:user].blank?
  User.new(params[:user]) if params[:user]

The “has_params way”


  User.new(params[:user]) unless blank_user_params?
  User.new(params[:user]) if has_user_params?
  User.new(params[:user]) if user_params?

Note: <name>_params? is nothing more than a shorter way to call has_<name>_params?.

Gotcha!

One thing to keep in mind. Under the covers, this plugin uses method_missing and parses out the crapola to get to the actual params key. So, if you’re using method_missing somewhere in your controller (application_controller perhaps?), if it’s at all possible, make sure that you call super at some point. Otherwise, this plugin won’t work ;-)

License

Copyright © 2008 Ryan Heath, released under the MIT license