Home > simply_settings

simply_settings

Simply_settings is a project mainly written in Ruby, based on the MIT license.

SimplySettings is a Rails plugin providing a relatively simple settings system for your application

SimplySettings

SimplySettings is a Rails plugin providing a relatively simple settings system for your application.

Setting are created like this:

Setting.create(:name => 'Comments enabled?', :slug => 'comments_enabled', :value => '1')

They are automatically converted to instance variables, like this: @comments_enabled (Boolean type in this case).

Requirements

Rails version 2.3+

Gem dependencies: redgreen (for testing) [OPTIONAL] sqlite3-ruby (for testing)

Installation

git submodule add git://github.com/pjg/simply_settings.git vendor/plugins/simply_settings git commit -m "Add simply_settings plugin as a submodule"

script/generate model setting class Setting < ActiveRecord::Base acts_as_setting end

db/migrate/xxxxxxxxxxxxxx_create_settings.rb create_table "settings" do |t| t.string "name" t.string "slug" t.string "value" end

add_index :settings, :slug, :unique => true

Add some settings:

Setting.create(:name => 'Comments enabled?', :slug => 'comments_enabled', :value => '1')

settings slugs must have /[a-z_]+/ format

Type of setting is based on the 'slug' field:

*_enabled, *_disabled, *_allowed
# boolean type assumed; automatically converted to Boolean ('0'/'1' in db)

*_value, *_limit, *_count
# numeric type assumed; automatically converted to Integer ('1231' in db)

*_text
# string type assumed; automatically converted to String ('Exemplary title' in db)

* (all others) - multiple-value custom setting assumed

Custom multiple-value (limited set) settings (rendered as with those four elements.

script/generate controller settings

The exemplary controller could look like this:

class SettingsController < ApplicationController before_filter :administrator_role_required layout 'administration'

def index
  edit_or_update_settings
end

end

And finally add a named route to process settings:

config/routes.rb map.settings '/ustawienia', :controller => :settings

The plugin comes with its own template form to change the settings (settings/index.html.erb).

Helpers

For each Boolean setting, there will be a helper with the '?' character suffixed. So for the 'registration_allowed' setting (slug), there will also be a registration_allowed? helper.

Filters

You can create your own custom Rails filters (before_ etc.) using stored settings. Like this:

in application_controller.rb:

FILTER

def registration_allowed return if registration_allowed? flash[:alert] = 'Rejestracja jest obecnie wyłączona' redirect_to SimplyAuthenticate::Settings.default_redirect_to end

The above code is a snippet from the simply_authenticate plugin (http://github.com/pjg/simply_authenticate).

Testing

If you'd like to run the tests included within this plugin you must have a settings table created.

You also need to have sqlite3 installed:

aptitude install sqlite3 libsqlite3-dev gem install sqlite3-ruby

Copyright (c) 2010 Paweł Gościcki, released under the MIT license

Previous:as3kiss