Showing posts with label gems. Show all posts
Showing posts with label gems. Show all posts

Wednesday, September 7, 2016

Inheriting Attributes in a Tree Data Structure

I just published a new gem, inherited-attributes, for Active Record that works with the Ancestry gem to provide a tree data structure the ability to inherit attributes from their parent node, or farther up the tree.  We've been using this technique for a long time to support configuring our multi-tenant application.

Once the gem is installed, its very simple to configure:




From there, you can access the effective attributes which look up the tree ancestry to find a value to inherit.




There are more options and examples in the gem, including has-one relationships, default values and support for enumerations.

We've found it helpful and writing a gem made this code much easier to test.  What code do you have that would be easier to test as a gem or would be useful to others?

Wednesday, May 8, 2013

How to write a simple Rails gem

Today, I wrote my first Ruby on Rails gem.  It was a very simple refactoring of our code that I undertook when I needed to add the same functionality to a new model and I decided to do it as a gem instead of keeping it within our project.  I wanted to see how it worked and this is what I ended up with.  You can follow the Rails Guide, but it didn't cover everything I wanted to do.