GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Description: Settings is a plugin that makes managing a table of global key, value pairs easy. Think of it like a global Hash stored in your database, that uses simple ActiveRecord like methods for manipulation. Keep track of any global setting that you don't want to hard code into your rails app. You can store any kind of object. Strings, numbers, arrays, or any serializable object.
Clone URL: git://github.com/Squeegy/rails-settings.git
squeegy (author)
Wed Jul 05 15:18:57 -0700 2006
commit  3675f67342754ade4209b9a27a4b95cef258213c
tree    62ae0a16f8380c315555235b5b863950e54b2f00
parent  6292e9333a2514c9a93c64016b8a7d0748333e68
name age message
file MIT-LICENSE Fri Feb 24 17:28:57 -0800 2006 git-svn-id: http://beautifulpixel.com/svn/plugi... [squeegy]
file README Wed Apr 19 00:51:42 -0700 2006 Added rdoc to Settings [squeegy]
directory doc/ Wed Apr 19 00:51:42 -0700 2006 Added rdoc to Settings [squeegy]
directory generators/ Fri Mar 03 10:38:40 -0800 2006 added a migration generator [squeegy]
file init.rb Fri Feb 24 16:16:04 -0800 2006 Settings plugin, initial commit [squeegy]
directory lib/ Wed Jul 05 15:18:57 -0700 2006 Fixed bug with "Settings.destroy(:foo)&quo... [squeegy]
directory test/ Tue Apr 18 16:18:07 -0700 2006 Fixed tests [squeegy]
README
= Settings Plugin

Settings is a plugin that makes managing a table of global key, value pairs easy.
Think of it like a global Hash stored in you database, that uses simple ActiveRecord
like methods for manipulation.  Keep track of any global setting that you dont want
to hard code into your rails app.  You can store any kind of object.  Strings, numbers,
arrays, or any object.

== Setup

You must create the table used by the Settings model.  Simply run this command:
  ruby script/generate settings_migration

Now just put that migration in the database with:
  rake migrate


== Usage

The syntax is easy.  First, lets create some settings to keep track of:

  Settings.admin_password = 'supersecret'
  Settings.date_format    = '%m %d, %Y'
  Settings.cocktails      = ['Martini', 'Screwdriver', 'White Russian']
  Settings.foo            = 123

Now lets read them back:

  Settings.foo            # returns 123

Changing an existing setting is the same as creating a new setting:

  Settings.foo = 'super duper bar'

Decide you dont want to track a particular setting anymore?

  Settings.destroy :foo
  Settings.foo            # Now gives a setting variable not found error.

Want a list of all the settings?

  Settings.all            # returns {'admin_password' => 'super_secret', 'date_format' => '%m %d, %Y'}

Set defaults for certain settings of your app.  This will cause the defined settings to return with the
Specified value even if they are not in the database.  Here is what you insert into your environment.rb

  module SettingsDefaults
    DEFAULTS = {
      :setting_one => 'footastic',
      :setting_two => 123.321
    }
  end
  
  Settings.setting_one          #=> returns "footastic" even though no record is in the databse for "some_setting"
  Settings.setting_one = 'bar'  # Database record is now created and 'bar' will be used instead of the default.

NOTE: the server must be restarted in order to see new default settings.

All there is to it!. Enjoy!