Links
Configurator
Categories
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.
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'
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' } }
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.
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'
Setup
To setup, you really just need to create the config table:
class AddConfigTable < ActiveRecord::Migration
def self.up
create_table :config do |t|
t.references :associated, :polymorphic => true
t.string :namespace
t.string :key, :limit => 40, :null => false
t.string :value
end
end
def self.down
drop_table :config
end
end
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.
Copyright © 2008 Brennan Dunn, released under the MIT license
Vitals
| Home | http://www.brennandunn.com |
|---|---|
| Repository | http://github.com/brennandunn/configurator/tree/master |
| License | Rails' (MIT) |
| Tags |
acts_as_config config
|
| Rating | (5 votes) |
| Owner | Brennan Dunn |
| Created | 4 May 2008 |

