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

.delete_all(*args) ⇒ Object



83
# File 'lib/setting.rb', line 83

def self.delete_all *args;  Setting.without_default_scope{ super } end

.deletedObject

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



102
103
104
# File 'lib/setting.rb', line 102

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.



97
98
99
# File 'lib/setting.rb', line 97

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

Instance Method Details

#delete(*args) ⇒ Object

Overrides the delete methods to ensure the default scope is not passed in the query



82
# File 'lib/setting.rb', line 82

def delete *args;           Setting.without_default_scope{ super } end

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



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

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



86
87
88
89
90
91
92
93
94
# File 'lib/setting.rb', line 86

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

#typeObject



53
54
55
# File 'lib/setting.rb', line 53

def type
  @type ||= ActiveSupport::StringInquirer.new(typecaster.try(:type) || 'string')
end

#typecastObject

Finds the typecast for this key in the settler configuration.



49
50
51
# File 'lib/setting.rb', line 49

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.



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

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

#untypecasted_valueObject

Reads the raw, untypecasted value.



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

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.



59
60
61
62
63
64
65
66
67
68
# File 'lib/setting.rb', line 59

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

#value=(val) ⇒ Object



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

def value=(val)
  typecaster.present? && typecaster.typecast_on_write? ? write_attribute(:value, typecaster.typecast_on_write(val)) : super
end