Skip to content

Commit

Permalink
Merge pull request solidusio#17 from jordan-brough/store-selector
Browse files Browse the repository at this point in the history
Cherry-pick current-store-selector updates from Solidus master
  • Loading branch information
jordan-brough authored Jul 13, 2017
2 parents e14d7c4 + 1aa4ea1 commit 8560a79
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 24 deletions.
10 changes: 10 additions & 0 deletions core/app/models/spree/app_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,16 @@ def automatic_promotion_decision_class
@automatic_promotion_decision_class ||= Spree::Promotion::AutomaticPromotionDecision
end

# Allows providing your own class for choosing which store to use.
#
# @!attribute [rw] current_store_selector_class
# @return [Class] a class with the same public interfaces as
# Spree::CurrentStoreSelector
attr_writer :current_store_selector_class
def current_store_selector_class
@current_store_selector_class ||= Spree::CurrentStoreSelector
end

def static_model_preferences
@static_model_preferences ||= Spree::Preferences::StaticModelPreferences.new
end
Expand Down
28 changes: 28 additions & 0 deletions core/app/models/spree/current_store_selector.rb
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
10 changes: 1 addition & 9 deletions core/lib/spree/core/controller_helpers/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,12 @@ module ControllerHelpers
module Store
extend ActiveSupport::Concern

# @!attribute [rw] current_store_class
# @!scope class
# Extension point for overriding how the current store is chosen.
# Defaults to checking headers and server name
# @return [#store] class used to help find the current store
included do
class_attribute :current_store_class
self.current_store_class = Spree::Core::CurrentStore

helper_method :current_store
end

def current_store
@current_store ||= current_store_class.new(request).store
@current_store ||= Spree::Config.current_store_selector_class.new(request).store
end
end
end
Expand Down
20 changes: 6 additions & 14 deletions core/lib/spree/core/current_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,16 @@ module Core
class CurrentStore
def initialize(request)
@request = request
@current_store_selector = Spree::Config.current_store_selector_class.new(request)
Spree::Deprecation.warn "Using Spree::Core::CurrentStore is deprecated. Use Spree::CurrentStoreSelector instead", caller
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.
# Delegate store selection to Spree::Config.current_store_selector_class
# Using this class is deprecated.
#
# @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']
@current_store_selector.store
end
end
end
Expand Down
7 changes: 6 additions & 1 deletion core/spec/lib/spree/core/current_store_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe Spree::Core::CurrentStore do
describe "#store" do
subject { Spree::Core::CurrentStore.new(request).store }
subject { Spree::Deprecation.silence { Spree::Core::CurrentStore.new(request).store } }

context "with a default" do
let(:request) { double(headers: {}, env: {}) }
Expand Down Expand Up @@ -32,5 +32,10 @@
end
end
end

it 'is deprecated' do
expect(Spree::Deprecation).to(receive(:warn))
Spree::Core::CurrentStore.new(double)
end
end
end
36 changes: 36 additions & 0 deletions core/spec/models/spree/current_store_selector_spec.rb
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

0 comments on commit 8560a79

Please sign in to comment.