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

#currencyString

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

Returns:

  • (String)

    currency ISO code, e.g. “USD”



26
27
28
# File 'app/models/spree/current.rb', line 26

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

#default_tax_zoneSpree::Zone?

Returns the default tax zone (memoized per request).

Returns:



46
47
48
49
50
51
52
# File 'app/models/spree/current.rb', line 46

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.



65
66
67
68
69
70
71
72
73
74
# File 'app/models/spree/current.rb', line 65

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

#localeString

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

Returns:

  • (String)

    locale code, e.g. “en”, “de”



33
34
35
# File 'app/models/spree/current.rb', line 33

def locale
  super || market&.default_locale || store&.default_locale
end

#marketSpree::Market?

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

Returns:



19
20
21
# File 'app/models/spree/current.rb', line 19

def market
  super || store&.default_market
end

#price_listsActiveRecord::Relation<Spree::PriceList>

Returns the current price lists for the global pricing context.

Returns:



56
57
58
59
60
61
# File 'app/models/spree/current.rb', line 56

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:



40
41
42
# File 'app/models/spree/current.rb', line 40

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