class TrustyCms::Config::Definition
def allow_blank?
Returns true unless :allow_blank has been explicitly set to false. Defaults to true.
def allow_blank? true unless allow_blank == false end
def boolean?
considered boolean, whether a type is declared or not. config.boolean? may therefore differ from config.definition.boolean?
Returns true if the definition included a :type => :boolean parameter. Config entries that end in '?' are automatically
def boolean? type == :boolean end
def empty?
definitions created when an undefined config item is set or got.
Returns true if the definition included an :empty flag, which should only be the case for the blank, unrestricting
def empty? !!empty end
def hidden?
def hidden? true if allow_display == false end
def initialize(options = {})
See the method documentation in TrustyCms::Config for options and conventions.
end
end
users.define 'allow_password_reset?', :label => 'Allow password reset?'
config.namespace('users', :allow_change => true) do |users|
TrustyCms::Config.prepare do |config|
The actual defining is done by TrustyCms::Config#define and usually in a block like this:
a config item is global or site-specific.
validate it when it changes. In the next update it will also allow you to declare that
to place limits on that behavior. It allows you to protect a config entry, to specify the values it can take and to
They're created when first mentioned and then available in all parts of the application. The definition mechanism is a way
By default trusty's configuration machinery is open and ad-hoc: config items are just globally-accessible variables.
Configuration 'definitions' are metadata held in memory that add restriction and description to individual config entries.
def initialize(options = {}) %i[empty default type notes validate_with select_from allow_blank allow_change allow_display units definer].each do |attribute| instance_variable_set "@#{attribute}".to_sym, options[attribute] end end
def integer?
def integer? type == :integer end
def normalize_selection(choices)
here we standardises on an options array-of-arrays so that it's easier to validate input
in definitions we accept anything that options_for_select would normally take
def normalize_selection(choices) choices = choices.to_a if Hash === choices choices = choices.collect { |c| (c.is_a? Array) ? c : [c, c] } end
def selectable?(value)
def selectable?(value) return true unless selector? selection.map(&:last).map(&:downcase).include?(value.downcase) end
def selected(value)
this will return the name corresponding to the currently selected value.
If the config item is a selector and :select_from specifies [name, value] pairs (as hash or array),
def selected(value) if value && selector? && pair = selection.find { |s| s.last == value } pair.first end end
def selection
if :select_from is a proc it is called first with no arguments and its return value passed through.
Returns the list of possible values for this config entry in a form suitable for passing to options_for_select.
def selection if selector? choices = select_from choices = choices.call if choices.respond_to? :call choices = normalize_selection(choices) choices.unshift ['', ''] if allow_blank? choices end end
def selector?
Returns true if the definition included a :select_from parameter (either as list or proc).
def selector? !select_from.blank? end
def settable?
Returns true unless :allow_change has been explicitly set to false. Defaults to true.
def settable? true unless allow_change == false end
def validate(setting)
* if :allow_blank has been set to false, we test that the value is not blank
* if the config item is a selector we test that its value is one of the permitted options
* if :type is :integer, we test that the supplied string resolves to a valid integer
* if :validate_with specifies a block, the setting object is passed to the block
There are several ways in which validations might be defined or implied:
Checks the supplied value against the validation rules for this definition.
def validate(setting) if allow_blank? return if setting.value.blank? else setting.errors.add :value, :blank if setting.value.blank? end if validate_with.is_a? Proc validate_with.call(setting) end if selector? setting.errors.add :value, :not_permitted unless selectable?(setting.value) end if integer? begin Integer(setting.value) rescue StandardError setting.errors.add :value, :not_a_number end end end
def visible?
Returns true unless :allow_change has been explicitly set to false. Defaults to true.
def visible? true unless allow_display == false end