Module: Spree::StorePreferences

Extended by:
ActiveSupport::Concern
Included in:
Fulfillment, PaymentMethod, Price, Product, Variant
Defined in:
app/models/concerns/spree/store_preferences.rb

Overview

Reads a store-scoped commerce setting from a record that is not itself a store.

Spree 6 moved settings that shape how a shop sells from Spree::Config onto Spree::Store, and core reads the store with no fallback to the old global — two sources of truth are what let them drift apart. Including models say how they reach a store by defining preference_store; when they can't reach one (a product with no store, a class-level scope, a console session), the read falls back to the preference's own declared default rather than raising.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.current(name) ⇒ Object

Reads a setting outside any record — class-level scopes and services that have no owning row to ask. Resolves the ambient store.

Parameters:

  • name (Symbol) —

    preference declared on Spree::Store

Returns:

  • (Object) —

    the current store's value, or the preference default



44
45
46
# File 'app/models/concerns/spree/store_preferences.rb', line 44

def current(name)
  read(Spree::Current.store, name)
end

.read(store, name) ⇒ Object

Returns the store's value, or the preference default.

Parameters:

  • store (Spree::Store, nil)
  • name (Symbol) —

    preference declared on Spree::Store

Returns:

  • (Object) —

    the store's value, or the preference default



32
33
34
35
36
37
38
# File 'app/models/concerns/spree/store_preferences.rb', line 32

def read(store, name)
  return store.get_preference(name) if store

  # `preference_default` is defined per instance, not on the class, and
  # an unsaved store is enough to read a declared default.
  Spree::Store.new.preference_default(name)
end

Instance Method Details

#preference_store ⇒ Spree::Store?

The store whose settings apply to this record. Override in models whose route to a store isn't a store association.

Returns:



24
25
26
# File 'app/models/concerns/spree/store_preferences.rb', line 24

def preference_store
  respond_to?(:store) ? store : nil
end

#store_preference(name) ⇒ Object

Returns the store's value, or the preference default.

Parameters:

  • name (Symbol) —

    preference declared on Spree::Store

Returns:

  • (Object) —

    the store's value, or the preference default



17
18
19
# File 'app/models/concerns/spree/store_preferences.rb', line 17

def store_preference(name)
  Spree::StorePreferences.read(preference_store, name)
end