rails/rails — pull request #8189 by georgebrock (2012-11-12 17:19:01+00:00)
Title: Move multi-parameter attributes from ActiveRecord to ActionController

Multi-parameter attributes (used to build a complex type like a `Date` from several simple values that can be easily POSTed) are currently implemented in `ActiveRecord::AttributeAssignment`. This is pretty frustrating if you're trying to use an `ActiveModel::Model` with `Date` or `DateTime` fields.

I've fixed this by moving multi-parameter conversion to the `ActionController::Parameters` class, so it happens before the value hits the model. IMO the model layer shouldn't need to be aware of the format that's used to pass the data from the client to the server.

To support this the type that the values should be re-assembled into is included in the form (e.g. ``) instead of being determined from the model schema.

The main aim was to get multi-parameter attributes working with `ActiveModel`, but a nice side effect of this implementation is that it becomes very easy to register custom types. For example, you could easily submit an amount of money as a currency and an amount and have it converted to your own custom `Money` class:

# In an initializer somewhere:
ActionController::MultiParameterConverter.register_type('Money') { |currency, amount|
Money.new(currency, amount)
}

# View:

This isn't a finished implementation yet. It needs documentation, and the `ActiveRecord` implementation needs to be deprecated or removed. I've added multi-parameter support to `ActionController` and updated the `date_select`, `datetime_select` and `time_select` helpers to use the new implementation. I figured this was far enough to get some feedback.