Class: TrustyCms::Config
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- TrustyCms::Config
- Defined in:
- app/models/trusty_cms/config.rb,
lib/trusty_cms/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
- .database_exists? ⇒ Boolean
- .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.
275 276 277 |
# File 'app/models/trusty_cms/config.rb', line 275 def definition @definition end |
Class Method Details
.[](key) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'app/models/trusty_cms/config.rb', line 78 def [](key) if database_exists? if table_exists? unless TrustyCms::Config.cache_file_exists? TrustyCms::Config.ensure_cache_file TrustyCms::Config.initialize_cache end TrustyCms::Config.initialize_cache if TrustyCms::Config.stale_cache? Rails.cache.read('TrustyCms::Config')[key] end end end |
.[]=(key, value) ⇒ Object
91 92 93 94 95 96 97 98 |
# File 'app/models/trusty_cms/config.rb', line 91 def []=(key, value) if database_exists? if table_exists? setting = where(key: key).first_or_initialize setting.value = value end end end |
.cache_file ⇒ Object
137 138 139 |
# File 'app/models/trusty_cms/config.rb', line 137 def cache_file File.join(cache_path,'radiant_config_cache.txt') end |
.cache_file_exists? ⇒ Boolean
119 120 121 |
# File 'app/models/trusty_cms/config.rb', line 119 def cache_file_exists? File.file?(cache_file) end |
.cache_path ⇒ Object
133 134 135 |
# File 'app/models/trusty_cms/config.rb', line 133 def cache_path "#{Rails.root}/tmp" end |
.database_exists? ⇒ Boolean
100 101 102 103 104 105 106 |
# File 'app/models/trusty_cms/config.rb', line 100 def database_exists? ActiveRecord::Base.connection rescue ActiveRecord::NoDatabaseError false else true end |
.default_settings ⇒ Object
145 146 147 |
# File 'app/models/trusty_cms/config.rb', line 145 def default_settings @default_settings ||= %w{ defaults.locale defaults.page.filter defaults.page.parts defaults.page.fields defaults.page.status } 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:
TrustyCms.config do |config|
config.define 'defaults.locale', :select_from => lambda { TrustyCms::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 = TrustyCms::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.
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'app/models/trusty_cms/config.rb', line 197 def define(key, ={}) called_from = caller.grep(/\/initializers\//).first if .is_a? TrustyCms::Config::Definition definition = else key = [[:prefix], key].join('.') if [:prefix] end definition ||= TrustyCms::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
221 222 223 |
# File 'app/models/trusty_cms/config.rb', line 221 def definition_for(key) definitions[key] ||= TrustyCms::Config::Definition.new(:empty => true) end |
.definitions ⇒ Object
217 218 219 |
# File 'app/models/trusty_cms/config.rb', line 217 def definitions TrustyCms.config_definitions end |
.ensure_cache_file ⇒ Object
128 129 130 131 |
# File 'app/models/trusty_cms/config.rb', line 128 def ensure_cache_file FileUtils.mkpath(cache_path) FileUtils.touch(cache_file) end |
.initialize_cache ⇒ Object
112 113 114 115 116 117 |
# File 'app/models/trusty_cms/config.rb', line 112 def initialize_cache TrustyCms::Config.ensure_cache_file Rails.cache.write('TrustyCms::Config',TrustyCms::Config.to_hash) Rails.cache.write('TrustyCms.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.
TrustyCms.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
163 164 165 166 |
# File 'app/models/trusty_cms/config.rb', line 163 def namespace(prefix, = {}, &block) prefix = [[:prefix], prefix].join('.') if [:prefix] (.merge(:prefix => prefix), &block) end |
.site_settings ⇒ Object
141 142 143 |
# File 'app/models/trusty_cms/config.rb', line 141 def site_settings @site_settings ||= %w{ site.title site.host dev.host local.timezone } end |
.stale_cache? ⇒ Boolean
123 124 125 126 |
# File 'app/models/trusty_cms/config.rb', line 123 def stale_cache? return true unless TrustyCms::Config.cache_file_exists? Rails.cache.read('TrustyCms.cache_mtime') != File.mtime(cache_file) end |
.to_hash ⇒ Object
108 109 110 |
# File 'app/models/trusty_cms/config.rb', line 108 def to_hash Hash[ *all.map { |pair| [pair.key, pair.value] }.flatten ] end |
.user_settings ⇒ Object
149 150 151 |
# File 'app/models/trusty_cms/config.rb', line 149 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.
281 282 283 |
# File 'app/models/trusty_cms/config.rb', line 281 def boolean? definition.boolean? || self.key.ends_with?("?") end |
#checked? ⇒ Boolean
Returns true if the item is boolean and true.
287 288 289 290 |
# File 'app/models/trusty_cms/config.rb', line 287 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.
300 301 302 |
# File 'app/models/trusty_cms/config.rb', line 300 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.
294 295 296 |
# File 'app/models/trusty_cms/config.rb', line 294 def selector? definition.selector? end |
#update_cache ⇒ Object
304 305 306 |
# File 'app/models/trusty_cms/config.rb', line 304 def update_cache TrustyCms::Config.initialize_cache end |
#validate ⇒ Object
310 311 312 |
# File 'app/models/trusty_cms/config.rb', line 310 def validate definition.validate(self) end |
#value ⇒ Object
264 265 266 267 268 269 270 |
# File 'app/models/trusty_cms/config.rb', line 264 def value if boolean? checked? else self[:value] end end |
#value=(param) ⇒ Object
The usual way to use a config item:
TrustyCms.config['key'] = value
is equivalent to this:
TrustyCms::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.
239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'app/models/trusty_cms/config.rb', line 239 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 |