Fatten your models with key/value pairs
Pull request Compare This branch is even with brennandunn:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.




Unleash your models and quickly and easily annotate anything. Store booleans, strings, or optionally serialized objects (hashes, custom classes, whatever) without extra migrations.

Configurator is meant to store basic things. There's no easy way to query for models that match a particular criteria, so don't go overboard. This satisfies some of my needs, but I don't recommend relying on it as a replacement for traditional model fields.

Want your users to be able to define custom settings?

class User < ActiveRecord::Base
  include Configurator


Now you can do things like:

@user.config[:receive_email_alerts?] = true


@user.config[:notification_address] = 'user@gmail.com'

You can set configurations to either instances or classes. Setting key/value pairs to classes is especially useful for setting up application-wide settings.

Think of it as just a giant hash.

@user.config = { :favorite_animal => 'dog', :favorite_color => 'blue' }


Support for one level of namespacing:

@user.config[:animals, :favorite] = 'cat'

Namespaces within hash assignments:

@user.config = { :animals => { :favorite => 'cat', :likes_elephants? => true }, :artists => { :favorite => 'Radiohead' } }

Querying namespaces:

@user.config = { :animals => { :cat => 'Toby', :dog => 'Gabby' } }
@user.config.namespace(:animals)  # => { :cat => 'Toby', :dog => 'Gabby' }

Form support

Easy to use in views:

<% fields_for :config, @user.config do |c| %>

  <%= c.select :favorite_color, %w(red green blue) %>

<% end %>

Default Options

Databases don't come filled, so there's an easy way to set defaults on your models. The default values will be added to the config table used by the plugin when loaded.

class User < ActiveRecord::Base
  include Configurator

  default_configuration :favorite_color => 'red', :receive_email_alerts? => true, :salary => { :default_for_manager => '$55,000', :default_for_employee => '$25,000' }

@user.config[:favorite_color] # => 'red'
@user.config[:favorite_color] = 'green'
@user.config[:favorite_color] # => 'green'

Global Settings

Sometimes you don't want to be restricted to configuring records, and would like to apply Configurator to a class or to something even more global. Well, now you can.

Per Model

User.config[:notification_email] = 'Welcome new user!'


Configurator[:default_notification_email] = 'Welcome to our website!'

Example of a database driven view layer:

<h1><%= Configurator[:login_page, :headline] %></h1>

<p><%= Configurator[:login_page, :username] %></p>
<%= text_field_tag :username %>

<p><%= Configurator[:login_page, :password] %></p>

Simply store the above values, and you're now able to quickly attach a form to those different values, and satisfy your clients need to have every single aspect of the application. I'm sure there are plugins that do just this, but this is an example of Configurators global reach.


First, install the gem. If you're using Rails:

config.gem 'ar-configurator', :lib => 'configurator'

Or just:

gem install configurator
  • Run the config_table generator

  • Migrate your database

  • Include Configurator into the models you need it in, and that's it.

If you need to be able to store complex objects, or strings greater than 255 characters, change the 'value' column to text. You can add to the ConfigurationHash class:

serialize :value

I haven't tried this yet, but it should work fine.


Brennan Dunn, Kyle Bolton, Jacek Becela

Copyright © 2009 Brennan Dunn, released under the MIT license