Tom Mango

ActiveConfiguration: A Generic Settings Store

ActiveConfiguration is a Rails engine that exposes a generic settings store to ActiveRecord models. Made for very configurable applications, it allows you to avoid implementing specific ways to store settings for each model that needs such a configuration. If your application isn’t very configurable, ActiveConfiguration isn’t what you want.

For example, if your application had a Category model that only had a configurable sort attribute, ActiveConfiguration would be overkill. Rather, you would just read and write values using a sort column and restrict the allowed values using something like validates_inclusion_of.

However, if your Category model was more flexible in its configuration, you may want a sort setting, a limit setting and multiple price_filter settings that can be configured by your end user. Without ActiveConfiguration, you would have to develop a way to store and validate these settings for this specific scenario. The sort and limit settings are simple but because price_filter can accept multiple rules, you’d have to set up an additional model. Still, this isn’t really an issue when you’re dealing with just a single configurable model. When you’re dealing with many, things tend to get messy.

With ActiveConfiguration, all of your settings, even for price_filter, can be stored in a generic way. ActiveConfiguration provides a place to store settings for each of your models and even handles validation when you restrict the allowed values or format of an option.

Source

The source for ActiveConfiguration is available at GitHub.

Installation

Add the following to your Gemfile:

Generate the migration for the settings table:

Migrate your database:

Example Configuration

After installing ActiveConfiguration, the #configure block is available to every ActiveRecord model. If the #configure block is defined with a valid configuration, additional methods are made available on the model.

Example Usage

Additional Notes

I accept that ActiveConfiguration’s concept is a bit out there, but I’ve found it very useful in certain circumstances and hope that others find it useful as well. I think the README provides all of the information necessary to get started using this gem, but there are additional details in the documentation.

If you have any questions or feedback, I’d love to hear from you. Please feel free to email me or open an issue at GitHub.