Class: Spree::Preferences::Configuration

Inherits:
Object
  • Object
show all
Includes:
Preferable
Defined in:
lib/spree/preferences/configuration.rb

Overview

This takes the preferrable methods and adds some syntatic sugar to access the preferences

class App < Configuration
  preference :color, :string
end

a = App.new

Provides the following setters:

a.color = :blue
a[:color] = :blue
a.set color: :blue
a.preferred_color = :blue

and the following getters:

a.color
a[:color]
a.get :color
a.preferred_color

Direct Known Subclasses

AppConfiguration

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Preferable

#admin_form_preference_names, #default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference

Instance Attribute Details

#preference_storeObject Also known as: preferences

Storage method for preferences.



39
# File 'lib/spree/preferences/configuration.rb', line 39

attr_writer :preference_store

Class Method Details

.class_name_attribute(name, default:) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/spree/preferences/configuration.rb', line 88

def self.class_name_attribute(name, default:)
  ivar = :"@#{name}"

  define_method("#{name}=") do |class_name|
    # If this is a named class constant, we should store it as a string to
    # allow code reloading.
    class_name = class_name.name if class_name.is_a?(Class) && class_name.name

    instance_variable_set(ivar, class_name)
  end

  define_method(name) do
    class_name = instance_variable_get(ivar)
    class_name ||= default
    class_name = class_name.constantize if class_name.is_a?(String)
    class_name
  end
end

.preference(name, type, options = {}) ⇒ Object



82
83
84
85
86
# File 'lib/spree/preferences/configuration.rb', line 82

def self.preference(name, type, options = {})
  super
  alias_method name.to_s, "preferred_#{name}"
  alias_method "#{name}=", "preferred_#{name}="
end

Instance Method Details

#configure {|config| ... } ⇒ Object

Yields:

  • (config)

    Yields this configuration object to a block



33
34
35
# File 'lib/spree/preferences/configuration.rb', line 33

def configure
  yield(self)
end

#resetObject

Reset all preferences to their default values.



66
67
68
# File 'lib/spree/preferences/configuration.rb', line 66

def reset
  set(default_preferences)
end

#set(preferences) ⇒ Object

Parameters:

  • preferences (Hash)

    a hash of preferences to set



76
77
78
79
80
# File 'lib/spree/preferences/configuration.rb', line 76

def set(preferences)
  preferences.each do |name, value|
    set_preference name, value
  end
end

#use_legacy_db_preferences!Object

Replace the new static preference store with the legacy store which fetches preferences from the DB.



59
60
61
# File 'lib/spree/preferences/configuration.rb', line 59

def use_legacy_db_preferences!
  @preference_store = ScopedStore.new(self.class.name.underscore)
end

#use_static_preferences!Object

Replace the default legacy preference store, which stores preferences in the spree_preferences table, with a plain in memory hash. This is faster and less error prone.

This will set all preferences to their default values.

These won’t be loaded from or persisted to the database, so any desired changes must be made each time the application is started, such as in an initializer.



53
54
55
# File 'lib/spree/preferences/configuration.rb', line 53

def use_static_preferences!
  @preference_store = default_preferences
end