Module: Spree::Preferences::Preferable

Extended by:
ActiveSupport::Concern
Included in:
Base, Configuration
Defined in:
app/models/spree/preferences/preferable.rb

Overview

Preferable allows defining preference accessor methods.

A class including Preferable must implement #preferences which should return an object responding to .fetch(key), []=(key, val), and .delete(key).

The generated writer method performs typecasting before assignment into the preferences object.

Examples:

# Spree::Base includes Preferable and defines preferences as a serialized
# column.
class Settings < Spree::Base
  preference :color,       :string,  default: 'red'
  preference :temperature, :integer, default: 21
end

s = Settings.new
s.preferred_color # => 'red'
s.preferred_temperature # => 21

s.preferred_color = 'blue'
s.preferred_color # => 'blue'

# Typecasting is performed on assignment
s.preferred_temperature = '24'
s.preferred_color # => 24

# Modifications have been made to the .preferences hash
s.preferences #=> {color: 'blue', temperature: 24}

# Save the changes. All handled by activerecord
s.save!

Instance Method Summary collapse

Instance Method Details

#clear_preferencesObject



83
84
85
# File 'app/models/spree/preferences/preferable.rb', line 83

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

#default_preferencesObject



75
76
77
78
79
80
81
# File 'app/models/spree/preferences/preferable.rb', line 75

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

#defined_preferencesObject



69
70
71
72
73
# File 'app/models/spree/preferences/preferable.rb', line 69

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

#get_preference(name) ⇒ Object



41
42
43
44
# File 'app/models/spree/preferences/preferable.rb', line 41

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

#has_preference!(name) ⇒ Object

Raises:

  • (NoMethodError)


61
62
63
# File 'app/models/spree/preferences/preferable.rb', line 61

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

#has_preference?(name) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'app/models/spree/preferences/preferable.rb', line 65

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

#preference_default(name) ⇒ Object



56
57
58
59
# File 'app/models/spree/preferences/preferable.rb', line 56

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

#preference_type(name) ⇒ Object



51
52
53
54
# File 'app/models/spree/preferences/preferable.rb', line 51

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

#set_preference(name, value) ⇒ Object



46
47
48
49
# File 'app/models/spree/preferences/preferable.rb', line 46

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