Class: ActiveSettings::Config
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- ActiveSettings::Config
- Defined in:
- lib/active_settings/config.rb,
lib/active_settings/config/definition.rb
Defined Under Namespace
Classes: ConfigError, Definition
Instance Attribute Summary collapse
-
#definition ⇒ Object
readonly
Returns the definition associated with this config item.
Class Method Summary collapse
-
.[](key) ⇒ Object
Requesting a config item:.
-
.[]=(key, value) ⇒ Object
The usual way to use a config item:.
- .cache_clear! ⇒ Object
- .cache_exists? ⇒ Boolean
- .clear_definitions! ⇒ Object
- .configure {|_self| ... } ⇒ Object
-
.define(key, options = {}) ⇒ Object
Declares a setting definition that will constrain and support the use of a particular config entry.
- .definition_for(key) ⇒ Object
- .definitions ⇒ Object
- .initialize_cache ⇒ Object
-
.namespace(prefix, options = {}, &block) ⇒ Object
A convenient drying method for specifying a prefix and options common to several settings.
- .to_hash ⇒ Object
Instance Method Summary collapse
-
#boolean? ⇒ Boolean
Returns true if the item key ends with ‘?’ or the definition specifies :type => :boolean.
-
#checked? ⇒ Boolean
Returns true if the item is boolean and true.
-
#selected_value ⇒ Object
Returns a name corresponding to the current setting value, if the setting definition includes a select_from parameter.
-
#selector? ⇒ Boolean
Returns true if the item defintion includes a :select_from parameter that limits the range of permissible options.
- #update_cache ⇒ Object
- #validate ⇒ Object
-
#value ⇒ Object
Requesting a config item:.
-
#value=(param) ⇒ Object
The usual way to use a config item:.
Instance Attribute Details
#definition ⇒ Object (readonly)
Returns the definition associated with this config item. If none has been declared this will be an empty definition that does not restrict use.
244 245 246 |
# File 'lib/active_settings/config.rb', line 244 def definition @definition end |
Class Method Details
.[](key) ⇒ Object
Requesting a config item:
key = ActiveSettings.detail['key']
71 72 73 74 75 76 77 78 |
# 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) ⇒ Object
The usual way to use a config item:
ActiveSettings.detail['key'] = value
84 85 86 87 88 89 |
# 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! ⇒ Object
100 101 102 |
# File 'lib/active_settings/config.rb', line 100 def cache_clear! initialize_cache end |
.cache_exists? ⇒ Boolean
104 105 106 |
# File 'lib/active_settings/config.rb', line 104 def cache_exists? true if Rails.cache.read('ActiveSettings::Config') end |
.clear_definitions! ⇒ Object
190 191 192 |
# File 'lib/active_settings/config.rb', line 190 def clear_definitions! ActiveSettings.config_definitions = {} end |
.configure {|_self| ... } ⇒ Object
108 109 110 |
# File 'lib/active_settings/config.rb', line 108 def configure(&block) yield self end |
.define(key, options = {}) ⇒ Object
Declares a setting definition that will constrain and support the use of a particular config entry.
define('setting.key', )
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.
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, ={}) called_from = caller.grep(/\/initializers\//).first if .is_a? ActiveSettings::Config::Definition definition = else key = [[:prefix], key].join('.') if [: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(.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) ⇒ Object
186 187 188 |
# File 'lib/active_settings/config.rb', line 186 def definition_for(key) definitions[key] ||= ActiveSettings::Config::Definition.new(:empty => true) end |
.definitions ⇒ Object
182 183 184 |
# File 'lib/active_settings/config.rb', line 182 def definitions ActiveSettings.config_definitions end |
.initialize_cache ⇒ Object
95 96 97 98 |
# 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) ⇒ Object
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
122 123 124 125 |
# File 'lib/active_settings/config.rb', line 122 def namespace(prefix, = {}, &block) prefix = [[:prefix], prefix].join('.') if [:prefix] (.merge(:prefix => prefix), &block) end |
.to_hash ⇒ Object
91 92 93 |
# File 'lib/active_settings/config.rb', line 91 def to_hash Hash[ *find(:all).map { |pair| [pair.key, pair.value] }.flatten ] end |
Instance Method Details
#boolean? ⇒ Boolean
Returns true if the item key ends with ‘?’ or the definition specifies :type => :boolean.
250 251 252 |
# File 'lib/active_settings/config.rb', line 250 def boolean? definition.boolean? || self.key.ends_with?('?') end |
#checked? ⇒ Boolean
Returns true if the item is boolean and true.
256 257 258 259 |
# File 'lib/active_settings/config.rb', line 256 def checked? return nil if self[:value].nil? boolean? && self[:value] == 'true' end |
#selected_value ⇒ Object
Returns a name corresponding to the current setting value, if the setting definition includes a select_from parameter.
269 270 271 |
# File 'lib/active_settings/config.rb', line 269 def selected_value definition.selected(value) end |
#selector? ⇒ Boolean
Returns true if the item defintion includes a :select_from parameter that limits the range of permissible options.
263 264 265 |
# File 'lib/active_settings/config.rb', line 263 def selector? definition.selector? end |
#update_cache ⇒ Object
273 274 275 |
# File 'lib/active_settings/config.rb', line 273 def update_cache ActiveSettings::Config.initialize_cache end |
#validate ⇒ Object
279 280 281 |
# File 'lib/active_settings/config.rb', line 279 def validate definition.validate(self) end |
#value ⇒ Object
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.
233 234 235 236 237 238 239 |
# File 'lib/active_settings/config.rb', line 233 def value if boolean? checked? else self[:value] end end |
#value=(param) ⇒ Object
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.
208 209 210 211 212 213 214 215 216 217 218 219 220 |
# 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 |