Class: Spree::StoreSelector::ByServerName

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree/store_selector/by_server_name.rb

Instance Method Summary collapse

Constructor Details

#initialize(request) ⇒ ByServerName

Returns a new instance of ByServerName



11
12
13
# File 'app/models/spree/store_selector/by_server_name.rb', line 11

def initialize(request)
  @request = request
end

Instance Method Details

#storeSpree::Store

Chooses the current store based on a request.

Returns:



17
18
19
20
21
22
23
24
25
26
27
# File 'app/models/spree/store_selector/by_server_name.rb', line 17

def store
  server_name = @request.env['SERVER_NAME']

  # We select a store which either matches our server name, or is default.
  # We sort by `default ASC` so that a store matching SERVER_NAME will come
  # first, and we will find that instead of the default.
  store = Spree::Store.where(url: server_name).or(Store.where(default: true)).order(default: :asc).first

  # Provide a fallback, mostly for legacy/testing purposes
  store || Spree::Store.new
end