Module: SettingAccessors::SettingScaffold

Defined in:
lib/setting_accessors/setting_scaffold.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/setting_accessors/setting_scaffold.rb', line 12

def self.included(base)
  base.extend ClassMethods
  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



141
142
143
# File 'lib/setting_accessors/setting_scaffold.rb', line 141

def default_value
  data['default'].freeze
end

#raw_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



160
161
162
# File 'lib/setting_accessors/setting_scaffold.rb', line 160

def raw_value
  @raw_value || value
end

#raw_value=(new_value) ⇒ Object

Sets the new setting value by converting the raw value automatically.



167
168
169
170
# File 'lib/setting_accessors/setting_scaffold.rb', line 167

def raw_value=(new_value)
  @raw_value = new_value
  self.value = converter.new(new_value).convert
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



149
150
151
# File 'lib/setting_accessors/setting_scaffold.rb', line 149

def value_type
  data['type'] || 'polymorphic'
end