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
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