Module: SettingAccessors::SettingScaffold
- Defined in:
- lib/setting_accessors/setting_scaffold.rb
Overview
Helper methods for the chosen setting model They are in this module to leave the end developer some room for his own methods in the setting model for his own methods.
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#default_value ⇒ Object
The default value for the current setting.
-
#i18n_lookup(key, options = {}) ⇒ Object
Performs an I18n lookup in the settings locale.
-
#localized_description ⇒ String
The localized setting description see #localized_name.
-
#localized_name ⇒ String
The localized setting name they are stored in config/locales/settings.LOCALE.yml.
-
#original_value ⇒ Object
We can’t use the name #value_before_type_cast here as it would shadow ActiveRecord’s default one - which might still be needed.
-
#set_value(new_value) ⇒ Object
Sets the setting’s value to the given one Performs automatic type casts.
-
#value_type ⇒ String
The setting’s type as specified in settings.yml If the setting wasn’t specified, a polymorphic type is assumed.
Class Method Details
.included(base) ⇒ Object
9 10 11 12 13 14 15 16 17 |
# File 'lib/setting_accessors/setting_scaffold.rb', line 9 def self.included(base) base.extend ClassMethods base.validates_with SettingAccessors::Validator base.serialize :value base.validates :name, :uniqueness => {:scope => [:assignable_type, :assignable_id]}, :presence => true end |
Instance Method Details
#default_value ⇒ Object
Returns the default value for the current setting.
196 197 198 |
# File 'lib/setting_accessors/setting_scaffold.rb', line 196 def default_value data['default'] end |
#i18n_lookup(key, options = {}) ⇒ Object
Performs an I18n lookup in the settings locale. Class based settings are store in ‘settings.CLASS.NAME’, globally defined settings in ‘settings.global.NAME’
187 188 189 190 191 |
# File 'lib/setting_accessors/setting_scaffold.rb', line 187 def i18n_lookup(key, = {}) [:scope] = [:settings, :global, self.name] [:scope] = [:settings, self.assignable.class.to_s.underscore, self.name] unless SettingAccessors::Internal.globally_defined_setting?(self.name) I18n.t(key, ) end |
#localized_description ⇒ String
Returns the localized setting description see #localized_name.
178 179 180 |
# File 'lib/setting_accessors/setting_scaffold.rb', line 178 def localized_description i18n_lookup(:description) end |
#localized_name ⇒ String
Returns the localized setting name they are stored in config/locales/settings.LOCALE.yml.
170 171 172 |
# File 'lib/setting_accessors/setting_scaffold.rb', line 170 def localized_name i18n_lookup(:name) end |
#original_value ⇒ Object
We can’t use the name #value_before_type_cast here as it would shadow ActiveRecord’s default one - which might still be needed.
215 216 217 |
# File 'lib/setting_accessors/setting_scaffold.rb', line 215 def original_value @original_value || self.value end |
#set_value(new_value) ⇒ Object
Sets the setting’s value to the given one Performs automatic type casts
223 224 225 226 |
# File 'lib/setting_accessors/setting_scaffold.rb', line 223 def set_value(new_value) @original_value = new_value self.value = converter.convert(new_value) end |
#value_type ⇒ String
Returns the setting’s type as specified in settings.yml If the setting wasn’t specified, a polymorphic type is assumed.
204 205 206 |
# File 'lib/setting_accessors/setting_scaffold.rb', line 204 def value_type data['type'] || 'polymorphic' end |