Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Added Stores::FindCurrent class, deprecated Store.current method
Damian Legawiecf3414d67b92a
diff --git a/core/app/finders/spree/stores/find_current.rb b/core/app/finders/spree/stores/find_current.rbnew file mode 100644index 0000000000..647d23f6c5--- /dev/null+++ b/core/app/finders/spree/stores/find_current.rb@@ -0,0 +1,24 @@+module Spree+ module Stores+ class FindCurrent+ def initialize(scope: nil, url: nil)+ @scope = scope || Spree::Store+ @url = url+ end++ def execute+ by_url(scope) || scope.default+ end++ protected++ attr_reader :scope, :url++ def by_url(scope)+ return if url.blank?++ scope.by_url(url).first+ end+ end+ end+enddiff --git a/core/app/mailers/spree/base_mailer.rb b/core/app/mailers/spree/base_mailer.rbindex 8de01c43df..121beaded5 100644--- a/core/app/mailers/spree/base_mailer.rb+++ b/core/app/mailers/spree/base_mailer.rb@@ -3,7 +3,7 @@ module Spreehelper Spree::MailHelperdef current_store- @current_store ||= Spree::Store.current+ @current_store ||= Spree::Dependencies.current_store_finder.constantize.new.executeendhelper_method :current_storediff --git a/core/app/mailers/spree/reimbursement_mailer.rb b/core/app/mailers/spree/reimbursement_mailer.rbindex feca26d5f7..0f258d2f41 100644--- a/core/app/mailers/spree/reimbursement_mailer.rb+++ b/core/app/mailers/spree/reimbursement_mailer.rb@@ -3,7 +3,7 @@ module Spreedef reimbursement_email(reimbursement, resend = false)@reimbursement = reimbursement.respond_to?(:id) ? reimbursement : Spree::Reimbursement.find(reimbursement)@order = @reimbursement.order- current_store = @reimbursement.store || Spree::Store.current+ current_store = @reimbursement.store || Spree::Store.defaultsubject = (resend ? "[#{Spree.t(:resend).upcase}] " : '')subject += "#{current_store.name} #{Spree.t('reimbursement_mailer.reimbursement_email.subject')} ##{@order.number}"mail(to: @order.email, from: current_store.mail_from_address, subject: subject, store_url: current_store.url)diff --git a/core/app/mailers/spree/test_mailer.rb b/core/app/mailers/spree/test_mailer.rbindex 804cb8f169..03b43cdbfe 100644--- a/core/app/mailers/spree/test_mailer.rb+++ b/core/app/mailers/spree/test_mailer.rb@@ -1,7 +1,7 @@module Spreeclass TestMailer < BaseMailerdef test_email(email)- subject = "#{Spree::Store.current.name} #{Spree.t('test_mailer.test_email.subject')}"+ subject = "#{Spree::Store.default.name} #{Spree.t('test_mailer.test_email.subject')}"mail(to: email, from: from_address, subject: subject)endenddiff --git a/core/app/models/spree/app_dependencies.rb b/core/app/models/spree/app_dependencies.rbindex 594ffd9165..8635cedde1 100644--- a/core/app/models/spree/app_dependencies.rb+++ b/core/app/models/spree/app_dependencies.rb@@ -13,7 +13,7 @@ module Spree:completed_order_finder, :order_sorter, :cart_compare_line_items_service, :collection_paginator, :products_sorter,:products_finder, :taxon_finder, :line_item_by_variant_finder, :cart_estimate_shipping_rates_service,:account_create_address_service, :account_update_address_service, :account_create_service, :account_update_service,- :address_finder, :collection_sorter, :error_handler+ :address_finder, :collection_sorter, :error_handler, :current_store_finder].freezeattr_accessor *INJECTION_POINTS@@ -77,6 +77,7 @@ module Spree@country_finder = 'Spree::Countries::Find'@menu_finder = 'Spree::Menus::Find'@current_order_finder = 'Spree::Orders::FindCurrent'+ @current_store_finder = 'Spree::Stores::FindCurrent'@completed_order_finder = 'Spree::Orders::FindComplete'@credit_card_finder = 'Spree::CreditCards::Find'@products_finder = 'Spree::Products::Find'diff --git a/core/app/models/spree/store.rb b/core/app/models/spree/store.rbindex 46f47a8562..7749a00e9b 100644--- a/core/app/models/spree/store.rb+++ b/core/app/models/spree/store.rb@@ -47,9 +47,12 @@ module Spreealias_attribute :contact_email, :customer_support_email- def self.current(domain = nil)- current_store = domain ? Store.by_url(domain).first : nil- current_store || Store.default+ def self.current(url = nil)+ ActiveSupport::Deprecation.warn(<<-DEPRECATION, caller)+ `Spree::Store.default` is deprecated and will be removed in Spree 5+ Spree::Stores::FindCurrent.new(url: "http://example.com") instead+ DEPRECATION+ Stores::FindCurrent.new(url: url).executeenddef self.defaultdiff --git a/core/lib/spree/core/controller_helpers/store.rb b/core/lib/spree/core/controller_helpers/store.rbindex c3701c8067..46092a3731 100644--- a/core/lib/spree/core/controller_helpers/store.rb+++ b/core/lib/spree/core/controller_helpers/store.rb@@ -11,7 +11,7 @@ module Spreeenddef current_store- @current_store ||= Spree::Store.current(request.env['SERVER_NAME'])+ @current_store ||= current_store_finder.new(url: request.env['HTTP_SERVER_NAME']).executeenddef available_menus@@ -49,6 +49,10 @@ module Spreedef current_tax_zone@current_tax_zone ||= @current_order&.tax_zone || Spree::Zone.default_taxend++ def current_store_finder+ Spree::Dependencies.current_store_finder.constantize+ endendendenddiff --git a/core/spec/finders/spree/stores/find_current_spec.rb b/core/spec/finders/spree/stores/find_current_spec.rbnew file mode 100644index 0000000000..6bb493fff1--- /dev/null+++ b/core/spec/finders/spree/stores/find_current_spec.rb@@ -0,0 +1,36 @@+require 'spec_helper'++module Spree+ describe Stores::FindCurrent do+ subject { described_class.new(scope: scope, url: url).execute }++ let!(:store) { create(:store, default: true) }+ let!(:store_2) { create(:store, url: 'another.com', default_currency: 'GBP') }++ let(:scope) { nil }+ let(:url) { nil }++ context 'no arguments' do+ it { expect(subject).to eq(store) }+ end++ context 'existing store' do+ let(:url) { 'another.com' }++ it { expect(subject).to eq(store_2) }+ end++ context 'non-existing store' do+ let(:url) { 'something-different.com' }++ it { expect(subject).to eq(store) }+ end++ context 'with scope' do+ let(:scope) { Spree::Store.where(default_currency: 'GBP') }+ let(:url) { 'another.com' }++ it { expect(subject).to eq(store_2) }+ end+ end+enddiff --git a/core/spec/lib/spree/core/controller_helpers/store_spec.rb b/core/spec/lib/spree/core/controller_helpers/store_spec.rbindex 23b968a21f..f618058fa9 100644--- a/core/spec/lib/spree/core/controller_helpers/store_spec.rb+++ b/core/spec/lib/spree/core/controller_helpers/store_spec.rb@@ -14,9 +14,34 @@ describe Spree::Core::ControllerHelpers::Store, type: :controller dodescribe '#current_store' dolet!(:store) { create :store, default: true }+ let!(:store_2) { create :store, url: 'another.com' }- it 'returns current store' do- expect(controller.current_store).to eq store+ context 'default store' do+ it 'returns current store' do+ expect(controller.current_store).to eq store+ end+ end++ context 'by domain' do+ before do+ controller.request.env['HTTP_SERVER_NAME'] = 'another.com'+ end++ it 'returns current store' do+ expect(controller.current_store).to eq store_2+ end+ end++ context 'by subdomain' do+ let!(:store_3) { create :store, url: 'some.another.com' }++ before do+ controller.request.env['HTTP_SERVER_NAME'] = 'some.another.com'+ end++ it 'returns current store' do+ expect(controller.current_store).to eq store_3+ endendenddiff --git a/core/spec/mailers/order_mailer_spec.rb b/core/spec/mailers/order_mailer_spec.rbindex 7060753045..50d8ab6a79 100644--- a/core/spec/mailers/order_mailer_spec.rb+++ b/core/spec/mailers/order_mailer_spec.rb@@ -41,7 +41,7 @@ describe Spree::OrderMailer, type: :mailer docontext ':from not set explicitly' doit 'falls back to spree config' domessage = Spree::OrderMailer.confirm_email(order)- expect(message.from).to eq([Spree::Store.current.mail_from_address])+ expect(message.from).to eq([Spree::Store.default.mail_from_address])endenddiff --git a/core/spec/mailers/reimbursement_mailer_spec.rb b/core/spec/mailers/reimbursement_mailer_spec.rbindex 7e1eb438b3..e7ff50f9a3 100644--- a/core/spec/mailers/reimbursement_mailer_spec.rb+++ b/core/spec/mailers/reimbursement_mailer_spec.rb@@ -10,7 +10,7 @@ describe Spree::ReimbursementMailer, type: :mailer docontext ':from not set explicitly' doit 'falls back to spree config' domessage = Spree::ReimbursementMailer.reimbursement_email(reimbursement)- expect(message.from).to eq [Spree::Store.current.mail_from_address]+ expect(message.from).to eq [Spree::Store.default.mail_from_address]endenddiff --git a/core/spec/mailers/shipment_mailer_spec.rb b/core/spec/mailers/shipment_mailer_spec.rbindex 5fec94a7f7..98b2a26fb2 100644--- a/core/spec/mailers/shipment_mailer_spec.rb+++ b/core/spec/mailers/shipment_mailer_spec.rb@@ -23,7 +23,7 @@ describe Spree::ShipmentMailer, type: :mailer docontext ':from not set explicitly' doit 'falls back to spree config' domessage = Spree::ShipmentMailer.shipped_email(shipment)- expect(message.from).to eq([Spree::Store.current.mail_from_address])+ expect(message.from).to eq([Spree::Store.default.mail_from_address])endenddiff --git a/core/spec/mailers/test_mailer_spec.rb b/core/spec/mailers/test_mailer_spec.rbindex 8d69646557..d42f092b12 100644--- a/core/spec/mailers/test_mailer_spec.rb+++ b/core/spec/mailers/test_mailer_spec.rb@@ -12,7 +12,7 @@ describe Spree::TestMailer, type: :mailer docontext ':from not set explicitly' doit 'falls back to spree config' domessage = Spree::TestMailer.test_email('[email protected]')- expect(message.from).to eq([Spree::Store.current.mail_from_address])+ expect(message.from).to eq([Spree::Store.default.mail_from_address])endend@@ -26,7 +26,7 @@ describe Spree::TestMailer, type: :mailer doit 'falls back to spree store url' doActionMailer::Base.default_url_options = {}Spree::TestMailer.test_email('[email protected]').deliver_now- expect(ActionMailer::Base.default_url_options[:host]).to eq(Spree::Store.current.url)+ expect(ActionMailer::Base.default_url_options[:host]).to eq(Spree::Store.default.url)endit 'uses developer set host' do