Make Any Ruby Object Feel Like ActiveRecord
Before I Begin, The ActiveModel API Before I begin, there are two major elements to ActiveModel. The first is the ActiveModel API, the interface that models must adhere to in order to gain compatibility with ActionPack’s helpers. I’ll be talking more about that soon, but for now, the important thing about the ActiveModel API is that your models can become ActiveModel compliant without using a single line of Rails code.
In order to help you ensure that your models are compliant, ActiveModel comes with a module called ActiveModel::Lint that you can include into your test cases to test compliance with the API:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
The ActiveModel::Lint::Tests provide a series of tests that are run against the @model, testing for compliance.
ActiveModel Modules
The second interesting part of ActiveModel is a series of modules provided by ActiveModel that you can use to implement common model functionality on your own Ruby objects. These modules were extracted from ActiveRecord, and are now included in ActiveRecord.
Because we’re dogfooding these modules, you can be assured that APIs you bring in to your models will remain consistent with ActiveRecord, and that they’ll continue to be maintained in future releases of Rails.
The ActiveModel comes with internationalization baked in, providing an avenue for much better community sharing around translating error messages and the like.
The Validations System
This was perhaps the most frustrating coupling in ActiveRecord, because it meant that people writing libraries for, say, CouchDB had to choose between painstakingly copying the API over, allowing inconsistencies to creep in, or just inventing a whole new API.
Validations have a few different elements.
First, declaring the validations themselves. You’ve seen the usage before in ActiveRecord:
1 2 3 |
|
To do the same thing for a plain old Ruby object, simply do the following:
1 2 3 4 5 6 7 8 9 10 |
|