forked from solidusio/solidus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request solidusio#17 from jordan-brough/store-selector
Cherry-pick current-store-selector updates from Solidus master
- Loading branch information
Showing
6 changed files
with
87 additions
and
24 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
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,28 @@ | ||
# Default class for deciding what the current store is, given an HTTP request | ||
# This is an extension point used in Spree::Core::ControllerHelpers::Store | ||
# Custom versions of this class must respond to a store instance method | ||
module Spree | ||
class CurrentStoreSelector | ||
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
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::CurrentStoreSelector do | ||
describe "#store" do | ||
subject { Spree::CurrentStoreSelector.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 |