Created
June 25, 2011 15:43
-
-
Save twinge/1046602 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Spree::PreferenceAccess | |
def self.included(base) | |
class << base | |
def get(key = nil) | |
key = key.to_s if key.is_a?(Symbol) | |
return nil unless config = self.instance | |
# preferences will be cached under the name of the class including this module (ex. Spree::Config) | |
prefs = Rails.cache.fetch("configuration_#{config.class.name}".to_sym) { config.preferences } | |
return prefs if key.nil? | |
prefs[key] | |
end | |
# Set the preferences as specified in a hash (like params[:preferences] from a post request) | |
def set(preferences={}) | |
config = self.instance | |
preferences.each do |key, value| | |
config.set_preference(key, value) | |
end | |
config.save | |
Rails.cache.delete("configuration_#{config.class.name}".to_sym) | |
end | |
alias_method :[], :get | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment