Class: Refinery::Setting
- Inherits:
-
Core::BaseModel
- Object
- Core::BaseModel
- Refinery::Setting
- Extended by:
- FriendlyId
- Defined in:
- app/models/refinery/setting.rb
Constant Summary collapse
- FORM_VALUE_TYPES =
[ ['Multi-line', 'text_area'], ['Checkbox', 'check_box'] ]
Class Method Summary collapse
-
.find_or_set(name, the_value, options = {}) ⇒ Object
(also: get_or_set)
find_or_set offers a convenient way to.
- .form_value_types ⇒ Object
-
.get(name, options = {}) ⇒ Object
(also: [])
Retrieve the current value for the setting whose name is supplied.
-
.per_page ⇒ Object
Number of settings to show per page when using will_paginate.
- .set(name, value) ⇒ Object
Instance Method Summary collapse
-
#form_value ⇒ Object
form_value is so that on the web interface we can display a sane value.
-
#should_generate_new_friendly_id? ⇒ Boolean
If name changes tell friendly_id to regenerate slug when saving record.
- #title ⇒ Object
- #value ⇒ Object
- #value=(new_value) ⇒ Object
Class Method Details
.find_or_set(name, the_value, options = {}) ⇒ Object Also known as: get_or_set
find_or_set offers a convenient way to
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'app/models/refinery/setting.rb', line 38 def find_or_set(name, the_value, ={}) # Merge default options with supplied options. = { scoping: nil, restricted: false, form_value_type: 'text_area' }.merge() # try to find the setting first value = get(name, scoping: [:scoping]) # if the setting's value is nil, store a new one using the existing functionality. value = set(name, .merge(value: the_value)) if value.nil? # Return what we found. value end |
.form_value_types ⇒ Object
33 34 35 |
# File 'app/models/refinery/setting.rb', line 33 def form_value_types FORM_VALUE_TYPES end |
.get(name, options = {}) ⇒ Object Also known as: []
Retrieve the current value for the setting whose name is supplied.
59 60 61 62 63 |
# File 'app/models/refinery/setting.rb', line 59 def get(name, = {}) = {scoping: nil}.update() where(name: name, scoping: [:scoping]) .select(:value).map(&:value).first end |
.per_page ⇒ Object
Number of settings to show per page when using will_paginate
29 30 31 |
# File 'app/models/refinery/setting.rb', line 29 def per_page 12 end |
.set(name, value) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'app/models/refinery/setting.rb', line 67 def set(name, value) return (value.is_a?(Hash) ? value[:value] : value) unless (table_exists? rescue false) scoping = (value[:scoping] if value.is_a?(Hash) and value.has_key?(:scoping)) setting = where(name: name.to_s, scoping: scoping).first_or_initialize # you could also pass in {:value => 'something', :scoping => 'somewhere'} unless value.is_a?(Hash) and value.has_key?(:value) setting.value = value else # set the value last, so that the other attributes can transform it if required. setting.form_value_type = value[:form_value_type] || 'text_area' if setting.respond_to?(:form_value_type) setting.scoping = value[:scoping] if value.has_key?(:scoping) setting.destroyable = value[:destroyable] if value.has_key?(:destroyable) setting.value = value[:value] end # Save because we're in a setter method. setting.save # Return the value setting.value end |
Instance Method Details
#form_value ⇒ Object
form_value is so that on the web interface we can display a sane value.
97 98 99 100 101 102 103 |
# File 'app/models/refinery/setting.rb', line 97 def form_value unless self[:value].blank? or self[:value].is_a?(String) YAML::dump(self[:value]) else self[:value] end end |
#should_generate_new_friendly_id? ⇒ Boolean
If name changes tell friendly_id to regenerate slug when saving record
10 11 12 |
# File 'app/models/refinery/setting.rb', line 10 def should_generate_new_friendly_id? name_changed? end |
#title ⇒ Object
92 93 94 |
# File 'app/models/refinery/setting.rb', line 92 def title self[:title].presence || auto_title end |
#value ⇒ Object
105 106 107 |
# File 'app/models/refinery/setting.rb', line 105 def value replacements!(self[:value]) end |
#value=(new_value) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 |
# File 'app/models/refinery/setting.rb', line 109 def value=(new_value) # must convert "1" to true and "0" to false when supplied using 'check_box', unfortunately. if %w[1 0].include?(new_value) && form_value_type == 'check_box' new_value = new_value == "1" end # must convert to string if true or false supplied otherwise it becomes 0 or 1, unfortunately. new_value = new_value.to_s if [true, false].include?(new_value) super end |