Pre-processing / normalizing parameters in Rails
Bruce Williams tries to solve the problem of multiple parameter types in Rails. For example, an action may accept dates via a parameter, but dates may be supplied in many forms. A 'date' parameter might arrived as if from a date_select helper, or might even be typed in directly by a user, or be pulled from a database. Rather than use before_filters to check parameters and normalize them, Bruce suggests that it should be possible to add basic conversion tools to certain data types so that all data is normalized by the time it hits your controllers.
Bruce provides a simple example to demonstrate how two different types of date could be prenormalized:
ActionController::Base.add_param_type 'date' do |value| case value when String Date.parse(value) when Hash Date.new(Integer(value['year']), Integer(value['month']), Integer(value['day'])) end end
His ideas are available in a plugin called param_types. Learn more about how to install and use it in his blog post.