diff --git a/core/app/models/spree/store.rb b/core/app/models/spree/store.rb index c263884b596..c3cc9af2a83 100644 --- a/core/app/models/spree/store.rb +++ b/core/app/models/spree/store.rb @@ -15,7 +15,8 @@ class Store < Spree::Base scope :by_url, lambda { |url| where("url like ?", "%#{url}%") } def self.current(store_key = nil) - current_store = Store.find_by(code: store_key) || Store.by_url(store_key).first + Spree::Deprecation.warn "Spree::Store.current needs a code or URL as an argument. If you want the default store use Spree::Store.default", caller if !store_key + current_store = Store.find_by(code: store_key) || Store.by_url(store_key).first if store_key current_store || Store.default end diff --git a/core/lib/spree/core/current_store.rb b/core/lib/spree/core/current_store.rb index 3b9552d607c..f836ebce035 100644 --- a/core/lib/spree/core/current_store.rb +++ b/core/lib/spree/core/current_store.rb @@ -13,7 +13,11 @@ def initialize(request) # looking up by the requesting server's name. # @return [Spree::Store] def store - Spree::Store.current(store_key) + if store_key + Spree::Store.current(store_key) + else + Spree::Store.default + end end private diff --git a/core/spec/models/spree/store_spec.rb b/core/spec/models/spree/store_spec.rb index 628ca49b3a2..2155a880e7b 100644 --- a/core/spec/models/spree/store_spec.rb +++ b/core/spec/models/spree/store_spec.rb @@ -16,23 +16,37 @@ end describe '.current' do - # there is a default store created with the test_app rake task. - let!(:store_1) { Spree::Store.first || create(:store) } - + let!(:store_1) { create(:store) } + let!(:store_default) { create(:store, name: 'default', default: true) } let!(:store_2) { create(:store, default: false, url: 'www.subdomain.com') } let!(:store_3) { create(:store, default: false, url: 'www.another.com', code: 'CODE') } - it 'should return default when no domain' do - expect(subject.class.current).to eql(store_1) + delegate :current, to: :described_class + + context "with no argument" do + it 'should return default' do + Spree::Deprecation.silence do + expect(current).to eql(store_default) + end + end + end + + context "with no match" do + it 'should return the default domain' do + expect(current('foobar.com')).to eql(store_default) + end end - it 'should return store for domain' do - expect(subject.class.current('spreecommerce.com')).to eql(store_1) - expect(subject.class.current('www.subdomain.com')).to eql(store_2) + context "with matching url" do + it 'should return matching store' do + expect(current('www.subdomain.com')).to eql(store_2) + end end - it 'should return store by code' do - expect(subject.class.current('CODE')).to eql(store_3) + context "with matching code" do + it 'should return matching store' do + expect(current('CODE')).to eql(store_3) + end end end