Method: ActiveSettings::Config.define

Defined in:
lib/active_settings/config.rb

.define(key, options = {}) ⇒ Object

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.

Raises:

  • (LoadError)


156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# 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