Skip to content

Commit

Permalink
Add a class that selects the store using requests info.
Browse files Browse the repository at this point in the history
This class logic is extracted from Solidus core, which is going
to be simplified a lot to avoid multiple queries at each request
if solidus_multi_domain extension is not used (solidusio/solidus#1971).
  • Loading branch information
kennyadsl committed Jun 15, 2017
1 parent 178a4b5 commit adb314c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
26 changes: 26 additions & 0 deletions app/models/spree/multi_domain_store_selector.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Class for deciding what the current store is, given an HTTP request
module Spree
class MultiDomainStoreSelector
def initialize(request)
@request = request
end

# Chooses the current store based on a request.
# Checks request headers for HTTP_SPREE_STORE and falls back to
# looking up by the requesting server's name.
# @return [Spree::Store]
def store
if store_key
Spree::Store.current(store_key)
else
Spree::Store.default
end
end

private

def store_key
@request.headers['HTTP_SPREE_STORE'] || @request.env['SERVER_NAME']
end
end
end
9 changes: 9 additions & 0 deletions lib/spree_multi_domain/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ def activate
end
end

# Previous versions of solidus has the multi domain store selection
# logic directly in core (called Spree::Core::CurrentStore or
# Spree::CurrentStoreSelector). As long as Spree::Config responds to
# current_store_selector_class we can use the new domain selector
# class safely.
if Spree::Config.respond_to?(:current_store_selector_class)
Spree::Config.current_store_selector_class = Spree::MultiDomainStoreSelector
end

Spree::Config.searcher_class = Spree::Search::MultiDomain
ApplicationController.send :include, SpreeMultiDomain::MultiDomainHelpers
end
Expand Down
36 changes: 36 additions & 0 deletions spec/models/spree/multi_domain_store_selector_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'spec_helper'

describe Spree::MultiDomainStoreSelector do
describe "#store" do
subject { Spree::MultiDomainStoreSelector.new(request).store }

context "with a default" do
let(:request) { double(headers: {}, env: {}) }
let!(:store_1) { create :store, default: true }

it "returns the default store" do
expect(subject).to eq(store_1)
end

context "with a domain match" do
let(:request) { double(headers: {}, env: { "SERVER_NAME" => url } ) }
let(:url) { "server-name.org" }
let!(:store_2) { create :store, default: false, url: url }

it "returns the store with the matching domain" do
expect(subject).to eq(store_2)
end

context "with headers" do
let(:request) { double(headers: { "HTTP_SPREE_STORE" => headers_code }, env: {}) }
let(:headers_code) { "HEADERS" }
let!(:store_3) { create :store, code: headers_code, default: false }

it "returns the store with the matching code" do
expect(subject).to eq(store_3)
end
end
end
end
end
end

0 comments on commit adb314c

Please sign in to comment.