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

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_valueObject

Returns the default value for the current setting.

Returns:

  • (Object)

    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, options = {})
  options[:scope] = [:settings, :global, self.name]
  options[:scope] = [:settings, self.assignable.class.to_s.underscore, self.name] unless SettingAccessors::Internal.globally_defined_setting?(self.name)
  I18n.t(key, options)
end

#localized_descriptionString

Returns the localized setting description see #localized_name.

Returns:

  • (String)

    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_nameString

Returns the localized setting name they are stored in config/locales/settings.LOCALE.yml.

Returns:

  • (String)

    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_valueObject

We can’t use the name #value_before_type_cast here as it would shadow ActiveRecord’s default one - which might still be needed.

Returns:

  • (Object)

    the setting’s value before it was type casted using the defined rule in settings.yml See #value_before_type_cast for ActiveRecord attributes



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_typeString

Returns the setting’s type as specified in settings.yml If the setting wasn’t specified, a polymorphic type is assumed.

Returns:

  • (String)

    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