Skip to content

Commit

Permalink
Merge pull request #1239 from solidusio/fix_store_current_without_args
Browse files Browse the repository at this point in the history
Fix Store.current without args
  • Loading branch information
jhawthorn authored Jun 29, 2016
2 parents 6a6ac5e + a682147 commit bcdf549
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
3 changes: 2 additions & 1 deletion core/app/models/spree/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 5 additions & 1 deletion core/lib/spree/core/current_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 24 additions & 10 deletions core/spec/models/spree/store_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit bcdf549

Please sign in to comment.