Every repository with this icon (
Every repository with this icon (
| name | age | message | |
|---|---|---|---|
| |
MIT-LICENSE | Mon May 05 13:25:55 -0700 2008 | [Brennan Dunn] |
| |
README.rdoc | Sat May 31 03:59:29 -0700 2008 | [Brennan Dunn] |
| |
Rakefile | Sun May 04 12:41:13 -0700 2008 | [Brennan Dunn] |
| |
init.rb | Sun May 04 12:41:13 -0700 2008 | [Brennan Dunn] |
| |
install.rb | Sun May 04 12:41:13 -0700 2008 | [Brennan Dunn] |
| |
lib/ | Wed Jun 11 12:01:26 -0700 2008 | [ncr] |
| |
tasks/ | Sun May 04 12:41:13 -0700 2008 | [Brennan Dunn] |
| |
test/ | Wed Jun 11 12:01:26 -0700 2008 | [ncr] |
| |
uninstall.rb | Sun May 04 12:41:13 -0700 2008 | [Brennan Dunn] |
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.
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
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




