My first little gem has_attributes_from for merging attributes from one ActiveRecord Class to another individual STI subclass.
So why, do we want to do that, you might ask? Well, I was working on a rails project where clients take care of the administration (planning and billing) for child daycare centres. In this project I have all kinds of people objects, like a child, a contactperson, a father, mother, caretaker etc. etc. So, the ideal casus for a Single Table Inheritance implementation. So I implemented a ‘classic’ Person Class as follows:
1 2 3 4 5 6 7 8 9 |
|
However, I like to add certain extra attributes to a Child, like for example its nickname and information about its allergies. So I introduce another class which I call ChildDetail. Of course I can add these attributes to the people table as well, but in this project I had several more fields to add and some other attributes for a father., which would lead to a lot of columns for only two subclasses (of the five in total).
1 2 3 4 5 6 |
|
Ok, now I can access the extra attributes
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
This is not really the way I want it, I like to ask directly for the nickname of a child without going through a child_detail. So to solve this ‘problem’ I wrote the has_attributes_from gem. Add the following line to your environment.rb file
1
|
|
Install and unpack this gem to your vendor directory or install as a plugin
1
./script/plugin install git://github.com/dovadi/has_attributes_from.git
Now we can do the following:
1 2 3 4 5 6 7 8 |
|
With has_attributes_from the attributes from ChidDetail are merged with Child. A child object acts as one single object and I can even do validation on nickname directly (or the other attributes from ChildDetail).
1 2 3 4 5 6 7 8 9 10 11 12 |
|
I think this is much nicer, besides it was fun to make and a good exercise to put some Ruby meta programming into practice.