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



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

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



106
107
108
# File 'lib/setting.rb', line 106

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.



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

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



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

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.



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

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



90
91
92
93
94
95
96
97
98
# File 'lib/setting.rb', line 90

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

#to_labelObject



57
58
59
# File 'lib/setting.rb', line 57

def to_label
  alt.present? ? alt : key
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.



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

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