Module: Spree::Preferences::Preferable
- Included in:
- Configuration
- Defined in:
- app/models/spree/preferences/preferable.rb
Overview
The preference_cache_key is used to determine if the preference can be set. The default behavior is to return nil if there is no id value. On ActiveRecords, new objects will have their preferences saved to a pending hash until it is persisted.
class_attributes are inheritied unless you reassign them in the subclass, so when you inherit a Preferable class, the inherited hook will assign a new hash for the subclass definitions and copy all the definitions allowing the subclass to add additional defintions without affecting the base
Class Method Summary collapse
Instance Method Summary collapse
- #clear_preferences ⇒ Object
- #get_preference(name) ⇒ Object (also: #preferred, #prefers?)
- #has_preference!(name) ⇒ Object
- #has_preference?(name) ⇒ Boolean
- #preference_cache_key(name) ⇒ Object
- #preference_default(name) ⇒ Object
- #preference_description(name) ⇒ Object
- #preference_type(name) ⇒ Object
- #preferences ⇒ Object
- #save_pending_preferences ⇒ Object
- #set_preference(name, value) ⇒ Object
Class Method Details
.included(base) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'app/models/spree/preferences/preferable.rb', line 13 def self.included(base) base.class_eval do extend Spree::Preferences::PreferableClassMethods if respond_to?(:after_create) after_create do |obj| obj.save_pending_preferences end end if respond_to?(:after_destroy) after_destroy do |obj| obj.clear_preferences end end end end |
Instance Method Details
#clear_preferences ⇒ Object
91 92 93 |
# File 'app/models/spree/preferences/preferable.rb', line 91 def clear_preferences preferences.keys.each {|pref| preference_store.delete preference_cache_key(pref)} end |
#get_preference(name) ⇒ Object Also known as: preferred, prefers?
32 33 34 35 |
# File 'app/models/spree/preferences/preferable.rb', line 32 def get_preference(name) has_preference! name send self.class.preference_getter_method(name) end |
#has_preference!(name) ⇒ Object
59 60 61 |
# File 'app/models/spree/preferences/preferable.rb', line 59 def has_preference!(name) raise NoMethodError.new "#{name} preference not defined" unless has_preference? name end |
#has_preference?(name) ⇒ Boolean
63 64 65 |
# File 'app/models/spree/preferences/preferable.rb', line 63 def has_preference?(name) respond_to? self.class.preference_getter_method(name) end |
#preference_cache_key(name) ⇒ Object
79 80 81 82 |
# File 'app/models/spree/preferences/preferable.rb', line 79 def preference_cache_key(name) return unless id [self.class.name, name, id].join('::').underscore end |
#preference_default(name) ⇒ Object
49 50 51 52 |
# File 'app/models/spree/preferences/preferable.rb', line 49 def preference_default(name) has_preference! name send self.class.preference_default_getter_method(name) end |
#preference_description(name) ⇒ Object
54 55 56 57 |
# File 'app/models/spree/preferences/preferable.rb', line 54 def preference_description(name) has_preference! name send self.class.preference_description_getter_method(name) end |
#preference_type(name) ⇒ Object
44 45 46 47 |
# File 'app/models/spree/preferences/preferable.rb', line 44 def preference_type(name) has_preference! name send self.class.preference_type_getter_method(name) end |
#preferences ⇒ Object
67 68 69 70 71 72 73 |
# File 'app/models/spree/preferences/preferable.rb', line 67 def preferences prefs = {} methods.grep(/^prefers_.*\?$/).each do |pref_method| prefs[pref_method.to_s.gsub(/prefers_|\?/, '').to_sym] = send(pref_method) end prefs end |
#save_pending_preferences ⇒ Object
84 85 86 87 88 89 |
# File 'app/models/spree/preferences/preferable.rb', line 84 def save_pending_preferences return unless @pending_preferences @pending_preferences.each do |name, value| set_preference(name, value) end end |
#set_preference(name, value) ⇒ Object
39 40 41 42 |
# File 'app/models/spree/preferences/preferable.rb', line 39 def set_preference(name, value) has_preference! name send self.class.preference_setter_method(name), value end |