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

#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:



50
51
52
53
54
55
56
# File 'app/models/spree/current.rb', line 50

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.



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

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”



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

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:



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:



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

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:



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

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