class TrustyCms::Config
def [](key)
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
def []=(key, value)
def []=(key, value) if database_exists? if table_exists? setting = where(key: key).first_or_initialize setting.value = value end end end
def boolean?
Returns true if the item key ends with '?' or the definition specifies :type => :boolean.
def boolean? definition.boolean? || key.ends_with?('?') end
def cache_file
def cache_file File.join(cache_path, 'trusty_config_cache.txt') end
def cache_file_exists?
def cache_file_exists? File.file?(cache_file) end
def cache_path
def cache_path "#{Rails.root}/tmp" end
def checked?
Returns true if the item is boolean and true.
def checked? return nil if self[:value].nil? boolean? && self[:value] == 'true' end
def database_exists?
def database_exists? ActiveRecord::Base.connection rescue ActiveRecord::NoDatabaseError false else true end
def default_settings
def default_settings @default_settings ||= %w{defaults.locale defaults.page.filter defaults.page.parts defaults.page.fields defaults.page.status} end
def define(key, options = {})
but at the moment that's only done in testing.
define "your.layout", choose_layout
define "my.layout", choose_layout
choose_layout = TrustyCms::Config::Definition.new(:select_from => lambda {Layout.all.map{|l| [l.name, l.d]}})
It's also possible to reuse a definition by passing it to define:
end
...
config.define 'defaults.page.parts', :default => "Body,Extended"
config.define 'defaults.locale', :select_from => lambda { TrustyCms::AvailableLocales.locales }, :allow_blank => true
TrustyCms.config do |config|
* :allow_display should be false if the config item should not be showable in radius tags
* :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_blank should be false if the config item must not be blank or nil
* :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.
* :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
* :type can be :string, :boolean or :integer. Note that all settings whose key ends in ? are considered boolean.
* :default is the value that will be placed in the database if none has been set already
Can take several options:
define('setting.key', options)
Declares a setting definition that will constrain and support the use of a particular config entry.
def define(key, options = {}) called_from = caller.grep(/\/initializers\//).first if options.is_a? TrustyCms::Config::Definition definition = options else key = [options[:prefix], key].join('.') if options[:prefix] end definition ||= TrustyCms::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
def definition
that does not restrict use.
Returns the definition associated with this config item. If none has been declared this will be an empty definition
def definition @definition ||= self.class.definition_for(key) end
def definition_for(key)
def definition_for(key) definitions[key] ||= TrustyCms::Config::Definition.new(empty: true) end
def definitions
def definitions TrustyCms.config_definitions end
def ensure_cache_file
def ensure_cache_file FileUtils.mkpath(cache_path) FileUtils.touch(cache_file) end
def initialize_cache
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
def namespace(prefix, options = {}, &block)
end
end
secret.define('longing', :default => 'vindication') # defines 'secret.longing'
secret.define('lair', :default => 'batcave') # defines 'secret.lair'
secret.define('identity', :default => 'batman') # defines 'secret.identity'
config.namespace('secret', :allow_display => false) do |secret|
TrustyCms.config do |config|
A convenient drying method for specifying a prefix and options common to several settings.
def namespace(prefix, options = {}, &block) prefix = [options[:prefix], prefix].join('.') if options[:prefix] with_options(options.merge(prefix: prefix), &block) end
def selected_value
Returns a name corresponding to the current setting value, if the setting definition includes a select_from parameter.
def selected_value definition.selected(value) end
def selector?
Returns true if the item defintion includes a :select_from parameter that limits the range of permissible options.
def selector? definition.selector? end
def site_settings
def site_settings @site_settings ||= %w{site.title site.host dev.host local.timezone} end
def stale_cache?
def stale_cache? return true unless TrustyCms::Config.cache_file_exists? Rails.cache.read('TrustyCms.cache_mtime') != File.mtime(cache_file) end
def to_hash
def to_hash Hash[*all.map { |pair| [pair.key, pair.value] }.flatten] end
def update_cache
def update_cache TrustyCms::Config.initialize_cache end
def user_settings
def user_settings @user_settings ||= ['user.allow_password_reset?'] end
def validate
def validate definition.validate(self) end
def value
for everything else a string.
If the config item is boolean the response will be true or false. For items with :type => :integer it will be an integer,
key = TrustyCms::Config.find_or_create_by_key('key').value
is equivalent to this:
key = TrustyCms.config['key']
Requesting a config item:
def value if boolean? checked? else self[:value] end end
def value=(param)
set a value that is not among those permitted.
so this will raise a ConfigError if you try to change a protected config entry or a RecordInvalid if you
Calling value= also applies any validations and restrictions that are found in the associated definition.
TrustyCms::Config.find_or_create_by_key('key').value = value
is equivalent to this:
TrustyCms.config['key'] = value
The usual way to use a config item:
def value=(param) newvalue = param.to_s if newvalue != self[:value] raise ConfigError, "#{key} cannot be changed" unless settable? || self[:value].blank? self[:value] = if boolean? newvalue == '1' || newvalue == 'true' ? 'true' : 'false' else newvalue end save! end self[:value] end