Today I created and released my first gem: attribute_delegator. It's basically a small plugin for ActiveRecord that generates getter/setter methods that allow you to treat the attributes of a has_one relationship like they are native to another model. It also makes sure that any changes to the has_one model are seamlessly saved.

The main code of them started off as a mixin in Vox Media's CMS application. We have few particularly large (column and row wise) STI tables, and wanted to limit their bloat. Specifically, we have a large 'entries' table, and many different subtypes of entry. However, different subtypes sometimes require additional fields, but I wanted to avoid the classic problem of ending up with unused columns for certain subtypes.

Adding these subtype specific fields to a different model is nothing new, but I also didn't want to put complicated logic into our story editor (in essence, a massive form) to deal with these fields. Attribute delegator allows us to treat these fields the same as any other in our form though, which is a big win.

At risk of duplicating documentation, check out the source/readme on Github for more info.

PS: Big thanks to Bundler gem development guide for making the publication process really easy!

 


Need help building a new web product or scaling your current one? I'm available for freelance work.