lib/settings.rb in settings-goo-0.3.0 vs lib/settings.rb in settings-goo-0.3.1

- old
+ new

@@ -20,42 +20,45 @@ def value=(val) self[:value] = val.to_json end def self.[](key) - if setting = find_cached(key) + setting = find_cached(key) + if setting setting.value else nil end end def self.[]=(key, val) - key = key.to_s - setting = Settings.find_by_key(key) || Settings.new(:key => key) - setting.value = val - setting.save + return nil if key.blank? + setting = Settings.find_key(key.to_s) + setting.update_attribute(:value, val) if setting end def self.keys Rails.cache.fetch("settings/settings.keys") { - self.find(:all, :select => 'key', :order => 'key ASC').map{|s| s.key.to_sym} + self.find(:all, :select => 'key', :order => 'key ASC').map{|setting| setting.key.to_sym} } end def self.remove(key) - key = key.to_s - setting = Settings.find_by_key(key) + setting = Settings.find_by_key(key.to_s) if setting setting.destroy else nil end end private - def self.find_cached(key) + def self.find_key(key) key = key.to_s - Rails.cache.fetch("settings/#{key}") { Settings.find_by_key(key) } + Settings.find_by_key(key) || Settings.new(:key => key) + end + + def self.find_cached(key) + Rails.cache.fetch("settings/#{key}") { Settings.find_by_key(key.to_s) } end def clear_cache Rails.cache.delete("settings/#{key}") end