brennandunn / configurator

Fatten your models with key/value pairs

This URL has Read+Write access

name age message
file .gitignore Mon Dec 14 07:35:39 -0800 2009 Another gem 'configurator' already exists. Pref... [brennandunn]
file MIT-LICENSE Mon May 05 13:25:55 -0700 2008 Modified license [Brennan Dunn]
file README.rdoc Mon Dec 14 07:35:39 -0800 2009 Another gem 'configurator' already exists. Pref... [brennandunn]
file Rakefile Fri Dec 11 14:02:24 -0800 2009 Converted from plugin to gem; Updated RakeFile ... [kb]
file VERSION Mon Dec 14 07:29:32 -0800 2009 Prepare for gemcutter [brennandunn]
file configurator.gemspec Mon Dec 14 07:35:39 -0800 2009 Another gem 'configurator' already exists. Pref... [brennandunn]
directory generators/ Fri Dec 11 14:03:53 -0800 2009 Added generator for config table; [kb]
directory lib/ Mon Dec 14 07:26:35 -0800 2009 Update README [brennandunn]
directory tasks/ Sun May 04 12:41:13 -0700 2008 Just off the plane. Lacks tests and some refine... [Brennan Dunn]
directory test/ Wed Feb 18 07:33:44 -0800 2009 Added data_type column, which will allow for re... [brennandunn]
README.rdoc

Configurator

Expalanation

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
  end

Basics

Now you can do things like:

  @user.config[:receive_email_alerts?] = true

or

  @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' }

Namespaces

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' }
  end

  @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!'

Globally

  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.

Setup

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.

Contributors

Brennan Dunn, Kyle Bolton, Jacek Becela

Copyright © 2009 Brennan Dunn, released under the MIT license