Class: Refinery::Setting

Inherits:
Core::BaseModel
  • Object
show all
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

Instance Method Summary collapse

Class Method Details

.cache_keyObject



84
85
86
# File 'app/models/refinery/setting.rb', line 84

def cache_key
  [Refinery::Core.base_cache_key, 'settings_cache'].join('_')
end

.cache_read(name = nil, scoping = nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/models/refinery/setting.rb', line 45

def cache_read(name = nil, scoping = nil)
  result = ensure_cache_exists!

  if name.present?
    scoping = scoping.to_s if scoping.is_a?(Symbol)
    result = result.detect do |rs|
      rs[:name] == name.to_s.downcase.to_sym and rs[:scoping] == scoping
    end
    result = result[:value] if !result.nil? and result.keys.include?(:value)
  end

  result
end

.ensure_cache_exists!Object



36
37
38
39
40
41
42
# File 'app/models/refinery/setting.rb', line 36

def ensure_cache_exists!
  if (result = Rails.cache.read(cache_key, :multithread => true)).nil?
    result = rewrite_cache
  end

  result
end

.find_or_set(name, the_value, options = {}) ⇒ Object Also known as: get_or_set

find_or_set offers a convenient way to



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/models/refinery/setting.rb', line 89

def find_or_set(name, the_value, options={})
  # Merge default options with supplied options.
  options = {
    :scoping => nil,
    :restricted => false,
    :form_value_type => 'text_area'
  }.merge(options)

  # try to find the setting first
  value = get(name, :scoping => options[:scoping])

  # if the setting's value is nil, store a new one using the existing functionality.
  value = set(name, options.merge({:value => the_value})) if value.nil?

  # Return what we found.
  value
end

.get(name, options = {}) ⇒ Object Also known as: []

Retrieve the current value for the setting whose name is supplied.



110
111
112
113
# File 'app/models/refinery/setting.rb', line 110

def get(name, options = {})
  options = {:scoping => nil}.update(options)
  cache_read(name, options[:scoping])
end

.per_pageObject

Number of settings to show per page when using will_paginate



32
33
34
# File 'app/models/refinery/setting.rb', line 32

def per_page
  12
end

.rewrite_cacheObject



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/models/refinery/setting.rb', line 70

def rewrite_cache
  # delete cache
  Rails.cache.delete(cache_key, :multithread => true)

  # generate new cache
  result = (to_cache(all) if (table_exists? rescue false))

  # write cache
  Rails.cache.write(cache_key, result, :multithread => true)

  # return cache, or lack thereof.
  result ||= []
end

.set(name, value) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'app/models/refinery/setting.rb', line 117

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 = find_or_initialize_by_name_and_scoping(name.to_s, scoping)

  # 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

.to_cache(settings) ⇒ Object



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

def to_cache(settings)
  settings.collect do |rs|
    {
      :name => rs.name.to_s.downcase.to_sym,
      :value => rs.value,
      :scoping => rs.scoping,
      :destroyable => rs.destroyable
    }
  end
end

Instance Method Details

#form_valueObject

form_value is so that on the web interface we can display a sane value.



151
152
153
154
155
156
157
# File 'app/models/refinery/setting.rb', line 151

def form_value
  unless self[:value].blank? or self[:value].is_a?(String)
    YAML::dump(self[:value])
  else
    self[:value]
  end
end

#titleObject

prettier version of the name. site_name becomes Site Name



144
145
146
147
148
# File 'app/models/refinery/setting.rb', line 144

def title
  result = name.to_s.titleize
  result << " (#{scoping.titleize})" if scoping.is_a?(String)
  result
end

#valueObject



159
160
161
# File 'app/models/refinery/setting.rb', line 159

def value
  replacements!(self[:value])
end

#value=(new_value) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'app/models/refinery/setting.rb', line 163

def value=(new_value)
  # must convert "1" to true and "0" to false when supplied using 'check_box', unfortunately.
  if ["1", "0"].include?(new_value) and form_value_type == 'check_box'
    new_value = new_value == "1" ? true : false
  end

  # must convert to string if true or false supplied otherwise it becomes 0 or 1, unfortunately.
  if [true, false].include?(new_value)
    new_value = new_value.to_s
  end

  super
end