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.



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

def initialize(request)
  @request = request
end

Instance Method Details

#storeSpree::Store

Chooses the current store based on a request.

Returns:



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

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(url: server_name)
end