Class: Setting

Inherits:
ActiveRecord::Base show all
Defined in:
lib/setting.rb

Overview

The Setting class is an AR model that encapsulates a Settler setting. The key if the setting is the only required attribute.\

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.deletedObject

Deleted scope is specified as a method as it needs to be an exclusive scope



89
90
91
# File 'lib/setting.rb', line 89

def self.deleted
  Setting.without_default_scope{ Setting.all :conditions => { :deleted => true } }
end

.without_default_scope(&block) ⇒ Object

Can be used to get all settings, including deleted settings.



84
85
86
# File 'lib/setting.rb', line 84

def self.without_default_scope &block
  Setting.with_exclusive_scope(&block)
end

Instance Method Details

#destroyObject

Performs a soft delete of the setting if this setting is deletable. This ensures this setting is not recreated from the configuraiton file. Returns false if the setting could not be destroyed.



64
65
66
67
68
69
70
71
# File 'lib/setting.rb', line 64

def destroy    
  if deletable?       
    self.deleted = true if Setting.update_all({ :deleted => true }, { :id => self })
    deleted?
  else 
    false
  end
end

#reset!Object

Resets this setting to the default stored in the settler configuration



74
75
76
77
78
79
80
81
# File 'lib/setting.rb', line 74

def reset!
  defaults = Settler.config[self.key]
  self.alt = defaults['alt']
  self.value = defaults['value']
  self.editable = defaults['editable']
  self.deletable = defaults['deletable']    
  rails3 ? save(:validate => false) : save(false)
end

#typecastObject

Finds the typecast for this key in the settler configuration.



45
46
47
# File 'lib/setting.rb', line 45

def typecast
  @typecast ||= Settler.typecast_for(key)
end

#typecasted_valueObject

Returns the typecasted value or the raw value if a typecaster could not be found.



40
41
42
# File 'lib/setting.rb', line 40

def typecasted_value
  typecaster.present? ? typecaster.typecast(untypecasted_value) : untypecasted_value
end

#untypecasted_valueObject

Reads the raw, untypecasted value.



35
36
37
# File 'lib/setting.rb', line 35

def untypecasted_value
  read_attribute(:value)
end

#valid_valuesObject

Returns all valid values for this setting, which is based on the presence of an inclusion validator. Will return nil if no valid values could be determined.



51
52
53
54
55
56
57
58
59
60
# File 'lib/setting.rb', line 51

def valid_values
  if validators['inclusion']
    return case
      when validators['inclusion'].is_a?(Array) then validators['inclusion']
      when validators['inclusion'].is_a?(String) then validators['inclusion'].to_s.split(',').map{|v| v.to_s.strip }
      else nil
    end
  end
  nil
end

#valueObject

Returns the value, typecasted if a typecaster is available.



30
31
32
# File 'lib/setting.rb', line 30

def value
  typecast.present? ? typecasted_value : super
end