class ActiveSettings::Config

Attributes

definition[R]

Public Class Methods

[](key) click to toggle source

Requesting a config item:

key = ActiveSettings.detail['key']
# File lib/active_settings/config.rb, line 71
def [](key)
  if table_exists?
    unless ActiveSettings::Config.cache_exists?
      ActiveSettings::Config.initialize_cache
    end
    Rails.cache.read('ActiveSettings::Config')[key]
  end
end
[]=(key, value) click to toggle source

The usual way to use a config item:

ActiveSettings.detail['key'] = value
# File lib/active_settings/config.rb, line 84
def []=(key, value)
  if table_exists?
    setting = find_or_initialize_by_key(key)
    setting.value = value
  end
end
cache_clear!() click to toggle source
# File lib/active_settings/config.rb, line 100
def cache_clear!
  initialize_cache
end
cache_exists?() click to toggle source
# File lib/active_settings/config.rb, line 104
def cache_exists?
  true if Rails.cache.read('ActiveSettings::Config')
end
clear_definitions!() click to toggle source
# File lib/active_settings/config.rb, line 190
def clear_definitions!
  ActiveSettings.config_definitions = {}
end
configure() { |self| ... } click to toggle source
# File lib/active_settings/config.rb, line 108
def configure(&block)
  yield self
end
define(key, options={}) click to toggle source

Declares a setting definition that will constrain and support the use of a particular config entry.

define('setting.key', options)

Can take several options:

  • :default is the value that will be placed in the database if none has been set already

  • :type can be :string, :boolean or :integer. Note that all settings whose key ends in ? are considered boolean.

  • :select_from should be a list or hash suitable for passing to options_for_select, or a block that will return such a list at runtime

  • :validate_with should be a block that will receive a value and return true or false. Validations are also implied by type or select_from.

  • :allow_blank should be false if the config item must not be blank or nil

  • :allow_change should be false if the config item can only be set, not changed. Add a default to specify an unchanging config entry.

  • :allow_display should be false if the config item should not be showable in radius tags

From the main ActiveSettings config/initializers/active_settings.rb:

ActiveSettings.detail.configure do |config|
  config.define 'defaults.locale', :select_from => lambda {Layout.all.map{|l| [l.name, l.d]}}, :allow_blank => true
  config.define 'defaults.page.parts', :default => "Body,Extended"
  ...
end

It’s also possible to reuse a definition by passing it to define:

choose_layout = ActiveSettings::Config::Definition.new(:select_from => lambda {Layout.all.map{|l| [l.name, l.d]}})
define "my.layout", choose_layout
define "your.layout", choose_layout

but at the moment that’s only done in testing.

# File lib/active_settings/config.rb, line 156
      def define(key, options={})
        called_from = caller.grep(/\/initializers\//).first
        if options.is_a? ActiveSettings::Config::Definition
          definition = options
        else
          key = [options[:prefix], key].join('.') if options[:prefix]
        end

        raise LoadError, %{
Config definition error: '#{key}' is defined twice:
1. #{called_from}
2. #{definitions[key].definer}
        } unless definitions[key].nil? || definitions[key].empty?

        definition ||= ActiveSettings::Config::Definition.new(options.merge(:definer => called_from))
        definitions[key] = definition

        if self[key].nil? && !definition.default.nil?
          begin
            self[key] = definition.default
          rescue ActiveRecord::RecordInvalid
            raise LoadError, "Default configuration invalid: value '#{definition.default}' is not allowed for '#{key}'"
          end
        end
      end
definition_for(key) click to toggle source
# File lib/active_settings/config.rb, line 186
def definition_for(key)
  definitions[key] ||= ActiveSettings::Config::Definition.new(:empty => true)
end
definitions() click to toggle source
# File lib/active_settings/config.rb, line 182
def definitions
  ActiveSettings.config_definitions
end
initialize_cache() click to toggle source
# File lib/active_settings/config.rb, line 95
def initialize_cache
  Rails.cache.write('ActiveSettings::Config', ActiveSettings::Config.to_hash)
  Rails.cache.silence!
end
namespace(prefix, options = {}, &block) click to toggle source

A convenient drying method for specifying a prefix and options common to several settings.

ActiveSettings.detail.configure do |config|
  config.namespace('secret', :allow_display => false) do |secret|
    secret.define('identity', :default => 'batman')      # defines 'secret.identity'
    secret.define('lair', :default => 'batcave')         # defines 'secret.lair'
    secret.define('longing', :default => 'vindication')  # defines 'secret.longing'
  end
end
# File lib/active_settings/config.rb, line 122
def namespace(prefix, options = {}, &block)
  prefix = [options[:prefix], prefix].join('.') if options[:prefix]
  with_options(options.merge(:prefix => prefix), &block)
end
to_hash() click to toggle source
# File lib/active_settings/config.rb, line 91
def to_hash
  Hash[ *find(:all).map { |pair| [pair.key, pair.value] }.flatten ]
end

Public Instance Methods

boolean?() click to toggle source

Returns true if the item key ends with ‘?’ or the definition specifies :type => :boolean.

# File lib/active_settings/config.rb, line 250
def boolean?
  definition.boolean? || self.key.ends_with?('?')
end
checked?() click to toggle source

Returns true if the item is boolean and true.

# File lib/active_settings/config.rb, line 256
def checked?
  return nil if self[:value].nil?
  boolean? && self[:value] == 'true'
end
selected_value() click to toggle source

Returns a name corresponding to the current setting value, if the setting definition includes a select_from parameter.

# File lib/active_settings/config.rb, line 269
def selected_value
  definition.selected(value)
end
selector?() click to toggle source

Returns true if the item defintion includes a :select_from parameter that limits the range of permissible options.

# File lib/active_settings/config.rb, line 263
def selector?
  definition.selector?
end
update_cache() click to toggle source
# File lib/active_settings/config.rb, line 273
def update_cache
  ActiveSettings::Config.initialize_cache
end
validate() click to toggle source
# File lib/active_settings/config.rb, line 279
def validate
  definition.validate(self)
end
value() click to toggle source

Requesting a config item:

key = ActiveSettings.detail['key']

is equivalent to this:

key = ActiveSettings::Config.find_or_create_by_key('key').value

If the config item is boolean the response will be true or false. For items with :type => :integer it will be an integer, for everything else a string.

# File lib/active_settings/config.rb, line 233
def value
  if boolean?
    checked?
  else
    self[:value]
  end
end
value=(param) click to toggle source

The usual way to use a config item:

ActiveSettings.detail['key'] = value

is equivalent to this:

ActiveSettings::Config.find_or_create_by_key('key').value = value

Calling value= also applies any validations and restrictions that are found in the associated definition. so this will raise a ConfigError if you try to change a protected config entry or a RecordInvalid if you set a value that is not among those permitted.

# File lib/active_settings/config.rb, line 208
def value=(param)
  newvalue = param.to_s
  if newvalue != self[:value]
    raise ConfigError, "#{self.key} cannot be changed" unless settable? || self[:value].blank?
    if boolean?
      self[:value] = (newvalue == '1' || newvalue == 'true') ? 'true' : 'false'
    else
      self[:value] = newvalue
    end
    self.save!
  end
  self[:value]
end