Settings Gem

This is improved from rails-settings, added caching for all settings. 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 dont want to hard code into your rails app. You can store any kind of object. Strings, numbers, arrays, or any object.



Edit your Gemfile:

# Rails 4.1.x
gem "rails-settings-cached", "~> 0.4.0"
# Rails 4+
gem "rails-settings-cached", "0.3.1"
# Rails 3.x
gem "rails-settings-cached", "0.2.4"

Generate your settings:

$ rails g settings <settings_name>

Note: If you migrating from gem rails-settings then make sure you have it in your model

class Settings < RailsSettings::CachedSettings

Now just put that migration in the database with:

rake db:migrate


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

Setting.admin_password = 'supersecret'
Setting.date_format    = '%m %d, %Y'
Setting.cocktails      = ['Martini', 'Screwdriver', 'White Russian']            = 123
Setting.credentials    = { :username => 'tom', :password => 'secret' }

Now lets read them back:            # returns 123

Changing an existing setting is the same as creating a new setting: = 'super duper bar'

For changing an existing setting which is a Hash, you can merge new values with existing ones:

Setting.merge!(:credentials, :password => 'topsecret')
Setting.credentials    # returns { :username => 'tom', :password => 'topsecret' }

Decide you dont want to track a particular setting anymore?

Setting.destroy :foo            # returns nil

Want a list of all the settings? ```ruby

Rails 4.1.x


Rails 3.x and 4.0.x


returns => 'super_secret', 'date_format' => '%m %d, %Y'

You need name spaces and want a list of settings for a give name space? Just choose your prefered named space delimiter and use `Setting.get_all` (`Settings.all` for # Rails 3.x and 4.0.x) like this:

Setting['preferences.color'] = :blue
Setting['preferences.size'] = :large
Setting['license.key'] = 'ABC-DEF'
# Rails 4.1.x
# Rails 3.x and 4.0.x
# returns { 'preferences.color' => :blue, 'preferences.size' => :large }

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. Make a new file in config/initializers/default_settings.rb with the following:

Setting.defaults[:some_setting] = 'footastic'
Setting.where(:var => "some_setting").count
=> 0
=> "footastic"

Init default value in database, this has indifferent with Setting.defaults[:some_setting], this will save the value into database:

Setting.save_default(:some_key, "123")
Setting.where(:var => "some_key").count
=> 1
=> "123"

Settings may be bound to any existing ActiveRecord object. Define this association like this: Notice! is not do caching in this version.

class User < ActiveRecord::Base
  include RailsSettings::Extend

Then you can set/get a setting for a given user instance just by doing this:

user = User.find(123)
user.settings.color = :red
user.settings.color # returns :red
# Rails 4.1.x
# Rails 3.x and 4.0.x
# { "color" => :red }

If you want to find users having or not having some settings, there are named scopes for this:

# => returns a scope of users having any setting

# => returns a scope of users having a 'color' setting

# returns a scope of users having no setting at all (means user.settings.get_all == {})

# returns a scope of users having no 'color' setting (means user.settings.color == nil)

Settings maybe dynamically scoped. For example, if you're using apartment gem for multitenancy, you may not want tenants to share settings:

class Settings < RailsSettings::CachedSettings
  cache_prefix { Apartment::Tenant.current }

How to create a list, form to manage Settings?

If you want create an admin interface to editing the Settings, you can try methods in follow:

class SettingsController < ApplicationController
  def index
    # to get all items for render list
    @settings = Setting.unscoped

  def edit
    @setting = Setting.unscoped.find(params[:id])

Also you may use rails-settings-ui gem for building ready to using interface with validations.