-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a class that selects the store using requests info.
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
Showing
3 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |