Class: Spree::Current

Inherits:
ActiveSupport::CurrentAttributes
  • Object
show all
Defined in:
app/models/spree/current.rb

Overview

Thread-safe, per-request attributes for the current store context.

All attributes are automatically reset between requests by Rails. Fallback chains ensure sensible defaults when attributes are not explicitly set.

Instance Method Summary collapse

Instance Method Details

#channelObject



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

def channel
  super || (self.channel = store&.default_channel)
end

#content_localeString

Returns the locale that base (untranslated) record columns are authored in for the current request — the current store's default locale. It is assigned per request alongside I18n.locale and must never be written to the process-global I18n.default_locale, which every thread in the server process shares. Outside a request it falls back to the application default locale.

Returns:

  • (String)

    locale code, e.g. "en", "de"



48
49
50
# File 'app/models/spree/current.rb', line 48

def content_locale
  super.presence || I18n.default_locale.name
end

#currencyString

Returns the current currency. Fallback: market currency -> store default currency.

Returns:

  • (String)

    currency ISO code, e.g. "USD"



30
31
32
# File 'app/models/spree/current.rb', line 30

def currency
  super || market&.currency || store&.default_currency
end

#default_tax_zoneSpree::Zone?

Returns the default tax zone (memoized per request).

Returns:



61
62
63
64
65
66
67
# File 'app/models/spree/current.rb', line 61

def default_tax_zone
  result = super
  return result if result || @default_tax_zone_loaded

  @default_tax_zone_loaded = true
  self.default_tax_zone = Spree::Zone.find_by(default_tax: true)
end

#global_pricing_contextSpree::Pricing::Context

Returns the current global pricing context, built from store, currency, zone, and market.



80
81
82
83
84
85
86
87
88
89
90
# File 'app/models/spree/current.rb', line 80

def global_pricing_context
  super || begin
    self.global_pricing_context = Spree::Pricing::Context.new(
      currency: currency,
      store: store,
      zone: zone,
      market: market,
      channel: channel
    )
  end
end

#localeString

Returns the current locale. Fallback: market default locale -> store default locale -> I18n default.

Returns:

  • (String)

    locale code, e.g. "en", "de"



37
38
39
# File 'app/models/spree/current.rb', line 37

def locale
  super || market&.default_locale.presence || store&.default_locale.presence || I18n.default_locale.to_s
end

#marketSpree::Market?

Returns the current market, falling back to the store's default market.

Returns:



23
24
25
# File 'app/models/spree/current.rb', line 23

def market
  super || store&.default_market
end

#price_listsActiveRecord::Relation<Spree::PriceList>

Returns the current price lists for the global pricing context.

Returns:



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

def price_lists
  super || begin
    context = global_pricing_context
    self.price_lists = Spree::PriceList.for_context(context)
  end
end

#storeSpree::Store

Returns the current store, falling back to the default store.

Returns:



13
14
15
# File 'app/models/spree/current.rb', line 13

def store
  super || Spree::Store.default
end

#zoneSpree::Zone?

Returns the current tax zone. Fallback: market's tax zone (from default country) -> global default tax zone.

Returns:



55
56
57
# File 'app/models/spree/current.rb', line 55

def zone
  super || market&.tax_zone || default_tax_zone
end