Skip to content

Commit

Permalink
Always use the default store
Browse files Browse the repository at this point in the history
This commit avoids doing 2 extra queries for every request (solidusio#1971)
even when solidus store is not using solidus_multi_domain extension.

This logic has now been moved to the solidus_multi_domain extension
since it is needed only when multiple stores are expected
(solidusio-contrib/solidus_multi_domain#67).
  • Loading branch information
kennyadsl committed Jun 16, 2017
1 parent f0cabac commit bb83dc0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 57 deletions.
23 changes: 8 additions & 15 deletions core/app/models/spree/current_store_selector.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
# Default class for deciding what the current store is, given an HTTP request
# This is an extension point used in Spree::Core::ControllerHelpers::Store
#
# To use a custom version of this class just set the preference:
# Spree::Config.current_store_selector_class = CustomCurrentStoreSelector
#
# 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.
# Select the store to be used. In this basic implementation the
# default store will be always selected.
#
# @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']
Spree::Store.default
end
end
end
22 changes: 1 addition & 21 deletions core/spec/lib/spree/core/current_store_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,12 @@
subject { Spree::Deprecation.silence { Spree::Core::CurrentStore.new(request).store } }

context "with a default" do
let(:request) { double(headers: {}, env: {}) }
let(:request) { double('any request') }
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

it 'is deprecated' do
Expand Down
22 changes: 1 addition & 21 deletions core/spec/models/spree/current_store_selector_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,12 @@
subject { Spree::CurrentStoreSelector.new(request).store }

context "with a default" do
let(:request) { double(headers: {}, env: {}) }
let(:request) { double('any request') }
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 bb83dc0

Please sign in to comment.