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

- old
+ new

@@ -1,9 +1,14 @@ class Settings < ActiveRecord::Base + validates_presence_of :key validates_uniqueness_of :key after_save :clear_cache + after_create :clear_cache_keys + #TODO - Only clear cached keys when :key is updated, not :value + after_update :clear_cache_keys after_destroy :clear_cache + after_destroy :clear_cache_keys def value val = self[:value] if val ActiveSupport::JSON.decode val @@ -30,11 +35,13 @@ setting.value = val setting.save end def self.keys - self.find(:all, :select => 'key', :order => 'key ASC').map{|s| s.key.to_sym} + Rails.cache.fetch("settings/settings.keys") { + self.find(:all, :select => 'key', :order => 'key ASC').map{|s| s.key.to_sym} + } end def self.remove(key) key = key.to_s setting = Settings.find_by_key(key) @@ -50,7 +57,11 @@ Rails.cache.fetch("settings/#{key}") { Settings.find_by_key(key) } end def clear_cache Rails.cache.delete("settings/#{key}") + end + + def clear_cache_keys + Rails.cache.delete("settings/settings.keys") end end