Module: Propensity::Preferences::Preferable
- Included in:
- Configuration
- Defined in:
- app/models/propensity/preferences/preferable.rb
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
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'app/models/propensity/preferences/preferable.rb', line 15 def self.included(base) base.class_eval do extend 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
93 94 95 |
# File 'app/models/propensity/preferences/preferable.rb', line 93 def clear_preferences preferences.keys.each {|pref| preference_store.delete preference_cache_key(pref)} end |
#get_preference(name) ⇒ Object Also known as: preferred, prefers?
34 35 36 37 |
# File 'app/models/propensity/preferences/preferable.rb', line 34 def get_preference(name) has_preference! name send self.class.preference_getter_method(name) end |
#has_preference!(name) ⇒ Object
61 62 63 |
# File 'app/models/propensity/preferences/preferable.rb', line 61 def has_preference!(name) raise NoMethodError.new "#{name} preference not defined" unless has_preference? name end |
#has_preference?(name) ⇒ Boolean
65 66 67 |
# File 'app/models/propensity/preferences/preferable.rb', line 65 def has_preference?(name) respond_to? self.class.preference_getter_method(name) end |
#preference_cache_key(name) ⇒ Object
81 82 83 84 |
# File 'app/models/propensity/preferences/preferable.rb', line 81 def preference_cache_key(name) return unless id [self.class.name, name, id].join('::').underscore end |
#preference_default(name) ⇒ Object
51 52 53 54 |
# File 'app/models/propensity/preferences/preferable.rb', line 51 def preference_default(name) has_preference! name send self.class.preference_default_getter_method(name) end |
#preference_description(name) ⇒ Object
56 57 58 59 |
# File 'app/models/propensity/preferences/preferable.rb', line 56 def preference_description(name) has_preference! name send self.class.preference_description_getter_method(name) end |
#preference_type(name) ⇒ Object
46 47 48 49 |
# File 'app/models/propensity/preferences/preferable.rb', line 46 def preference_type(name) has_preference! name send self.class.preference_type_getter_method(name) end |
#preferences ⇒ Object
69 70 71 72 73 74 75 |
# File 'app/models/propensity/preferences/preferable.rb', line 69 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
86 87 88 89 90 91 |
# File 'app/models/propensity/preferences/preferable.rb', line 86 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
41 42 43 44 |
# File 'app/models/propensity/preferences/preferable.rb', line 41 def set_preference(name, value) has_preference! name send self.class.preference_setter_method(name), value end |