Class: Setting
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Setting
- 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
- .delete_all(*args) ⇒ Object
-
.deleted ⇒ Object
Deleted scope is specified as a method as it needs to be an exclusive scope.
Instance Method Summary collapse
-
#delete(*args) ⇒ Object
Overrides the delete methods to ensure the default scope is not passed in the query.
-
#destroy ⇒ Object
Performs a soft delete of the setting if this setting is deletable.
-
#reset! ⇒ Object
Resets this setting to the default stored in the settler configuration.
- #to_label ⇒ Object
- #type ⇒ Object
-
#typecast ⇒ Object
Finds the typecast for this key in the settler configuration.
-
#typecasted_value ⇒ Object
Returns the typecasted value or the raw value if a typecaster could not be found.
-
#untypecasted_value ⇒ Object
Reads the raw, untypecasted value.
-
#valid_values ⇒ Object
Returns all valid values for this setting, which is based on the presence of an inclusion validator.
-
#value ⇒ Object
Returns the value, typecasted if a typecaster is available.
- #value=(val) ⇒ Object
Class Method Details
.delete_all(*args) ⇒ Object
77 |
# File 'lib/setting.rb', line 77 def self.delete_all *args; Setting.unscoped{ super } end |
.deleted ⇒ Object
Deleted scope is specified as a method as it needs to be an exclusive scope
91 92 93 |
# File 'lib/setting.rb', line 91 def self.deleted unscoped { Setting.where(:deleted => true) } end |
Instance Method Details
#delete(*args) ⇒ Object
Overrides the delete methods to ensure the default scope is not passed in the query
76 |
# File 'lib/setting.rb', line 76 def delete *args; Setting.unscoped{ super } end |
#destroy ⇒ Object
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.
66 67 68 69 70 71 72 73 |
# File 'lib/setting.rb', line 66 def destroy if deletable? self.deleted = true if Setting.where(:id => self).update_all(:deleted => true) deleted? else false end end |
#reset! ⇒ Object
Resets this setting to the default stored in the settler configuration
80 81 82 83 84 85 86 87 88 |
# File 'lib/setting.rb', line 80 def reset! defaults = Settler.config[self.key] self.label = defaults['label'] self.value = defaults['value'] self.editable = defaults['editable'] self.deletable = defaults['deletable'] self.deleted = false save(:validate => false) end |
#to_label ⇒ Object
47 48 49 |
# File 'lib/setting.rb', line 47 def to_label label.present? ? label : key end |
#type ⇒ Object
43 44 45 |
# File 'lib/setting.rb', line 43 def type @type ||= ActiveSupport::StringInquirer.new(typecaster.try(:type) || 'string') end |
#typecast ⇒ Object
Finds the typecast for this key in the settler configuration.
39 40 41 |
# File 'lib/setting.rb', line 39 def typecast @typecast ||= Settler.typecast_for(key) end |
#typecasted_value ⇒ Object
Returns the typecasted value or the raw value if a typecaster could not be found.
34 35 36 |
# File 'lib/setting.rb', line 34 def typecasted_value typecaster.present? ? typecaster.typecast(untypecasted_value) : untypecasted_value end |
#untypecasted_value ⇒ Object
Reads the raw, untypecasted value.
29 30 31 |
# File 'lib/setting.rb', line 29 def untypecasted_value read_attribute(:value) end |
#valid_values ⇒ Object
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.
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/setting.rb', line 53 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 |
#value ⇒ Object
Returns the value, typecasted if a typecaster is available.
20 21 22 |
# File 'lib/setting.rb', line 20 def value typecast.present? ? typecasted_value : super end |
#value=(val) ⇒ Object
24 25 26 |
# File 'lib/setting.rb', line 24 def value=(val) typecaster.present? && typecaster.typecast_on_write? ? write_attribute(:value, typecaster.typecast_on_write(val)) : super end |