-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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 #2041 from jhawthorn/select_store_single_query
Simplify current store selection
- Loading branch information
Showing
11 changed files
with
133 additions
and
44 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
This file was deleted.
Oops, something went wrong.
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,30 @@ | ||
# Default implementation for finding the current store is given an HTTP request | ||
# | ||
# This is the new default behaviour, starting in Solidus 2.3.0. For the old | ||
# behaviour see Spree::StoreSelector::Legacy. | ||
# | ||
# This attempts to find a Spree::Store with a URL matching the domain name of | ||
# the request exactly. Failing that it will return the store marked as default. | ||
module Spree | ||
module StoreSelector | ||
class ByServerName | ||
def initialize(request) | ||
@request = request | ||
end | ||
|
||
# Chooses the current store based on a request. | ||
# @return [Spree::Store] | ||
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 | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# This class provides the old behaviour for finding a matching Spree::Store | ||
# based on a request. | ||
# | ||
# To enable this, somewhere inside config/initializers/ add | ||
# | ||
# Spree::Config.current_store_selector_class = Spree::StoreSelector::Legacy | ||
# | ||
# This classes behaviour is somewhat complicated and has issues, which is why | ||
# it has been replaced with Spree::StoreSelector::ByServerName by default. | ||
# | ||
# It will: | ||
# * Find a "store_key" | ||
# * from the HTTP_SPREE_STORE header, if it exists | ||
# * or the server's domain name if HTTP_SPREE_STORE isn't set | ||
# * Find a store, using the first match of: | ||
# * having a code matching the store_key exactly | ||
# * having a url which contains the store_key anywhere as a substring | ||
# * has default set to true | ||
# | ||
module Spree | ||
module StoreSelector | ||
class Legacy | ||
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 | ||
current_store = | ||
if store_key | ||
Spree::Store.find_by(code: store_key) || | ||
Store.where("url like ?", "%#{store_key}%").first | ||
end | ||
|
||
current_store || Spree::Store.default | ||
end | ||
|
||
private | ||
|
||
def store_key | ||
@request.headers['HTTP_SPREE_STORE'] || @request.env['SERVER_NAME'] | ||
end | ||
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
26 changes: 26 additions & 0 deletions
26
core/spec/models/spree/store_selector/by_server_name_spec.rb
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 @@ | ||
require 'spec_helper' | ||
|
||
describe Spree::StoreSelector::ByServerName do | ||
describe "#store" do | ||
subject { described_class.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 | ||
end | ||
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