vendor/plugins/refinery_settings/app/models/refinery_setting.rb in refinerycms-0.9.6.28 vs vendor/plugins/refinery_settings/app/models/refinery_setting.rb in refinerycms-0.9.6.29
- old
+ new
@@ -1,27 +1,11 @@
class RefinerySetting < ActiveRecord::Base
validates_presence_of :name
- #validates_uniqueness_of :name
serialize :value # stores into YAML format
- after_save do |object|
- cache_write(object.name, object.value)
- end
- def self.cache_key(name)
- "refinery_setting_#{name}"
- end
-
- def self.cache_write(name, value)
- Rails.cache.write(cache_key(name), value)
- end
-
- def self.cache_read(name)
- Rails.cache.read(cache_key(name))
- end
-
# Number of settings to show per page when using will_paginate
def self.per_page
10
end
@@ -45,37 +29,16 @@
self[method_name]
end
end
def self.find_or_set(name, the_value)
- # Try to get the value from cache first.
- unless (value = cache_read(name)).present?
- # Either find the record or create one with the defined value
- value = find_or_create_by_name(:name => name.to_s, :value => the_value).value
- # Cache it
- cache_write(name, value)
- end
-
- # Return what we found.
- value
+ find_or_create_by_name(:name => name.to_s, :value => the_value).value
end
def self.[](name)
- # Try to get the value from cache first.
- unless (value = cache_read(name)).present?
- # Not found in cache, try to find the record
- value = if (setting = self.find_by_name(name.to_s)).present?
- # Cache it
- cache_write(name, setting.value)
- else
- # Still cache the nil to prevent more lookups to find nothing.
- cache_write(name, nil)
- end
- end
-
- # Return what we found.
- value
+ setting = self.find_by_name(name.to_s)
+ setting.value unless setting.nil?
end
def self.[]=(name, value)
setting = find_or_create_by_name(name.to_s)
setting.value = value
@@ -88,13 +51,11 @@
# it just returns a string. This code maps the two boolean
# values correctly so a boolean is returned
REPLACEMENTS = {"true" => true, "false" => false}
def value
- current_value = self[:value]
-
- if current_value.present?
+ if (current_value = self[:value]).present?
# This bit handles true and false so that true and false are actually returned
# not "0" and "1"
REPLACEMENTS.each do |current, new_value|
current_value = new_value if current_value == current
end
@@ -117,6 +78,6 @@
# must convert to string if true or false supplied otherwise it becomes 0 or 1, unfortunately.
new_value = new_value.to_s if ["trueclass","falseclass"].include?(new_value.class.to_s.downcase)
self[:value] = new_value
end
-end
+end
\ No newline at end of file