Module: Spree::Preferences::Preferable

Extended by:
ActiveSupport::Concern
Included in:
Base
Defined in:
lib/spree/core/preferences/preferable.rb

Instance Method Summary collapse

Instance Method Details

#clear_preferencesObject



103
104
105
# File 'lib/spree/core/preferences/preferable.rb', line 103

def clear_preferences
  preferences.keys.each { |pref| preferences.delete pref }
end

#default_preferencesObject



91
92
93
94
95
96
97
# File 'lib/spree/core/preferences/preferable.rb', line 91

def default_preferences
  Hash[
    defined_preferences.map do |preference|
      [preference, preference_default(preference)]
    end
  ]
end

#defined_preferencesObject



78
79
80
81
82
# File 'lib/spree/core/preferences/preferable.rb', line 78

def defined_preferences
  methods.grep(/\Apreferred_.*=\Z/).map do |pref_method|
    pref_method.to_s.gsub(/\Apreferred_|=\Z/, '').to_sym
  end
end

#deprecated_preferencesObject



84
85
86
87
88
89
# File 'lib/spree/core/preferences/preferable.rb', line 84

def deprecated_preferences
  defined_preferences.each_with_object([]) do |pref_name, array|
    deprecated_message = preference_deprecated(pref_name)
    array << { name: pref_name, message: deprecated_message } unless deprecated_message.nil?
  end
end

#get_preference(name) ⇒ Object



45
46
47
48
# File 'lib/spree/core/preferences/preferable.rb', line 45

def get_preference(name)
  has_preference! name
  send self.class.preference_getter_method(name)
end

#has_preference!(name) ⇒ Object

Raises:

  • (NoMethodError)


70
71
72
# File 'lib/spree/core/preferences/preferable.rb', line 70

def has_preference!(name)
  raise NoMethodError, "#{name} preference not defined" unless has_preference? name
end

#has_preference?(name) ⇒ Boolean



74
75
76
# File 'lib/spree/core/preferences/preferable.rb', line 74

def has_preference?(name)
  respond_to? self.class.preference_getter_method(name)
end

#preference_change(name, changes_or_previous_changes) ⇒ Object



111
112
113
114
115
116
117
118
119
# File 'lib/spree/core/preferences/preferable.rb', line 111

def preference_change(name, changes_or_previous_changes)
  preference_changes = changes_or_previous_changes.with_indifferent_access.fetch('preferences', [{}, {}])
  before_preferences = preference_changes[0] || {}
  after_preferences = preference_changes[1] || {}

  return if before_preferences[name] == after_preferences[name]

  [before_preferences[name], after_preferences[name]]
end

#preference_default(name) ⇒ Object



60
61
62
63
# File 'lib/spree/core/preferences/preferable.rb', line 60

def preference_default(name)
  has_preference! name
  send self.class.preference_default_getter_method(name)
end

#preference_deprecated(name) ⇒ Object



65
66
67
68
# File 'lib/spree/core/preferences/preferable.rb', line 65

def preference_deprecated(name)
  has_preference! name
  send(self.class.preference_deprecated_getter_method(name))
end

#preference_type(name) ⇒ Object



55
56
57
58
# File 'lib/spree/core/preferences/preferable.rb', line 55

def preference_type(name)
  has_preference! name
  send self.class.preference_type_getter_method(name)
end

#preferences_of_type(type) ⇒ Object



99
100
101
# File 'lib/spree/core/preferences/preferable.rb', line 99

def preferences_of_type(type)
  defined_preferences.find_all { |preference| preference_type(preference) == type.to_sym }
end

#restore_preferences_for(preference_keys) ⇒ Object



107
108
109
# File 'lib/spree/core/preferences/preferable.rb', line 107

def restore_preferences_for(preference_keys)
  preference_keys.each { |pref| preferences[pref] = preference_default(pref) }
end

#set_preference(name, value) ⇒ Object



50
51
52
53
# File 'lib/spree/core/preferences/preferable.rb', line 50

def set_preference(name, value)
  has_preference! name
  send self.class.preference_setter_method(name), value
end