Class: Radiant::Config
- Defined in:
- app/models/radiant/config.rb,
lib/radiant/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
- .[]=(key, value) ⇒ Object
- .cache_file ⇒ Object
- .cache_file_exists? ⇒ Boolean
- .cache_path ⇒ Object
- .clear_definitions! ⇒ Object
- .default_settings ⇒ 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
- .ensure_cache_file ⇒ Object
- .initialize_cache ⇒ Object
-
.namespace(prefix, options = {}, &block) ⇒ Object
A convenient drying method for specifying a prefix and options common to several settings.
- .site_settings ⇒ Object
- .stale_cache? ⇒ Boolean
- .to_hash ⇒ Object
- .user_settings ⇒ 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.
272 273 274 |
# File 'app/models/radiant/config.rb', line 272 def definition @definition end |
Class Method Details
.[](key) ⇒ Object
77 78 79 80 81 82 83 84 85 86 |
# File 'app/models/radiant/config.rb', line 77 def [](key) if table_exists? unless Radiant::Config.cache_file_exists? Radiant::Config.ensure_cache_file Radiant::Config.initialize_cache end Radiant::Config.initialize_cache if Radiant::Config.stale_cache? Rails.cache.read('Radiant::Config')[key] end end |
.[]=(key, value) ⇒ Object
88 89 90 91 92 93 |
# File 'app/models/radiant/config.rb', line 88 def []=(key, value) if table_exists? setting = find_or_initialize_by_key(key) setting.value = value end end |
.cache_file ⇒ Object
124 125 126 |
# File 'app/models/radiant/config.rb', line 124 def cache_file cache_file = File.join(cache_path,'radiant_config_cache.txt') end |
.cache_file_exists? ⇒ Boolean
106 107 108 |
# File 'app/models/radiant/config.rb', line 106 def cache_file_exists? File.file?(cache_file) end |
.cache_path ⇒ Object
120 121 122 |
# File 'app/models/radiant/config.rb', line 120 def cache_path "#{Rails.root}/tmp" end |
.clear_definitions! ⇒ Object
218 219 220 |
# File 'app/models/radiant/config.rb', line 218 def clear_definitions! Radiant.config_definitions = {} end |
.default_settings ⇒ Object
132 133 134 |
# File 'app/models/radiant/config.rb', line 132 def default_settings @default_settings ||= %w{ defaults.locale defaults.page.filter defaults.page.parts defaults.page.fields defaults.page.status defaults.snippet.filter } 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 radiant config/initializers/radiant_config.rb:
Radiant.config do |config|
config.define 'defaults.locale', :select_from => lambda { Radiant::AvailableLocales.locales }, :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 = Radiant::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.
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'app/models/radiant/config.rb', line 184 def define(key, ={}) called_from = caller.grep(/\/initializers\//).first if .is_a? Radiant::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 ||= Radiant::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
214 215 216 |
# File 'app/models/radiant/config.rb', line 214 def definition_for(key) definitions[key] ||= Radiant::Config::Definition.new(:empty => true) end |
.definitions ⇒ Object
210 211 212 |
# File 'app/models/radiant/config.rb', line 210 def definitions Radiant.config_definitions end |
.ensure_cache_file ⇒ Object
115 116 117 118 |
# File 'app/models/radiant/config.rb', line 115 def ensure_cache_file FileUtils.mkpath(cache_path) FileUtils.touch(cache_file) end |
.initialize_cache ⇒ Object
99 100 101 102 103 104 |
# File 'app/models/radiant/config.rb', line 99 def initialize_cache Radiant::Config.ensure_cache_file Rails.cache.write('Radiant::Config',Radiant::Config.to_hash) Rails.cache.write('Radiant.cache_mtime', File.mtime(cache_file)) Rails.cache.silence! end |
.namespace(prefix, options = {}, &block) ⇒ Object
A convenient drying method for specifying a prefix and options common to several settings.
Radiant.config 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
150 151 152 153 |
# File 'app/models/radiant/config.rb', line 150 def namespace(prefix, = {}, &block) prefix = [[:prefix], prefix].join('.') if [:prefix] (.merge(:prefix => prefix), &block) end |
.site_settings ⇒ Object
128 129 130 |
# File 'app/models/radiant/config.rb', line 128 def site_settings @site_settings ||= %w{ site.title site.host dev.host local.timezone } end |
.stale_cache? ⇒ Boolean
110 111 112 113 |
# File 'app/models/radiant/config.rb', line 110 def stale_cache? return true unless Radiant::Config.cache_file_exists? Rails.cache.read('Radiant.cache_mtime') != File.mtime(cache_file) end |
.to_hash ⇒ Object
95 96 97 |
# File 'app/models/radiant/config.rb', line 95 def to_hash Hash[ *find(:all).map { |pair| [pair.key, pair.value] }.flatten ] end |
.user_settings ⇒ Object
136 137 138 |
# File 'app/models/radiant/config.rb', line 136 def user_settings @user_settings ||= ['user.allow_password_reset?'] end |
Instance Method Details
#boolean? ⇒ Boolean
Returns true if the item key ends with ‘?’ or the definition specifies :type => :boolean.
278 279 280 |
# File 'app/models/radiant/config.rb', line 278 def boolean? definition.boolean? || self.key.ends_with?("?") end |
#checked? ⇒ Boolean
Returns true if the item is boolean and true.
284 285 286 287 |
# File 'app/models/radiant/config.rb', line 284 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.
297 298 299 |
# File 'app/models/radiant/config.rb', line 297 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.
291 292 293 |
# File 'app/models/radiant/config.rb', line 291 def selector? definition.selector? end |
#update_cache ⇒ Object
301 302 303 |
# File 'app/models/radiant/config.rb', line 301 def update_cache Radiant::Config.initialize_cache end |
#validate ⇒ Object
307 308 309 |
# File 'app/models/radiant/config.rb', line 307 def validate definition.validate(self) end |
#value ⇒ Object
Requesting a config item:
key = Radiant.config['key']
is equivalent to this:
key = Radiant::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.
261 262 263 264 265 266 267 |
# File 'app/models/radiant/config.rb', line 261 def value if boolean? checked? else self[:value] end end |
#value=(param) ⇒ Object
The usual way to use a config item:
Radiant.config['key'] = value
is equivalent to this:
Radiant::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.
236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'app/models/radiant/config.rb', line 236 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 |