Class: Spree::Preferences::Configuration

Inherits:
Object
  • Object
show all
Includes:
Preferable
Defined in:
app/models/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

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

Instance Attribute Details

- (Object) preference_store Also known as: preferences

Storage method for preferences. Default is ScopedStore



35
# File 'app/models/spree/preferences/configuration.rb', line 35

attr_writer :preference_store

Class Method Details

+ (Object) preference(name, type, options = {})



72
73
74
75
76
# File 'app/models/spree/preferences/configuration.rb', line 72

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

Instance Method Details

- (Object) configure {|config| ... }

Yields:

  • (config)

    Yields this configuration object to a block



29
30
31
# File 'app/models/spree/preferences/configuration.rb', line 29

def configure
  yield(self)
end

- (Object) reset

Reset all preferences to their default values.



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

def reset
  set(default_preferences)
end

- (Object) set(preferences)

Parameters:

  • preferences (Hash)

    a hash of preferences to set



66
67
68
69
70
# File 'app/models/spree/preferences/configuration.rb', line 66

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

- (Object) use_static_preferences!

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.



49
50
51
# File 'app/models/spree/preferences/configuration.rb', line 49

def use_static_preferences!
  @preference_store = default_preferences
end