Browse the Ruby on Rails Community.

You are here: Browse Railsplugins Param Types

Param Types

Credits = Author

Bruce Williams (http://codefluency.com)

=== Contributers

  • Gabriel Gironda, bug fixes
Description

Adds custom param type preprocessing.

In your environment.rb (or elsewhere), add your custom param types:

If you wanted to handle conversion to Dates, you might do something like:

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

Here we're registering a new param type 'date', and giving it a block to convert values. The block will be yielded the raw value of any incoming parameter matching the type you've defined, and the value returned by the block will be the new value of the parameter.

= How Types are Defined in Requests

Param types are defined by the use of parenthesis. For example:

<%= text_field_tag 'birthday(date)' %>

This defines a parameter, ‘birthday’, with the param type ‘date’. Since we’ve defined a ‘date’ param type in the controller, we’ll end up with params[:birthday] (converted to a Date by our block) rather than a plain params[“birthday(date)”] String.

It works with nesting, too.

<%= text_field_tag 'student[birthday(date)]' %>

In this case, you’d end up with params:student, as you might expect.

=== Complex Values

The ‘date’ example above could also handle a Hash of date components, like:

<%= text_field_tag 'birthday(date)[day]' %>
  <%= text_field_tag 'birthday(date)[month]' %>
  <%= text_field_tag 'birthday(date)[year]' %>
(or as selects, of course)

... and it will put the date together for you.

=== Really Complex Values

The params hash is transformed depth-first, so the values passed to your conversion Procs will already have been transformed. Here’s an example.

Let’s add a class (this could be a model, too)

class Birthday attr_accessor :name, :date def initialize(name,date) @name, @date = name, date end end

And a param type to create them:

ActionController::Base.add_param_type ‘birthday’ do |value| Birthday.new(value[‘name’], value[‘when’]) end

Now, if a query string like the following was passed:

bday(birthday)[name]=Bruce%20Williams&bday(birthday)[when(date)]=1980-02-01

params[:bday] would be a new Birthday instance, and the @date instance variable on the Birthday instance would be a Date object, not a String.

=== Unused Types

If an unused type is encountered (ie, there’s a parameter named “foo(bar)” and you haven’t designated a ‘bar’ param type), it’s passed through untouched.

== Questions/Ideas?

Questions and ideas can be directed to me (see http://codefluency.com/about-me for contact info). There’s some continuing development in the works (including integration with FormBuilder tags), but more ideas are always welcome!

NOTE: This description has been extracted from the Plugin README and so the formatting may need updating to make browser friendly

Users


See all details


Membership

+ Join this railsplugin

Record Maintainer

'None'