Skip to content

armgit / spree

Public Imported from GitHub
0

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.

Extract multi store features into `spree_multi_store` gem (#13614)

Damian Legawiec5408b0b48099
diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml
index 940685206f..87680472fe 100644
--- a/.github/workflows/tests.yml
+++ b/.github/workflows/tests.yml
@@ -52,7 +52,7 @@ jobs:
- name: Install all project dependencies
run: |
- for project in core api admin emails; do
+ for project in core api admin emails multi_store; do
echo "--- Installing gems for $project ---"
cd backend/engines/$project
bundle config set --local path ${{ github.workspace }}/vendor/bundle
@@ -91,7 +91,7 @@ jobs:
- name: Install all project dependencies
run: |
- for project in core api admin emails; do
+ for project in core api admin emails multi_store; do
echo "--- Installing gems for $project ---"
cd backend/engines/$project
bundle config set --local path ${{ github.workspace }}/vendor/bundle
@@ -122,6 +122,8 @@ jobs:
- { project: admin, shard: 2, total_shards: 2 }
# emails: 1 shard
- { project: emails, shard: 1, total_shards: 1 }
+ # multi_store: 1 shard
+ - { project: multi_store, shard: 1, total_shards: 1 }
services:
postgres:
image: postgres:16
@@ -248,6 +250,8 @@ jobs:
- { project: admin, shard: 2, total_shards: 2 }
# emails: 1 shard
- { project: emails, shard: 1, total_shards: 1 }
+ # multi_store: 1 shard
+ - { project: multi_store, shard: 1, total_shards: 1 }
services:
mysql:
image: mysql:8.0
diff --git a/backend/Gemfile.lock b/backend/Gemfile.lock
index 14674a0369..d083a13287 100644
--- a/backend/Gemfile.lock
+++ b/backend/Gemfile.lock
@@ -1,6 +1,6 @@
GIT
remote: https://github.com/spree/spree_stripe.git
- revision: 78b85ac550249b21d6688b6c2ec82312d240ee87
+ revision: 2c9791452b1769573f0815d21c1d778f579f1dd2
specs:
spree_stripe (1.5.1)
spree (>= 5.3.0)
@@ -458,21 +458,21 @@ GEM
net-smtp (0.5.1)
net-protocol
nio4r (2.7.5)
- nokogiri (1.19.0-aarch64-linux-gnu)
+ nokogiri (1.19.1-aarch64-linux-gnu)
racc (~> 1.4)
- nokogiri (1.19.0-aarch64-linux-musl)
+ nokogiri (1.19.1-aarch64-linux-musl)
racc (~> 1.4)
- nokogiri (1.19.0-arm-linux-gnu)
+ nokogiri (1.19.1-arm-linux-gnu)
racc (~> 1.4)
- nokogiri (1.19.0-arm-linux-musl)
+ nokogiri (1.19.1-arm-linux-musl)
racc (~> 1.4)
- nokogiri (1.19.0-arm64-darwin)
+ nokogiri (1.19.1-arm64-darwin)
racc (~> 1.4)
- nokogiri (1.19.0-x86_64-darwin)
+ nokogiri (1.19.1-x86_64-darwin)
racc (~> 1.4)
- nokogiri (1.19.0-x86_64-linux-gnu)
+ nokogiri (1.19.1-x86_64-linux-gnu)
racc (~> 1.4)
- nokogiri (1.19.0-x86_64-linux-musl)
+ nokogiri (1.19.1-x86_64-linux-musl)
racc (~> 1.4)
octokit (4.25.1)
faraday (>= 1, < 3)
@@ -482,13 +482,14 @@ GEM
ostruct (>= 0.2)
orm_adapter (0.5.0)
ostruct (0.6.3)
- pagy (43.2.9)
+ pagy (43.2.10)
json
+ uri
yaml
parallel (1.27.0)
paranoia (3.1.0)
activerecord (>= 7, < 8.2)
- parser (3.3.10.1)
+ parser (3.3.10.2)
ast (~> 2.4.1)
racc
pg (1.6.3)
@@ -609,7 +610,7 @@ GEM
rspec-mocks (3.13.7)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
- rspec-rails (8.0.2)
+ rspec-rails (8.0.3)
actionpack (>= 7.2)
activesupport (>= 7.2)
railties (>= 7.2)
@@ -994,22 +995,22 @@ CHECKSUMS
net-protocol (0.2.2) sha256=aa73e0cba6a125369de9837b8d8ef82a61849360eba0521900e2c3713aa162a8
net-smtp (0.5.1) sha256=ed96a0af63c524fceb4b29b0d352195c30d82dd916a42f03c62a3a70e5b70736
nio4r (2.7.5) sha256=6c90168e48fb5f8e768419c93abb94ba2b892a1d0602cb06eef16d8b7df1dca1
- nokogiri (1.19.0-aarch64-linux-gnu) sha256=11a97ecc3c0e7e5edcf395720b10860ef493b768f6aa80c539573530bc933767
- nokogiri (1.19.0-aarch64-linux-musl) sha256=eb70507f5e01bc23dad9b8dbec2b36ad0e61d227b42d292835020ff754fb7ba9
- nokogiri (1.19.0-arm-linux-gnu) sha256=572a259026b2c8b7c161fdb6469fa2d0edd2b61cd599db4bbda93289abefbfe5
- nokogiri (1.19.0-arm-linux-musl) sha256=23ed90922f1a38aed555d3de4d058e90850c731c5b756d191b3dc8055948e73c
- nokogiri (1.19.0-arm64-darwin) sha256=0811dfd936d5f6dd3f6d32ef790568bf29b2b7bead9ba68866847b33c9cf5810
- nokogiri (1.19.0-x86_64-darwin) sha256=1dad56220b603a8edb9750cd95798bffa2b8dd9dd9aa47f664009ee5b43e3067
- nokogiri (1.19.0-x86_64-linux-gnu) sha256=f482b95c713d60031d48c44ce14562f8d2ce31e3a9e8dd0ccb131e9e5a68b58c
- nokogiri (1.19.0-x86_64-linux-musl) sha256=1c4ca6b381622420073ce6043443af1d321e8ed93cc18b08e2666e5bd02ffae4
+ nokogiri (1.19.1-aarch64-linux-gnu) sha256=cfdb0eafd9a554a88f12ebcc688d2b9005f9fce42b00b970e3dc199587b27f32
+ nokogiri (1.19.1-aarch64-linux-musl) sha256=1e2150ab43c3b373aba76cd1190af7b9e92103564063e48c474f7600923620b5
+ nokogiri (1.19.1-arm-linux-gnu) sha256=0a39ed59abe3bf279fab9dd4c6db6fe8af01af0608f6e1f08b8ffa4e5d407fa3
+ nokogiri (1.19.1-arm-linux-musl) sha256=3a18e559ee499b064aac6562d98daab3d39ba6cbb4074a1542781b2f556db47d
+ nokogiri (1.19.1-arm64-darwin) sha256=dfe2d337e6700eac47290407c289d56bcf85805d128c1b5a6434ddb79731cb9e
+ nokogiri (1.19.1-x86_64-darwin) sha256=7093896778cc03efb74b85f915a775862730e887f2e58d6921e3fa3d981e68bf
+ nokogiri (1.19.1-x86_64-linux-gnu) sha256=1a4902842a186b4f901078e692d12257678e6133858d0566152fe29cdb98456a
+ nokogiri (1.19.1-x86_64-linux-musl) sha256=4267f38ad4fc7e52a2e7ee28ed494e8f9d8eb4f4b3320901d55981c7b995fc23
octokit (4.25.1) sha256=c02092ee82dcdfe84db0e0ea630a70d32becc54245a4f0bacfd21c010df09b96
oj (3.16.15) sha256=4d3324cac3e8fef54c0fa250b2af26a16dadd9f9788a1d6b1b2098b793a1b2cd
orm_adapter (0.5.0) sha256=aa5d0be5d540cbb46d3a93e88061f4ece6a25f6e97d6a47122beb84fe595e9b9
ostruct (0.6.3) sha256=95a2ed4a4bd1d190784e666b47b2d3f078e4a9efda2fccf18f84ddc6538ed912
- pagy (43.2.9) sha256=6a167c8da72388883e8f4c8e29dc8f5577f9f934d8d0b704cb2f39d2cebd276e
+ pagy (43.2.10) sha256=494539ede5ca15e8a59c621f6ea89233cd0e3e142e34b2d9b2726b6dd14bc980
parallel (1.27.0) sha256=4ac151e1806b755fb4e2dc2332cbf0e54f2e24ba821ff2d3dcf86bf6dc4ae130
paranoia (3.1.0) sha256=36574a46e8df663d0da7d9008e987b72a6a9eccee13c058b7094acc810e39f52
- parser (3.3.10.1) sha256=06f6a725d2cd91e5e7f2b7c32ba143631e1f7c8ae2fb918fc4cebec187e6a688
+ parser (3.3.10.2) sha256=6f60c84aa4bdcedb6d1a2434b738fe8a8136807b6adc8f7f53b97da9bc4e9357
pg (1.6.3) sha256=1388d0563e13d2758c1089e35e973a3249e955c659592d10e5b77c468f628a99
pg (1.6.3-aarch64-linux) sha256=0698ad563e02383c27510b76bf7d4cd2de19cd1d16a5013f375dd473e4be72ea
pg (1.6.3-aarch64-linux-musl) sha256=06a75f4ea04b05140146f2a10550b8e0d9f006a79cdaf8b5b130cde40e3ecc2c
@@ -1059,7 +1060,7 @@ CHECKSUMS
rspec-core (3.13.6) sha256=a8823c6411667b60a8bca135364351dda34cd55e44ff94c4be4633b37d828b2d
rspec-expectations (3.13.5) sha256=33a4d3a1d95060aea4c94e9f237030a8f9eae5615e9bd85718fe3a09e4b58836
rspec-mocks (3.13.7) sha256=0979034e64b1d7a838aaaddf12bf065ea4dc40ef3d4c39f01f93ae2c66c62b1c
- rspec-rails (8.0.2) sha256=113139a53f5d068d4f48d1c29ad5f982013ed9b0daa69d7f7b266eda5d433ace
+ rspec-rails (8.0.3) sha256=b0a440e7a10700317d898a014852e26660867298c4076dbc3baa99c768b79dc1
rspec-retry (0.6.2) sha256=6101ba23a38809811ae3484acde4ab481c54d846ac66d5037ccb40131a60d858
rspec-support (3.13.7) sha256=0640e5570872aafefd79867901deeeeb40b0c9875a36b983d85f54fb7381c47c
rspec_junit_formatter (0.6.0) sha256=40dde674e6ae4e6cc0ff560da25497677e34fefd2338cc467a8972f602b62b15
diff --git a/backend/README.md b/backend/README.md
index d0ab59c491..10ca7fd612 100644
--- a/backend/README.md
+++ b/backend/README.md
@@ -1,6 +1,6 @@
-# Spree Server
+# Spree Backend
-A Rails application that serves as the Spree Commerce backend. It mounts the Spree engines (core, api, admin, emails, sample) from the `engines/` directory and provides the API and admin interface.
+A Rails application that serves as the Spree Commerce backend. It mounts the Spree engines (core, api, admin, emails) from the `engines/` directory and provides the API and admin interface.
## Structure
@@ -12,10 +12,10 @@ backend/
├── db/
└── engines/
├── core/ # spree_core - models, services, business logic
- ├── api/ # spree_api - Storefront & Platform REST APIs
+ ├── api/ # spree_api - REST APIs and Webhooks
├── admin/ # spree_admin - admin dashboard
├── emails/ # spree_emails - transactional emails
- └── sample/ # spree_sample - sample seed data
+ └── multi_store/ # spree_multi_store - multi-store setup, the only one licensed under AGPLv3, obtain a commercial license for production use at https://spreecommerce.org/enterprise/, not included by default
```
## Setup
@@ -108,4 +108,4 @@ bundle exec rake test_app # generates a dummy Rails app for testing
bundle exec rspec spec
```
-Replace `core` with `api`, `admin`, `emails`, or `sample` to test other engines.
+Replace `core` with `api`, `admin`, `emails`, or `multi_store` to test other engines.
diff --git a/backend/engines/admin/app/controllers/spree/admin/dashboard_controller.rb b/backend/engines/admin/app/controllers/spree/admin/dashboard_controller.rb
index 4621225697..7f5be9a1bd 100644
--- a/backend/engines/admin/app/controllers/spree/admin/dashboard_controller.rb
+++ b/backend/engines/admin/app/controllers/spree/admin/dashboard_controller.rb
@@ -146,7 +146,8 @@ module Spree
end
@top_landing_pages = @visits_scope.where.not(landing_page: [nil, '']).top(:landing_page, 10)
- @top_referrers = @visits_scope.where.not(referring_domain: current_store.custom_domains.pluck(:url) << current_store.url).top(
+ excluded_domains = current_store.respond_to?(:custom_domains) ? current_store.custom_domains.pluck(:url) : []
+ @top_referrers = @visits_scope.where.not(referring_domain: excluded_domains << current_store.url).top(
:referring_domain, 10
)
@top_locations = @visits_scope.top(:country, 10)
diff --git a/backend/engines/admin/app/controllers/spree/admin/stores_controller.rb b/backend/engines/admin/app/controllers/spree/admin/stores_controller.rb
index 6d2994829c..1a9815f21b 100644
--- a/backend/engines/admin/app/controllers/spree/admin/stores_controller.rb
+++ b/backend/engines/admin/app/controllers/spree/admin/stores_controller.rb
@@ -9,31 +9,6 @@ module Spree
before_action :load_all_countries, only: [:edit, :update]
before_action :load_all_zones, only: [:edit, :update]
- def new
- @store = Spree::Store.new(
- default_country_iso: current_store.default_country_iso,
- default_currency: current_store.default_currency
- )
- render :new, layout: 'spree/admin_wizard'
- end
-
- def create
- @store = Spree::Store.new(permitted_store_params)
- @store.mail_from_address = current_store.mail_from_address
-
- if @store.save
- # Move/copy all existing users (staff) to the new store
- current_store.role_users.each do |role_user|
- @store.add_user(role_user.user, role_user.role)
- end
-
- flash[:success] = flash_message_for(@store, :successfully_created)
- redirect_to spree.admin_getting_started_url(host: @store.url), allow_other_host: true
- else
- render :new, status: :unprocessable_content
- end
- end
-
def edit
if params[:section] == 'emails'
add_breadcrumb Spree.t(:emails), spree.edit_admin_store_path(section: params[:section])
@@ -61,7 +36,7 @@ module Spree
flash[:error] = "#{Spree.t('store_errors.unable_to_update')}: #{@store.errors.full_messages.join(', ')}"
end
- if @store.saved_changes? && permitted_store_params[:code].present?
+ if @store.saved_changes? && permitted_store_params[:code].present? && spree.respond_to?(:admin_custom_domains_url)
redirect_to spree.admin_custom_domains_url(host: @store.url), allow_other_host: true
else
respond_to do |format|
diff --git a/backend/engines/admin/app/helpers/spree/admin/stores_helper.rb b/backend/engines/admin/app/helpers/spree/admin/stores_helper.rb
index e6c3ea773a..f5490c436b 100644
--- a/backend/engines/admin/app/helpers/spree/admin/stores_helper.rb
+++ b/backend/engines/admin/app/helpers/spree/admin/stores_helper.rb
@@ -3,10 +3,6 @@ module Spree
module StoresHelper
include Spree::ImagesHelper
- def available_stores
- @available_stores ||= Spree::Store.accessible_by(current_ability, :manage).includes(:logo_attachment, :favicon_image_attachment, :default_custom_domain)
- end
-
DEFAULT_ICON_SIZE = 40
def store_admin_icon(store, opts = {})
diff --git a/backend/engines/admin/app/views/spree/admin/payment_methods/_form.html.erb b/backend/engines/admin/app/views/spree/admin/payment_methods/_form.html.erb
index a780bd3593..266eb39b85 100644
--- a/backend/engines/admin/app/views/spree/admin/payment_methods/_form.html.erb
+++ b/backend/engines/admin/app/views/spree/admin/payment_methods/_form.html.erb
@@ -24,18 +24,6 @@
<div class="card-body">
<%= f.spree_text_field :name, help: 'This name will be used to identify the payment method on the storefront' %>
- <% if can?(:manage, Spree::Store) && available_stores.count > 1 %>
- <div class="form-group">
- <%= label_tag :payment_method_stores, Spree.t(:stores) %>
- <%= f.collection_check_boxes :store_ids, available_stores, :id, :name do |b| %>
- <div class="custom-control form-checkbox mb-1">
- <%= b.check_box(class: 'custom-control-input') %>
- <%= b.label(class: 'custom-control-label') %>
- </div>
- <% end %>
- </div>
- <% end %>
-
<%= f.spree_select :display_on, display_on_options, { label: Spree.t(:display) } %>
<%= f.spree_select :auto_capture, [["#{Spree.t(:use_app_default)} (#{Spree::Config[:auto_capture]})", ''], [Spree.t(:say_yes).to_s, true], [Spree.t(:say_no).to_s, false]], { label: Spree.t(:auto_capture) } %>
diff --git a/backend/engines/admin/app/views/spree/admin/products/_form.html.erb b/backend/engines/admin/app/views/spree/admin/products/_form.html.erb
index 2320919026..85f1fed380 100644
--- a/backend/engines/admin/app/views/spree/admin/products/_form.html.erb
+++ b/backend/engines/admin/app/views/spree/admin/products/_form.html.erb
@@ -16,7 +16,6 @@
<%= render 'spree/admin/products/form/categorization', f: f %>
<%= render 'spree/admin/products/form/shipping', f: f %>
<%= render 'spree/admin/products/form/tax', f: f %>
- <%= render 'spree/admin/products/form/stores', f: f if available_stores.count > 1 %>
<%= render_admin_partials(:product_form_sidebar_partials, f: f, product: @product) %>
<%= render 'spree/admin/shared/seo',
diff --git a/backend/engines/admin/app/views/spree/admin/products/form/_stores.html.erb b/backend/engines/admin/app/views/spree/admin/products/form/_stores.html.erb
deleted file mode 100644
index 68daf2b0cc..0000000000
--- a/backend/engines/admin/app/views/spree/admin/products/form/_stores.html.erb
+++ /dev/null
@@ -1,27 +0,0 @@
-<div class="card mb-6">
- <div class="card-header">
- <h5 class="card-title">
- <%= Spree.t(:stores) %>
- </h5>
- </div>
- <div class="card-body">
- <div class="form-group">
- <% if can? :modify, Spree::StoreProduct %>
- <p class="form-text"><%= Spree.t('admin.products.stores.choose_stores') %>.</p>
-
- <%= f.collection_check_boxes :store_ids, available_stores, :id, :name do |b| %>
- <div class="custom-control form-checkbox mb-1">
- <%= b.check_box(class: 'custom-control-input') %>
- <%= b.label(class: 'custom-control-label') %>
- </div>
- <% end %>
- <% elsif @product.stores.any? %>
- <ul class="text_list">
- <% @product.stores.pluck(:name).each do |store_name| %>
- <li><%= store_name %></li>
- <% end %>
- </ul>
- <% end %>
- </div>
- </div>
-</div>
\ No newline at end of file
diff --git a/backend/engines/admin/app/views/spree/admin/shared/sidebar/_store_dropdown.html.erb b/backend/engines/admin/app/views/spree/admin/shared/sidebar/_store_dropdown.html.erb
index cba68ac1e8..12e52ce440 100644
--- a/backend/engines/admin/app/views/spree/admin/shared/sidebar/_store_dropdown.html.erb
+++ b/backend/engines/admin/app/views/spree/admin/shared/sidebar/_store_dropdown.html.erb
@@ -15,39 +15,6 @@
<%= icon 'tools' %>
<%= Spree.t('admin.edit_theme') %>
<% end if current_store.respond_to?(:default_theme) && current_store.default_theme && can?(:manage, Spree::Theme) %>
-
- <% if available_stores.count > 1 %>
- <div class="dropdown-divider"></div>
- <h6 class="dropdown-header">
- <%= Spree.t(:switch_store) %>
- </h6>
- <div class="store-chooser">
- <% available_stores.each do |store| %>
- <% if store.id == current_store.id %>
- <div class="store-chooser-item active">
- <%= store_admin_icon(store, height: 32, width: 32) %>
- <span class="flex flex-col mr-2">
- <%= store.name %>
- <small class="text-gray-600">
- <%= store.url_or_custom_domain %>
- </small>
- </span>
- <%= icon('check', class: 'ml-auto') %>
- </div>
- <% else %>
- <%= link_to spree.admin_dashboard_url(host: store.url), class: 'store-chooser-item' do %>
- <%= store_admin_icon(store, height: 32, width: 32) %>
- <span class="flex flex-col">
- <%= store.name %>
- <small class="text-gray-600">
- <%= store.url_or_custom_domain %>
- </small>
- </span>
- <% end %>
- <% end %>
- <% end %>
- </div>
- <% end %>
<% end %>
<% end %>
diff --git a/backend/engines/admin/app/views/spree/admin/shared/sidebar/_store_nav.html.erb b/backend/engines/admin/app/views/spree/admin/shared/sidebar/_store_nav.html.erb
index 298347ecdf..2b38918c7d 100644
--- a/backend/engines/admin/app/views/spree/admin/shared/sidebar/_store_nav.html.erb
+++ b/backend/engines/admin/app/views/spree/admin/shared/sidebar/_store_nav.html.erb
@@ -15,7 +15,7 @@
<%= nav_item(Spree.t(:checkout), spree.edit_admin_store_path(section: 'checkout'), icon: 'shopping-cart', active: params[:section] == 'checkout') if can?(:manage, current_store) %>
<%= render_admin_partials(:store_settings_nav_partials) %>
- <% if can?(:manage, Spree::CustomDomain) %>
+ <% if defined?(Spree::CustomDomain) && can?(:manage, Spree::CustomDomain) %>
<%= nav_item(Spree.t(:domains), spree.admin_custom_domains_path, icon: 'world-www') %>
<% end %>
diff --git a/backend/engines/admin/config/initializers/spree_admin_navigation.rb b/backend/engines/admin/config/initializers/spree_admin_navigation.rb
index 3c463e095d..47d7b0d513 100644
--- a/backend/engines/admin/config/initializers/spree_admin_navigation.rb
+++ b/backend/engines/admin/config/initializers/spree_admin_navigation.rb
@@ -258,15 +258,6 @@ Rails.application.config.after_initialize do
active: -> { controller_name == 'policies' },
if: -> { can?(:manage, Spree::Policy) }
- # Domains
- settings_nav.add :domains,
- label: :domains,
- url: :admin_custom_domains_path,
- icon: 'world-www',
- position: 60,
- active: -> { controller_name == 'custom_domains' },
- if: -> { can?(:manage, Spree::CustomDomain) }
-
# Payment Methods
settings_nav.add :payment_methods,
label: :payments,
diff --git a/backend/engines/admin/config/routes.rb b/backend/engines/admin/config/routes.rb
index 48b63e502c..aaec9cb7cb 100644
--- a/backend/engines/admin/config/routes.rb
+++ b/backend/engines/admin/config/routes.rb
@@ -208,9 +208,6 @@ Spree::Core::Engine.add_routes do
member do
get :edit_emails
end
- end
- # setting up a new store
- resources :stores, only: [:new, :create] do
resources :role_users, only: [:destroy]
resources :links, controller: 'page_links', only: [:create]
end
@@ -230,8 +227,6 @@ Spree::Core::Engine.add_routes do
put :mark_as_default
end
end
- resources :custom_domains, except: :show
-
# integrations
resources :integrations
diff --git a/backend/engines/admin/lib/spree/admin/engine.rb b/backend/engines/admin/lib/spree/admin/engine.rb
index 7ba8ba94c9..de747abb94 100644
--- a/backend/engines/admin/lib/spree/admin/engine.rb
+++ b/backend/engines/admin/lib/spree/admin/engine.rb
@@ -47,6 +47,7 @@ module Spree
:pages_actions_partials,
:pages_header_partials,
:payment_methods_actions_partials,
+ :payment_method_form_partials,
:payment_methods_header_partials,
:post_categories_actions_partials,
:post_categories_header_partials,
diff --git a/backend/engines/admin/spec/controllers/spree/admin/stores_controller_spec.rb b/backend/engines/admin/spec/controllers/spree/admin/stores_controller_spec.rb
index 4dd558149c..6406dbc54f 100644
--- a/backend/engines/admin/spec/controllers/spree/admin/stores_controller_spec.rb
+++ b/backend/engines/admin/spec/controllers/spree/admin/stores_controller_spec.rb
@@ -6,7 +6,6 @@ describe Spree::Admin::StoresController do
let(:store) { create(:store) }
let(:user) { create(:admin_user) }
- let(:user2) { create(:admin_user) }
let!(:uk_country) { create(:country, iso: 'GB', iso3: 'GBR', name: 'United Kingdom') }
before do
@@ -14,38 +13,10 @@ describe Spree::Admin::StoresController do
allow(controller).to receive(:try_spree_current_user).and_return(user)
store.add_user(user)
- store.add_user(user2)
allow(Spree).to receive(:root_domain).and_return('lvh.me')
end
- describe 'POST #create' do
- subject { post :create, params: { store: store_params }, format: :turbo_stream }
-
- let(:store_params) do
- {
- name: 'New UK Store',
- default_currency: 'GBR',
- default_country_iso: 'GB',
- default_locale: 'en'
- }
- end
-
- it 'creates a new store' do
- expect(store.users).to contain_exactly(user, user2)
- expect { subject }.to change(Spree::Store, :count).by(1).and change(Spree::RoleUser, :count).by(2)
-
- expect(flash[:success]).to match('has been successfully created')
-
- new_store = Spree::Store.last
- expect(new_store.name).to eq('New UK Store')
- expect(new_store.default_currency).to eq('GBR')
- expect(new_store.default_country).to eq(uk_country)
- expect(new_store.default_locale).to eq('en')
- expect(new_store.users).to contain_exactly(user, user2)
- end
- end
-
describe 'GET #edit' do
subject { get :edit }
diff --git a/backend/engines/admin/spec/features/spree/admin/settings/store_spec.rb b/backend/engines/admin/spec/features/spree/admin/settings/store_spec.rb
index 03313fc674..24c2411965 100644
--- a/backend/engines/admin/spec/features/spree/admin/settings/store_spec.rb
+++ b/backend/engines/admin/spec/features/spree/admin/settings/store_spec.rb
@@ -16,24 +16,6 @@ describe 'Store admin', type: :feature, js: true do
Rails.application.config.hosts << /.*\.lvh\.me/
end
- describe 'creating another store' do
- it 'creates another store' do
- visit spree.admin_root_path
- find('#store-menu .dropdown:last-child button').click
- click_on 'New Store'
-
- expect(page).to have_content('New Store')
- fill_in 'store_name', with: 'New Store Name'
- click_button 'Create'
-
- expect(page).to have_content('Getting started')
-
- new_store = Spree::Store.last
-
- expect(page).to have_current_path(spree.admin_getting_started_url(host: new_store.url, port: Capybara.current_session.server.port))
- end
- end
-
describe 'updating store' do
it 'updates store' do
visit spree.edit_admin_store_path(section: 'general-settings')
diff --git a/backend/engines/api/app/controllers/spree/api/v3/store/stores_controller.rb b/backend/engines/api/app/controllers/spree/api/v3/store/stores_controller.rb
index 9466df41d0..c900d77760 100644
--- a/backend/engines/api/app/controllers/spree/api/v3/store/stores_controller.rb
+++ b/backend/engines/api/app/controllers/spree/api/v3/store/stores_controller.rb
@@ -3,18 +3,11 @@ module Spree
module V3
module Store
class StoresController < ResourceController
- # Public endpoint - no authentication required
+ skip_before_action :set_resource
- # GET /api/v3/store/store
- def current
- @store = current_store
- render json: serialize_resource(@store)
- end
-
- # GET /api/v3/store/stores/:code
+ # GET /api/v3/store/store
def show
- @resource = Spree::Store.find_by!(code: params[:id])
- render json: serialize_resource(@resource)
+ render json: serialize_resource(current_store)
end
protected
diff --git a/backend/engines/api/config/routes.rb b/backend/engines/api/config/routes.rb
index b6195acd8d..ad6fb79079 100644
--- a/backend/engines/api/config/routes.rb
+++ b/backend/engines/api/config/routes.rb
@@ -9,7 +9,7 @@ Spree::Core::Engine.add_routes do
post 'auth/oauth/callback', to: 'auth#oauth_callback'
# Store
- get 'store', to: 'stores#current'
+ resource :store, only: [:show]
# Geography - countries include nested states from checkout zone
resources :countries, only: [:index, :show]
diff --git a/backend/engines/api/spec/controllers/spree/api/v3/store/stores_controller_spec.rb b/backend/engines/api/spec/controllers/spree/api/v3/store/stores_controller_spec.rb
index d0f1174e61..73369f64e6 100644
--- a/backend/engines/api/spec/controllers/spree/api/v3/store/stores_controller_spec.rb
+++ b/backend/engines/api/spec/controllers/spree/api/v3/store/stores_controller_spec.rb
@@ -9,22 +9,22 @@ RSpec.describe Spree::Api::V3::Store::StoresController, type: :controller do
request.headers['X-Spree-Api-Key'] = api_key.token
end
- describe 'GET #current' do
+ describe 'GET #show' do
it 'returns the current store' do
- get :current
+ get :show
expect(response).to have_http_status(:ok)
expect(json_response['id']).to eq(store.prefixed_id)
end
it 'returns store attributes' do
- get :current
+ get :show
expect(json_response).to include('id', 'name', 'url', 'default_currency')
end
it 'returns store settings' do
- get :current
+ get :show
expect(json_response['default_currency']).to eq(store.default_currency)
end
@@ -33,7 +33,7 @@ RSpec.describe Spree::Api::V3::Store::StoresController, type: :controller do
before { request.headers['X-Spree-Api-Key'] = nil }
it 'returns unauthorized' do
- get :current
+ get :show
expect(response).to have_http_status(:unauthorized)
expect(json_response['error']['code']).to eq('invalid_token')
@@ -45,7 +45,7 @@ RSpec.describe Spree::Api::V3::Store::StoresController, type: :controller do
before { request.headers['X-Spree-Api-Key'] = 'invalid' }
it 'returns unauthorized' do
- get :current
+ get :show
expect(response).to have_http_status(:unauthorized)
expect(json_response['error']['code']).to eq('invalid_token')
@@ -59,7 +59,7 @@ RSpec.describe Spree::Api::V3::Store::StoresController, type: :controller do
before { request.headers['X-Spree-Api-Key'] = other_api_key.token }
it 'returns unauthorized as API keys are scoped to their store' do
- get :current
+ get :show
expect(response).to have_http_status(:unauthorized)
expect(json_response['error']['code']).to eq('invalid_token')
diff --git a/backend/engines/core/app/finders/spree/stores/find_default.rb b/backend/engines/core/app/finders/spree/stores/find_default.rb
new file mode 100644
index 0000000000..c386df5c16
--- /dev/null
+++ b/backend/engines/core/app/finders/spree/stores/find_default.rb
@@ -0,0 +1,17 @@
+module Spree
+ module Stores
+ class FindDefault
+ def initialize(scope: nil, url: nil)
+ @scope = scope || Spree::Store
+ end
+
+ def execute
+ store = @scope.where(default: true).first || @scope.first
+ return if store.nil?
+
+ Spree::Current.store = store
+ store
+ end
+ end
+ end
+end
diff --git a/backend/engines/core/app/helpers/spree/base_helper.rb b/backend/engines/core/app/helpers/spree/base_helper.rb
index dc198d5d98..9834c7c919 100644
--- a/backend/engines/core/app/helpers/spree/base_helper.rb
+++ b/backend/engines/core/app/helpers/spree/base_helper.rb
@@ -134,10 +134,10 @@ module Spree
base_url = if options[:relative]
''
- elsif store.formatted_custom_domain.blank?
- store.formatted_url
- else
+ elsif store.respond_to?(:formatted_custom_domain) && store.formatted_custom_domain.present?
store.formatted_custom_domain
+ else
+ store.formatted_url
end
localize = if options[:locale].present?
diff --git a/backend/engines/core/app/models/concerns/spree/store_scoped_resource.rb b/backend/engines/core/app/models/concerns/spree/store_scoped_resource.rb
new file mode 100644
index 0000000000..5b9b59d68a
--- /dev/null
+++ b/backend/engines/core/app/models/concerns/spree/store_scoped_resource.rb
@@ -0,0 +1,24 @@
+module Spree
+ module StoreScopedResource
+ extend ActiveSupport::Concern
+
+ included do
+ scope :for_store, ->(store) { joins(:stores).where(Store.table_name => { id: store.id }) }
+
+ before_validation :set_default_store, if: :new_record?
+ end
+
+ protected
+
+ def set_default_store
+ return if disable_store_presence_validation?
+ return if stores.any?
+
+ stores << Spree::Store.default
+ end
+
+ def disable_store_presence_validation?
+ Spree::Config[:disable_store_presence_validation]
+ end
+ end
+end
diff --git a/backend/engines/core/app/models/spree/payment_method.rb b/backend/engines/core/app/models/spree/payment_method.rb
index 860f659681..fba99e756b 100644
--- a/backend/engines/core/app/models/spree/payment_method.rb
+++ b/backend/engines/core/app/models/spree/payment_method.rb
@@ -5,7 +5,7 @@ module Spree
acts_as_paranoid
acts_as_list
- include Spree::MultiStoreResource
+ include Spree::StoreScopedResource
include Spree::Metafields
include Spree::Metadata
include Spree::DisplayOn
diff --git a/backend/engines/core/app/models/spree/product.rb b/backend/engines/core/app/models/spree/product.rb
index 14b4f93035..1f5a249f32 100644
--- a/backend/engines/core/app/models/spree/product.rb
+++ b/backend/engines/core/app/models/spree/product.rb
@@ -27,7 +27,7 @@ module Spree
normalizes :name, with: ->(value) { value&.to_s&.squish&.presence }
include Spree::ProductScopes
- include Spree::MultiStoreResource
+ include Spree::StoreScopedResource
include Spree::TranslatableResource
include Spree::MemoizedData
include Spree::Metafields
diff --git a/backend/engines/core/app/models/spree/promotion.rb b/backend/engines/core/app/models/spree/promotion.rb
index 06b34d49e8..5bb10fa841 100644
--- a/backend/engines/core/app/models/spree/promotion.rb
+++ b/backend/engines/core/app/models/spree/promotion.rb
@@ -2,7 +2,7 @@ module Spree
class Promotion < Spree.base_class
has_prefix_id :promo # Spree-specific: promotion
- include Spree::MultiStoreResource
+ include Spree::StoreScopedResource
include Spree::Metafields
include Spree::Metadata
if defined?(Spree::Security::Promotions)
diff --git a/backend/engines/core/app/models/spree/store.rb b/backend/engines/core/app/models/spree/store.rb
index 4503627c65..92f647b7df 100644
--- a/backend/engines/core/app/models/spree/store.rb
+++ b/backend/engines/core/app/models/spree/store.rb
@@ -4,10 +4,6 @@ module Spree
class Store < Spree.base_class
has_prefix_id :store # Spree-specific: store
- RESERVED_CODES = %w(
- admin default app api www cdn files assets checkout account auth login user
- )
-
include FriendlyId
include Spree::TranslatableResource
include Spree::Metafields
@@ -21,7 +17,7 @@ module Spree
# Magic methods
#
acts_as_paranoid
- friendly_id :slug_candidates, use: [:slugged, :history], slug_column: :code, routes: :normal
+ friendly_id :code, use: [:slugged], slug_column: :code, routes: :normal
#
# Translations
@@ -103,9 +99,6 @@ module Spree
has_many :reports, class_name: 'Spree::Report'
has_many :exports, class_name: 'Spree::Export'
- has_many :custom_domains, class_name: 'Spree::CustomDomain', dependent: :destroy
- has_one :default_custom_domain, -> { where(default: true) }, class_name: 'Spree::CustomDomain'
-
has_many :posts, class_name: 'Spree::Post', dependent: :destroy, inverse_of: :store
has_many :post_categories, class_name: 'Spree::PostCategory', dependent: :destroy, inverse_of: :store
@@ -134,9 +127,6 @@ module Spree
#
# Virtual attributes
#
- attribute :import_products_from_store_id, :string, default: nil
- attribute :import_payment_methods_from_store_id, :string, default: nil
- attr_accessor :skip_validate_not_last
store_accessor :private_metadata, :storefront_password
#
@@ -147,7 +137,6 @@ module Spree
end
validates :preferred_digital_asset_authorized_clicks, numericality: { only_integer: true, greater_than: 0 }
validates :preferred_digital_asset_authorized_days, numericality: { only_integer: true, greater_than: 0 }
- validates :code, uniqueness: { case_sensitive: false, conditions: -> { with_deleted } }, exclusion: RESERVED_CODES
validates :mail_from_address, email: { allow_blank: false }
# FIXME: we should remove this condition in v5
if !ENV['SPREE_DISABLE_DB_CONNECTION'] &&
@@ -169,39 +158,27 @@ module Spree
#
# Callbacks
before_validation :ensure_default_country
- before_validation :set_code, on: :create
- before_validation :set_url
+ before_validation :set_default_code, on: :create
before_save :ensure_default_exists_and_is_unique
before_save :ensure_supported_currencies, :ensure_supported_locales
after_create :ensure_default_taxonomies_are_created
after_create :ensure_default_automatic_taxons
after_create :ensure_default_post_categories_are_created
- after_create :import_products_from_store, if: -> { import_products_from_store_id.present? }
- after_create :import_payment_methods_from_store, if: -> { import_payment_methods_from_store_id.present? }
after_create :create_default_policies
- before_destroy :validate_not_last, unless: :skip_validate_not_last
- before_destroy :pass_default_flag_to_other_store
after_commit :clear_cache
- after_commit :handle_code_changes, on: :update, if: -> { code_previously_changed? }
#
# Scopes
#
default_scope { order(created_at: :asc) }
- scope :by_custom_domain, ->(url) { left_joins(:custom_domains).where("#{Spree::CustomDomain.table_name}.url" => url) }
- scope :by_url, ->(url) { where(url: url).or(where("#{table_name}.url like ?", "%#{url}%")) }
#
# Delegations
#
delegate :iso, to: :default_country, prefix: true, allow_nil: true
- def self.current(url = nil)
- if url.present?
- Spree.current_store_finder.new(url: url).execute
- else
- Spree::Current.store
- end
+ def self.current(_url = nil)
+ Spree::Current.store
end
# @deprecated The or_initialize behavior will be removed in Spree 5.5.
@@ -221,7 +198,7 @@ module Spree
end
def self.available_locales
- Spree::Store.all.map(&:supported_locales_list).flatten.uniq
+ Spree::Store.default&.supported_locales_list || []
end
def default_country_iso=(iso)
@@ -299,26 +276,12 @@ module Spree
end
end
- def formatted_custom_domain
- return unless default_custom_domain
-
- @formatted_custom_domain ||= if Rails.env.development? || Rails.env.test?
- URI::Generic.build(
- scheme: Rails.application.routes.default_url_options[:protocol] || 'http',
- host: default_custom_domain.url,
- port: Rails.application.routes.default_url_options[:port]
- ).to_s
- else
- URI::HTTPS.build(host: default_custom_domain.url).to_s
- end
- end
-
def url_or_custom_domain
- default_custom_domain&.url || url
+ url
end
def formatted_url_or_custom_domain
- formatted_custom_domain || formatted_url
+ formatted_url
end
# Returns the countries available for checkout for the store or creates a new one if it doesn't exist
@@ -369,10 +332,6 @@ module Spree
favicon_image.variant(resize_to_limit: [32, 32])
end
- def can_be_deleted?
- self.class.where.not(id: id).any?
- end
-
def metric_unit_system?
preferred_unit_system == 'metric'
end
@@ -385,24 +344,6 @@ module Spree
@digital_shipping_category ||= ShippingCategory.find_or_create_by(name: 'Digital')
end
- def import_products_from_store
- store = Store.find(import_products_from_store_id)
- product_ids = store.products.pluck(:id)
-
- return if product_ids.empty?
-
- StoreProduct.insert_all(product_ids.map { |product_id| { store_id: id, product_id: product_id } })
- end
-
- def import_payment_methods_from_store
- store = Store.find(import_payment_methods_from_store_id)
- payment_method_ids = store.payment_method_ids
-
- return if payment_method_ids.empty?
-
- StorePaymentMethod.insert_all(payment_method_ids.map { |payment_method_id| { store_id: id, payment_method_id: payment_method_id } })
- end
-
%w[customer_terms_of_service customer_privacy_policy customer_returns_policy customer_shipping_policy].each do |policy_method|
define_method policy_method do
Spree::Deprecation.warn("Store##{policy_method} is deprecated and will be removed in Spree 5.5. Please use Store#policies instead.")
@@ -421,14 +362,6 @@ module Spree
"#{cache_key_with_version}/#{checkout_zone&.cache_key_with_version}/states_available_for_checkout/#{country&.cache_key_with_version}"
end
- def ensure_default_exists_and_is_unique
- if default
- Store.where.not(id: id).update_all(default: false)
- elsif Store.where(default: true).count.zero?
- self.default = true
- end
- end
-
def ensure_supported_locales
return unless attributes.keys.include?('supported_locales')
return if supported_locales.present?
@@ -445,20 +378,6 @@ module Spree
self.supported_currencies = default_currency
end
- def validate_not_last
- unless can_be_deleted?
- errors.add(:base, :cannot_destroy_only_store)
- throw(:abort)
- end
- end
-
- def pass_default_flag_to_other_store
- if default? && can_be_deleted?
- self.class.where.not(id: id).first.update!(default: true)
- self.default = false
- end
- end
-
def clear_cache
Rails.cache.delete('default_store')
end
@@ -554,42 +473,20 @@ module Spree
I18n.t(key, locale: locale, default: I18n.t(key, locale: :en))
end
- # code is slug, so we don't want to generate new slug when code changes
- # we use friendlyId only for history feature
- def should_generate_new_friendly_id?
- false
- end
-
- def slug_candidates
- []
- end
-
- def handle_code_changes
- # implement your custom logic here
+ def ensure_default_exists_and_is_unique
+ if default
+ Spree::Store.where.not(id: id).update_all(default: false)
+ elsif Spree::Store.where(default: true).count.zero?
+ self.default = true
+ end
end
- # This FriendlyId method is overwitten to keep our logic for generating code
- # there is no option for own format
- def set_code
- self.code = if code.present?
- code.parameterize.strip
- elsif name.present?
- name.parameterize.strip
- end
-
- return if self.code.blank?
-
- # ensure code is unique
- self.code = [name.parameterize, rand(9999)].join('-') while Spree::Store.with_deleted.where(code: self.code).exists?
+ def should_generate_new_friendly_id?
+ false
end
- # auto-assign internal URL for stores
- def set_url
- return if url_changed?
- return unless code_changed?
- return unless Spree.root_domain.present?
-
- self.url = [code, Spree.root_domain].join('.')
+ def set_default_code
+ self.code = 'default' if code.blank?
end
end
end
diff --git a/backend/engines/core/app/services/spree/seeds/admin_user.rb b/backend/engines/core/app/services/spree/seeds/admin_user.rb
index 71d39c6987..bdba826e3b 100644
--- a/backend/engines/core/app/services/spree/seeds/admin_user.rb
+++ b/backend/engines/core/app/services/spree/seeds/admin_user.rb
@@ -14,9 +14,8 @@ module Spree
)
user.save!
- Spree::Store.all.each do |store|
- store.add_user(user)
- end
+ store = Spree::Store.default
+ store&.add_user(user) if store&.persisted?
end
end
end
diff --git a/backend/engines/core/lib/spree/core/dependencies.rb b/backend/engines/core/lib/spree/core/dependencies.rb
index feb92e4149..1a41985428 100644
--- a/backend/engines/core/lib/spree/core/dependencies.rb
+++ b/backend/engines/core/lib/spree/core/dependencies.rb
@@ -100,7 +100,7 @@ module Spree
cms_page_finder: nil, # LEGACY
menu_finder: nil, # LEGACY
current_order_finder: 'Spree::Orders::FindCurrent',
- current_store_finder: 'Spree::Stores::FindCurrent',
+ current_store_finder: 'Spree::Stores::FindDefault',
completed_order_finder: 'Spree::Orders::FindComplete',
credit_card_finder: 'Spree::CreditCards::Find',
products_finder: 'Spree::Products::Find',
diff --git a/backend/engines/core/lib/spree/core/engine.rb b/backend/engines/core/lib/spree/core/engine.rb
index 0d8432aea6..7f5b4b5d9a 100644
--- a/backend/engines/core/lib/spree/core/engine.rb
+++ b/backend/engines/core/lib/spree/core/engine.rb
@@ -231,7 +231,6 @@ module Spree
Spree::Address,
Spree::Asset,
Spree::CreditCard,
- Spree::CustomDomain,
Spree::CustomerReturn,
Spree::GiftCard,
Spree::Image,
diff --git a/backend/engines/core/lib/spree/permitted_attributes.rb b/backend/engines/core/lib/spree/permitted_attributes.rb
index 7d93621516..e98200cb01 100644
--- a/backend/engines/core/lib/spree/permitted_attributes.rb
+++ b/backend/engines/core/lib/spree/permitted_attributes.rb
@@ -277,7 +277,6 @@ module Spree
:preferred_limit_digital_download_count, :preferred_limit_digital_download_days,
:preferred_digital_asset_link_expire_time,
:logo, :mailer_logo, :social_logo, :favicon_image,
- :import_products_from_store_id, :import_payment_methods_from_store_id,
:checkout_message, :preferred_guest_checkout,
:customer_terms_of_service, :customer_privacy_policy,
:customer_returns_policy, :customer_shipping_policy, :default_country_iso]
diff --git a/backend/engines/core/lib/spree/testing_support/factories/custom_domain_factory.rb b/backend/engines/core/lib/spree/testing_support/factories/custom_domain_factory.rb
index c42b52c845..c8c5860ded 100644
--- a/backend/engines/core/lib/spree/testing_support/factories/custom_domain_factory.rb
+++ b/backend/engines/core/lib/spree/testing_support/factories/custom_domain_factory.rb
@@ -1,7 +1,9 @@
-FactoryBot.define do
- factory :custom_domain, class: Spree::CustomDomain do
- url { FFaker::Internet.domain_name }
- association :store, factory: :store
- default { true }
+if defined?(Spree::CustomDomain)
+ FactoryBot.define do
+ factory :custom_domain, class: Spree::CustomDomain do
+ url { FFaker::Internet.domain_name }
+ association :store, factory: :store
+ default { true }
+ end
end
end
diff --git a/backend/engines/core/spec/finders/spree/stores/find_default_spec.rb b/backend/engines/core/spec/finders/spree/stores/find_default_spec.rb
new file mode 100644
index 0000000000..c26758824c
--- /dev/null
+++ b/backend/engines/core/spec/finders/spree/stores/find_default_spec.rb
@@ -0,0 +1,48 @@
+require 'spec_helper'
+
+module Spree
+ describe Stores::FindDefault do
+ subject { described_class.new(scope: scope, url: url).execute }
+
+ let!(:store) { @default_store }
+ let!(:store_2) { create(:store, url: 'another.com', default_currency: 'GBP') }
+
+ let(:scope) { nil }
+ let(:url) { nil }
+
+ before do
+ Spree::Current.store = nil
+ end
+
+ context 'no arguments' do
+ it { expect(subject).to eq(store) }
+ it { subject; expect(Spree::Current.store).to eq(store) }
+ end
+
+ context 'with url argument (ignored)' do
+ let(:url) { 'another.com' }
+
+ it 'returns the default store regardless of url' do
+ expect(subject).to eq(store)
+ end
+ end
+
+ context 'with scope' do
+ let(:scope) { Spree::Store.where(default_currency: 'GBP') }
+
+ it 'returns the first store in scope' do
+ expect(subject).to eq(store_2)
+ end
+ end
+
+ context 'when no default store exists' do
+ before do
+ Spree::Store.update_all(default: false)
+ end
+
+ it 'returns the first store' do
+ expect(subject).to eq(store)
+ end
+ end
+ end
+end
diff --git a/backend/engines/core/spec/helpers/base_helper_spec.rb b/backend/engines/core/spec/helpers/base_helper_spec.rb
index 5cd6bfe8c9..e1bbcfde46 100644
--- a/backend/engines/core/spec/helpers/base_helper_spec.rb
+++ b/backend/engines/core/spec/helpers/base_helper_spec.rb
@@ -83,14 +83,6 @@ describe Spree::BaseHelper, type: :helper do
end
end
- context 'for product with custom domain' do
- let!(:custom_domain) { create :custom_domain, store: current_store }
-
- it 'returns the product url' do
- expect(spree_storefront_resource_url(product)).to eq("http://#{custom_domain.url}/products/#{product.slug}")
- end
- end
-
context 'for product with relative option' do
it 'returns the product url' do
expect(spree_storefront_resource_url(product, relative: true)).to eq("/products/#{product.slug}")
diff --git a/backend/engines/core/spec/helpers/locale_helper_spec.rb b/backend/engines/core/spec/helpers/locale_helper_spec.rb
index e30f7ad55f..2d2a44bf6c 100644
--- a/backend/engines/core/spec/helpers/locale_helper_spec.rb
+++ b/backend/engines/core/spec/helpers/locale_helper_spec.rb
@@ -27,8 +27,9 @@ describe Spree::LocaleHelper, type: :helper do
describe '#available_locales_options' do
before do
- create(:store, supported_locales: 'en,de')
- create(:store, supported_locales: 'en')
+ store = create(:store, default: true, supported_locales: 'en,de', default_locale: 'en')
+ Spree::Store.where.not(id: store.id).update_all(default: false)
+ Rails.cache.clear
end
it { expect(available_locales_options).to contain_exactly(['English (US)', 'en'], ['Deutsch (DE)', 'de']) }
diff --git a/backend/engines/core/spec/lib/spree/core/controller_helpers/order_spec.rb b/backend/engines/core/spec/lib/spree/core/controller_helpers/order_spec.rb
index f43e2fff56..ab63e9f28f 100644
--- a/backend/engines/core/spec/lib/spree/core/controller_helpers/order_spec.rb
+++ b/backend/engines/core/spec/lib/spree/core/controller_helpers/order_spec.rb
@@ -92,16 +92,6 @@ describe Spree::Core::ControllerHelpers::Order, type: :controller do
expect(token_cookie_domain).to eq(store.url)
end
- context 'on a custom domain' do
- let!(:custom_domain) { create(:custom_domain, store: store, url: 'test-cookie-store.com') }
-
- it 'creates a new token cookie on a custom domain' do
- controller.current_order
-
- expect(token_cookie).to be_present
- expect(token_cookie_domain).to eq('test-cookie-store.com')
- end
- end
end
context 'for a cart without token' do
@@ -131,16 +121,6 @@ describe Spree::Core::ControllerHelpers::Order, type: :controller do
expect(token_cookie_domain).to eq(store.url)
end
- context 'on a custom domain' do
- let!(:custom_domain) { create(:custom_domain, store: store, url: 'test-cookie-store.com') }
-
- it 'creates a new token cookie on a custom domain' do
- controller.current_order
-
- expect(token_cookie).to be_present
- expect(token_cookie_domain).to eq('test-cookie-store.com')
- end
- end
end
context 'for a checkout without token' do
diff --git a/backend/engines/core/spec/lib/spree/core/controller_helpers/store_spec.rb b/backend/engines/core/spec/lib/spree/core/controller_helpers/store_spec.rb
index 20b23bbb42..973d51d8ee 100644
--- a/backend/engines/core/spec/lib/spree/core/controller_helpers/store_spec.rb
+++ b/backend/engines/core/spec/lib/spree/core/controller_helpers/store_spec.rb
@@ -21,28 +21,6 @@ describe Spree::Core::ControllerHelpers::Store, type: :controller do
expect(controller.current_store).to eq store
end
end
-
- context 'by domain' do
- before do
- controller.request.env['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['SERVER_NAME'] = 'some.another.com'
- end
-
- it 'returns current store' do
- expect(controller.current_store).to eq store_3
- end
- end
end
describe '#ensure_current_store' do
diff --git a/backend/engines/core/spec/models/spree/store_spec.rb b/backend/engines/core/spec/models/spree/store_spec.rb
index 02031d260b..d6d799796d 100644
--- a/backend/engines/core/spec/models/spree/store_spec.rb
+++ b/backend/engines/core/spec/models/spree/store_spec.rb
@@ -5,10 +5,6 @@ describe Spree::Store, type: :model, without_global_store: true do
Spree::Country.find_by(iso: 'US') || create(:country_us)
end
- before do
- allow(Spree).to receive(:root_domain).and_return('mydomain.dev')
- end
-
context 'Associations' do
subject { create(:store) }
@@ -120,40 +116,21 @@ describe Spree::Store, type: :model, without_global_store: true do
end
context 'Callbacks' do
- describe '#set_code' do
+ describe '#set_default_code' do
let(:store) { build(:store, name: 'Store', code: nil) }
- it 'sets the code' do
- expect { store.valid? }.to change(store, :code).from(nil).to('store')
+ it 'sets the code to default when blank' do
+ expect { store.valid? }.to change(store, :code).from(nil).to('default')
end
context 'when code is already set' do
- let(:store) { build(:store, name: 'Store', code: 'store') }
+ let(:store) { build(:store, name: 'Store', code: 'mycode') }
it 'does not change the code' do
expect { store.valid? }.not_to change(store, :code)
end
end
- context 'when name is not set' do
- let(:store) { build(:store, name: nil, code: nil) }
-
- it 'does not set the code' do
- expect { store.valid? }.not_to change(store, :code)
- end
- end
-
- context 'when code is already taken' do
- let(:default_store) { create(:store, default: true, code: 'store') }
- let(:store) { build(:store, name: 'Store', code: default_store.code) }
-
- it 'generates a new code' do
- expect { store.valid? }.to change(store, :code)
- expect(store.code).not_to eq(default_store.code)
- expect(store.code).to match(/store-\d+/)
- end
- end
-
describe '#create_default_policies' do
let(:store) { build(:store) }
@@ -205,28 +182,6 @@ describe Spree::Store, type: :model, without_global_store: true do
end
end
- describe '#set_url' do
- let(:store) { build(:store, code: 'my_store', url: nil) }
-
- context 'on create' do
- it 'sets url' do
- store.save!
- expect(store.url).to eq('my_store.mydomain.dev')
- end
- end
-
- context 'on update code change update url' do
- let!(:store) { create(:store, code: 'my_store', url: 'my_store.mydomain.dev') }
-
- it 'updates url but keep old one' do
- expect(store.url).to eq('my_store.mydomain.dev')
- store.update!(code: 'my_store_2')
- expect(store.reload.url).to eq('my_store_2.mydomain.dev')
- expect(Spree::Store.friendly.find('my_store').id).to eq(store.id)
- end
- end
- end
-
describe '#ensure_default_taxonomies_are_created' do
let(:store) { build(:store) }
@@ -325,44 +280,15 @@ describe Spree::Store, type: :model, without_global_store: true do
end
end
- describe '#import_products_from_store' do
- let(:store) { build(:store, import_products_from_store_id: other_store.id) }
- let(:other_store) { create(:store) }
- let!(:products) { create_list(:product, 2, stores: [other_store]) }
-
- it 'imports products from other store' do
- expect { store.save! }.to change(Spree::StoreProduct, :count).by(2)
- expect(store.products.count).to eq(2)
-
- expect(store.products.pluck(:id)).to match_array(products.pluck(:id))
- end
- end
-
- describe '#import_payment_methods_from_store' do
- let(:store) { build(:store, import_payment_methods_from_store_id: other_store.id) }
- let(:other_store) { create(:store) }
- let!(:payment_methods) { create_list(:payment_method, 2, stores: [other_store]) }
-
- it 'imports payment methods from other store' do
- expect { store.save! }.to change(Spree::StorePaymentMethod, :count).by(2)
- expect(store.payment_methods.count).to eq(2)
- end
- end
end
context 'Validations' do
describe '#code' do
- let(:default_store) { create(:store, default: true, code: 'store') }
-
- it 'cannot create 2 stores with the same code' do
- new_store = create(:store, name: default_store.code)
- expect(new_store.persisted?).to be(true)
- expect(new_store.code).not_to eq(default_store.code) # code is generated
- end
-
- it 'cannot create a store with reserved code' do
- new_store = build(:store, code: 'admin')
- expect(new_store.valid?).to be(false)
+ it 'requires code to be present' do
+ store = build(:store, code: nil, name: nil)
+ store.valid?
+ # set_default_code sets it to 'default' if blank, so it should be valid
+ expect(store.code).to eq('default')
end
end
end
@@ -389,68 +315,31 @@ describe Spree::Store, type: :model, without_global_store: true do
end
end
- describe '.by_url' do
- let!(:store) { create(:store, url: "website1.com\nwww.subdomain.com") }
- let!(:store_2) { create(:store, url: 'freethewhales.com') }
-
- it 'finds stores by url' do
- by_domain = Spree::Store.by_url('www.subdomain.com')
-
- expect(by_domain).to include(store)
- expect(by_domain).not_to include(store_2)
- end
- 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_2) { create(:store, default: false, url: 'www.subdomain.com') }
-
- it 'returns default when no domain' do
+ it 'returns Spree::Current.store' do
+ Spree::Current.store = store_1
expect(subject.class.current).to eql(store_1)
end
- it 'returns store for domain' do
- expect(Spree::Stores::FindCurrent.new(url: 'spreecommerce.com').execute).to eql(store_1)
- expect(Spree::Stores::FindCurrent.new(url: 'www.subdomain.com').execute).to eql(store_2)
+ it 'ignores url argument' do
+ Spree::Current.store = store_1
+ expect(subject.class.current('other-url.com')).to eql(store_1)
end
end
describe '.default' do
+ before { Rails.cache.clear }
+
context 'when a default store is already present' do
- let!(:store) { create(:store) }
- let!(:store_2) { create(:store, default: true) }
+ let!(:store_2) { create(:store, default: true) }
+
+ before { Spree::Store.where.not(id: store_2.id).update_all(default: false) }
it 'returns the already existing default store' do
expect(described_class.default).to eq(store_2)
end
-
- it "ensures there is a default if one doesn't exist yet" do
- expect(store_2.default).to be true
- end
-
- it 'ensures there is only one default' do
- [store, store_2].each(&:reload)
-
- expect(Spree::Store.where(default: true).count).to eq(1)
- expect(store_2.default).to be true
- expect(store.default).not_to be true
- end
-
- context 'when store is not saved' do
- before do
- store.default = true
- store.name = nil
- store.save
- end
-
- it 'ensure old default location still default' do
- [store, store_2].each(&:reload)
- expect(store.default).to be false
- expect(store_2.default).to be true
- end
- end
end
context 'when a default store is not present' do
@@ -472,11 +361,16 @@ describe Spree::Store, type: :model, without_global_store: true do
end
describe '.available_locales' do
- let!(:store) { create(:store, default_locale: 'en') }
- let!(:store_2) { create(:store, default_locale: 'de') }
- let!(:store_3) { create(:store, default_locale: 'en') }
+ let!(:store) { create(:store, default: true, default_locale: 'en', supported_locales: 'en,fr') }
+
+ before do
+ Spree::Store.where.not(id: store.id).update_all(default: false)
+ Rails.cache.clear
+ end
- it { expect(described_class.available_locales).to contain_exactly('en', 'de') }
+ it 'returns the default store supported locales' do
+ expect(described_class.available_locales).to contain_exactly('en', 'fr')
+ end
end
shared_context 'with checkout zone set' do
@@ -758,61 +652,12 @@ describe Spree::Store, type: :model, without_global_store: true do
end
end
- describe '#can_be_deleted?' do
- let(:default_store) { create(:store, default: true) }
-
- it 'cannot delete the only store' do
- Spree::Store.where.not(id: default_store.id).delete_all
- expect(default_store.can_be_deleted?).to eq(false)
- end
-
- it 'can delete when there are more than 1 stores' do
- create(:store)
- expect(default_store.can_be_deleted?).to eq(true)
- end
- end
-
describe 'soft deletion' do
- let!(:default_store) { create(:store, default: true) }
- let(:another_store) { create(:store) }
-
- context 'default store' do
- context 'with multiple stores' do
- before do
- Spree::Store.where.not(id: default_store.id).delete_all
- another_store
- end
-
- it 'can be deleted' do
- expect(default_store.deleted?).to eq(false)
- expect { default_store.destroy }.to change(default_store, :deleted_at)
- expect(default_store.deleted?).to eq(true)
- end
+ let!(:store) { create(:store) }
- it 'passes default flag to other store' do
- expect(another_store.default?).to eq(false)
- default_store.destroy
- expect(default_store.default?).to eq(false)
- expect(another_store.reload.default?).to eq(true)
- expect(described_class.default).to eq(another_store)
- end
- end
-
- context 'single store' do
- before { Spree::Store.where.not(id: default_store.id).delete_all }
-
- it 'cannot be deleted' do
- expect { default_store.destroy! }.to raise_error(ActiveRecord::RecordNotDestroyed)
- expect(default_store.errors.full_messages.to_sentence).to eq('Cannot destroy the only Store.')
- end
- end
- end
-
- context 'another store' do
- it 'soft-deletes when destroy is called' do
- another_store.destroy!
- expect(another_store.deleted_at).not_to be_nil
- end
+ it 'soft-deletes when destroy is called' do
+ store.destroy!
+ expect(store.deleted_at).not_to be_nil
end
end
@@ -874,14 +719,16 @@ describe Spree::Store, type: :model, without_global_store: true do
describe '#formatted_url_or_custom_domain' do
let(:store) { build(:store, code: 'mystore', url: 'mystore.mydomain.dev:3000') }
- context 'without custom domain' do
- it { expect(store.formatted_url_or_custom_domain).to eq('http://mystore.mydomain.dev:3000') }
+ it 'returns formatted_url as fallback' do
+ expect(store.formatted_url_or_custom_domain).to eq('http://mystore.mydomain.dev:3000')
end
+ end
- context 'with custom domain' do
- let!(:custom_domain) { create(:custom_domain, store: store, url: 'mystore.com') }
+ describe '#url_or_custom_domain' do
+ let(:store) { build(:store, code: 'mystore', url: 'mystore.mydomain.dev') }
- it { expect(store.formatted_url_or_custom_domain).to eq('http://mystore.com:3000') }
+ it 'returns url as fallback' do
+ expect(store.url_or_custom_domain).to eq('mystore.mydomain.dev')
end
end
end
diff --git a/backend/engines/core/spec/presenters/spree/csv/product_variant_presenter_spec.rb b/backend/engines/core/spec/presenters/spree/csv/product_variant_presenter_spec.rb
index 5f3920e16e..881b517538 100644
--- a/backend/engines/core/spec/presenters/spree/csv/product_variant_presenter_spec.rb
+++ b/backend/engines/core/spec/presenters/spree/csv/product_variant_presenter_spec.rb
@@ -109,17 +109,6 @@ RSpec.describe Spree::CSV::ProductVariantPresenter do
expect(subject[31]).to start_with("http://#{store.url}")
end
- context 'when custom domain is set' do
- let!(:custom_domain) { create(:custom_domain, store: store, url: 'custom.domain') }
-
- before { store.reload }
-
- it 'returns images with the custom domain' do
- expect(subject[29]).to start_with('http://custom.domain')
- expect(subject[30]).to start_with('http://custom.domain')
- expect(subject[31]).to start_with('http://custom.domain')
- end
- end
end
end
end
diff --git a/backend/engines/emails/spec/mailers/spree/order_mailer_spec.rb b/backend/engines/emails/spec/mailers/spree/order_mailer_spec.rb
index 2c23737bf1..00faf4450c 100644
--- a/backend/engines/emails/spec/mailers/spree/order_mailer_spec.rb
+++ b/backend/engines/emails/spec/mailers/spree/order_mailer_spec.rb
@@ -267,18 +267,6 @@ describe Spree::OrderMailer, type: :mailer do
expect(ActionMailer::Base.default_url_options[:host]).to eq(second_order.store.url)
end
- context 'with custom domain' do
- let(:store) { create(:store) }
- let!(:custom_domain) { create(:custom_domain, store: store, url: 'my-store.com') }
-
- it 'uses custom domain for URLs in emails' do
- email = described_class.confirm_email(order).deliver_now
- expect(ActionMailer::Base.default_url_options[:host]).to eq('my-store.com')
- # testing HTML body
- expect(email.body.parts.last.body).to include('my-store.com')
- expect(email.body.parts.last.body).not_to include(store.url)
- end
- end
end
describe '#payment_link_email' do
diff --git a/backend/engines/multi_store/Gemfile b/backend/engines/multi_store/Gemfile
new file mode 100644
index 0000000000..8ce2a0ac90
--- /dev/null
+++ b/backend/engines/multi_store/Gemfile
@@ -0,0 +1,13 @@
+eval_gemfile('../common_spree_dependencies.rb')
+
+gem 'spree', path: '../'
+gem 'spree_admin', path: '../admin'
+gem 'spree_emails', path: '../emails'
+
+gem 'sprockets-rails', '>= 3.5.2'
+
+group :test, :development do
+ gem 'selenium-webdriver'
+end
+
+gemspec
diff --git a/backend/engines/multi_store/LICENSE.md b/backend/engines/multi_store/LICENSE.md
new file mode 100644
index 0000000000..1708018b23
--- /dev/null
+++ b/backend/engines/multi_store/LICENSE.md
@@ -0,0 +1,14 @@
+Copyright (c) 2026 Vendo Connect Inc. Vendo Sp. z o.o.
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU Affero General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU Affero General Public License for more details.
+
+You should have received a copy of the GNU Affero General Public License
+along with this program. If not, see <https://www.gnu.org/licenses/>.
diff --git a/backend/engines/multi_store/Rakefile b/backend/engines/multi_store/Rakefile
new file mode 100644
index 0000000000..d1229bb687
--- /dev/null
+++ b/backend/engines/multi_store/Rakefile
@@ -0,0 +1,17 @@
+require 'rubygems'
+require 'rake'
+require 'rake/testtask'
+require 'rspec/core/rake_task'
+require 'spree/testing_support/common_rake'
+
+RSpec::Core::RakeTask.new
+
+task default: :spec
+
+desc "Generates a dummy app for testing"
+task :test_app do
+ ENV['LIB_NAME'] = 'spree/multi_store'
+ Rake::Task['common:test_app'].execute(
+ install_admin: true
+ )
+end
diff --git a/backend/engines/admin/app/controllers/spree/admin/custom_domains_controller.rb b/backend/engines/multi_store/app/controllers/spree/admin/custom_domains_controller.rb
similarity index 100%
rename from backend/engines/admin/app/controllers/spree/admin/custom_domains_controller.rb
rename to backend/engines/multi_store/app/controllers/spree/admin/custom_domains_controller.rb
diff --git a/backend/engines/multi_store/app/controllers/spree/admin/multi_store/stores_controller.rb b/backend/engines/multi_store/app/controllers/spree/admin/multi_store/stores_controller.rb
new file mode 100644
index 0000000000..aa6be11d12
--- /dev/null
+++ b/backend/engines/multi_store/app/controllers/spree/admin/multi_store/stores_controller.rb
@@ -0,0 +1,38 @@
+module Spree
+ module Admin
+ module MultiStore
+ class StoresController < Spree::Admin::BaseController
+ def new
+ @store = Spree::Store.new(
+ default_country_iso: current_store.default_country_iso,
+ default_currency: current_store.default_currency
+ )
+ render :new, layout: 'spree/admin_wizard'
+ end
+
+ def create
+ @store = Spree::Store.new(permitted_store_params)
+ @store.mail_from_address = current_store.mail_from_address
+
+ if @store.save
+ # Move/copy all existing users (staff) to the new store
+ current_store.role_users.each do |role_user|
+ @store.add_user(role_user.user, role_user.role)
+ end
+
+ flash[:success] = flash_message_for(@store, :successfully_created)
+ redirect_to spree.admin_getting_started_url(host: @store.url), allow_other_host: true
+ else
+ render :new, status: :unprocessable_content
+ end
+ end
+
+ protected
+
+ def permitted_store_params
+ params.require(:store).permit(permitted_store_attributes)
+ end
+ end
+ end
+ end
+end
diff --git a/backend/engines/core/app/finders/spree/stores/find_current.rb b/backend/engines/multi_store/app/finders/spree/stores/find_current.rb
similarity index 100%
rename from backend/engines/core/app/finders/spree/stores/find_current.rb
rename to backend/engines/multi_store/app/finders/spree/stores/find_current.rb
diff --git a/backend/engines/multi_store/app/helpers/spree/admin/multi_store_helper.rb b/backend/engines/multi_store/app/helpers/spree/admin/multi_store_helper.rb
new file mode 100644
index 0000000000..9fbde228f4
--- /dev/null
+++ b/backend/engines/multi_store/app/helpers/spree/admin/multi_store_helper.rb
@@ -0,0 +1,11 @@
+module Spree
+ module Admin
+ module MultiStoreHelper
+ def available_stores
+ scope = Spree::Store.accessible_by(current_ability, :manage).includes(:logo_attachment, :favicon_image_attachment)
+ scope = scope.includes(:default_custom_domain) if Spree::Store.reflect_on_association(:default_custom_domain)
+ @available_stores ||= scope
+ end
+ end
+ end
+end
diff --git a/backend/engines/core/app/models/concerns/spree/multi_store_resource.rb b/backend/engines/multi_store/app/models/concerns/spree/multi_store_resource.rb
similarity index 100%
rename from backend/engines/core/app/models/concerns/spree/multi_store_resource.rb
rename to backend/engines/multi_store/app/models/concerns/spree/multi_store_resource.rb
diff --git a/backend/engines/multi_store/app/models/concerns/spree/store/multi_store_methods.rb b/backend/engines/multi_store/app/models/concerns/spree/store/multi_store_methods.rb
new file mode 100644
index 0000000000..6631ae0312
--- /dev/null
+++ b/backend/engines/multi_store/app/models/concerns/spree/store/multi_store_methods.rb
@@ -0,0 +1,122 @@
+module Spree
+ module Store::MultiStoreMethods
+ extend ActiveSupport::Concern
+
+ RESERVED_CODES = %w(
+ admin default app api www cdn files assets checkout account auth login user
+ )
+
+ included do
+ has_many :custom_domains, class_name: 'Spree::CustomDomain', dependent: :destroy
+ has_one :default_custom_domain, -> { where(default: true) }, class_name: 'Spree::CustomDomain'
+
+ attribute :import_products_from_store_id, :string, default: nil
+ attribute :import_payment_methods_from_store_id, :string, default: nil
+
+ attr_accessor :skip_validate_not_last
+
+ validates :code, uniqueness: { case_sensitive: false, conditions: -> { with_deleted } },
+ exclusion: Spree::Store::MultiStoreMethods::RESERVED_CODES
+
+ before_validation :set_url
+ after_create :import_products_from_store, if: -> { import_products_from_store_id.present? }
+ after_create :import_payment_methods_from_store, if: -> { import_payment_methods_from_store_id.present? }
+ after_commit :handle_code_changes, on: :update, if: -> { code_previously_changed? }
+ before_destroy :validate_not_last, unless: :skip_validate_not_last
+ before_destroy :pass_default_flag_to_other_store
+
+ scope :by_custom_domain, ->(url) { left_joins(:custom_domains).where("#{Spree::CustomDomain.table_name}.url" => url) }
+ scope :by_url, ->(url) { where(url: url).or(where("#{table_name}.url like ?", "%#{url}%")) }
+
+ # Re-configure FriendlyId to use :history for code tracking across renames
+ friendly_id :slug_candidates, use: [:slugged, :history], slug_column: :code, routes: :normal
+ end
+
+ class_methods do
+ def current(url = nil)
+ if url.present?
+ Spree.current_store_finder.new(url: url).execute
+ else
+ Spree::Current.store
+ end
+ end
+
+ def available_locales
+ Spree::Store.all.map(&:supported_locales_list).flatten.uniq
+ end
+ end
+
+ def formatted_custom_domain
+ return unless default_custom_domain
+
+ @formatted_custom_domain ||= if Rails.env.development? || Rails.env.test?
+ URI::Generic.build(
+ scheme: Rails.application.routes.default_url_options[:protocol] || 'http',
+ host: default_custom_domain.url,
+ port: Rails.application.routes.default_url_options[:port]
+ ).to_s
+ else
+ URI::HTTPS.build(host: default_custom_domain.url).to_s
+ end
+ end
+
+ def can_be_deleted?
+ self.class.where.not(id: id).any?
+ end
+
+ def import_products_from_store
+ store = Spree::Store.find(import_products_from_store_id)
+ product_ids = store.products.pluck(:id)
+
+ return if product_ids.empty?
+
+ Spree::StoreProduct.insert_all(product_ids.map { |product_id| { store_id: id, product_id: product_id } })
+ end
+
+ def import_payment_methods_from_store
+ store = Spree::Store.find(import_payment_methods_from_store_id)
+ payment_method_ids = store.payment_method_ids
+
+ return if payment_method_ids.empty?
+
+ Spree::StorePaymentMethod.insert_all(payment_method_ids.map { |payment_method_id| { store_id: id, payment_method_id: payment_method_id } })
+ end
+
+ private
+
+ def validate_not_last
+ unless can_be_deleted?
+ errors.add(:base, :cannot_destroy_only_store)
+ throw(:abort)
+ end
+ end
+
+ def pass_default_flag_to_other_store
+ if default? && can_be_deleted?
+ self.class.where.not(id: id).first.update!(default: true)
+ self.default = false
+ end
+ end
+
+ def handle_code_changes
+ # hook for custom logic on code changes
+ end
+
+ # Auto-assign internal URL for stores based on code + root domain
+ def set_url
+ return if url_changed?
+ return unless code_changed?
+ return unless Spree.root_domain.present?
+
+ self.url = [code, Spree.root_domain].join('.')
+ end
+
+ def slug_candidates
+ []
+ end
+
+ def should_generate_new_friendly_id?
+ false
+ end
+ end
+end
diff --git a/backend/engines/multi_store/app/models/concerns/spree/store/multi_store_overrides.rb b/backend/engines/multi_store/app/models/concerns/spree/store/multi_store_overrides.rb
new file mode 100644
index 0000000000..b26335cf64
--- /dev/null
+++ b/backend/engines/multi_store/app/models/concerns/spree/store/multi_store_overrides.rb
@@ -0,0 +1,39 @@
+module Spree
+ module Store::MultiStoreOverrides
+ def url_or_custom_domain
+ default_custom_domain&.url || url
+ end
+
+ def formatted_url_or_custom_domain
+ formatted_custom_domain || formatted_url
+ end
+
+ def can_be_deleted?
+ self.class.where.not(id: id).any?
+ end
+
+ private
+
+ # Override core's simple set_default_code with full code generation logic
+ def set_default_code
+ self.code = if code.present?
+ code.parameterize.strip
+ elsif name.present?
+ name.parameterize.strip
+ end
+
+ return if self.code.blank?
+
+ # ensure code is unique
+ self.code = [name.parameterize, rand(9999)].join('-') while Spree::Store.with_deleted.where(code: self.code).exists?
+ end
+
+ def should_generate_new_friendly_id?
+ false
+ end
+
+ def slug_candidates
+ []
+ end
+ end
+end
diff --git a/backend/engines/core/app/models/spree/custom_domain.rb b/backend/engines/multi_store/app/models/spree/custom_domain.rb
similarity index 100%
rename from backend/engines/core/app/models/spree/custom_domain.rb
rename to backend/engines/multi_store/app/models/spree/custom_domain.rb
diff --git a/backend/engines/admin/app/views/spree/admin/custom_domains/_custom_domain.html.erb b/backend/engines/multi_store/app/views/spree/admin/custom_domains/_custom_domain.html.erb
similarity index 100%
rename from backend/engines/admin/app/views/spree/admin/custom_domains/_custom_domain.html.erb
rename to backend/engines/multi_store/app/views/spree/admin/custom_domains/_custom_domain.html.erb
diff --git a/backend/engines/admin/app/views/spree/admin/custom_domains/_custom_domains.html.erb b/backend/engines/multi_store/app/views/spree/admin/custom_domains/_custom_domains.html.erb
similarity index 100%
rename from backend/engines/admin/app/views/spree/admin/custom_domains/_custom_domains.html.erb
rename to backend/engines/multi_store/app/views/spree/admin/custom_domains/_custom_domains.html.erb
diff --git a/backend/engines/admin/app/views/spree/admin/custom_domains/_form.html.erb b/backend/engines/multi_store/app/views/spree/admin/custom_domains/_form.html.erb
similarity index 100%
rename from backend/engines/admin/app/views/spree/admin/custom_domains/_form.html.erb
rename to backend/engines/multi_store/app/views/spree/admin/custom_domains/_form.html.erb
diff --git a/backend/engines/admin/app/views/spree/admin/custom_domains/edit.html.erb b/backend/engines/multi_store/app/views/spree/admin/custom_domains/edit.html.erb
similarity index 100%
rename from backend/engines/admin/app/views/spree/admin/custom_domains/edit.html.erb
rename to backend/engines/multi_store/app/views/spree/admin/custom_domains/edit.html.erb
diff --git a/backend/engines/admin/app/views/spree/admin/custom_domains/index.html.erb b/backend/engines/multi_store/app/views/spree/admin/custom_domains/index.html.erb
similarity index 100%
rename from backend/engines/admin/app/views/spree/admin/custom_domains/index.html.erb
rename to backend/engines/multi_store/app/views/spree/admin/custom_domains/index.html.erb
diff --git a/backend/engines/admin/app/views/spree/admin/custom_domains/new.html.erb b/backend/engines/multi_store/app/views/spree/admin/custom_domains/new.html.erb
similarity index 100%
rename from backend/engines/admin/app/views/spree/admin/custom_domains/new.html.erb
rename to backend/engines/multi_store/app/views/spree/admin/custom_domains/new.html.erb
diff --git a/backend/engines/admin/app/views/spree/admin/stores/new.html.erb b/backend/engines/multi_store/app/views/spree/admin/multi_store/stores/new.html.erb
similarity index 100%
rename from backend/engines/admin/app/views/spree/admin/stores/new.html.erb
rename to backend/engines/multi_store/app/views/spree/admin/multi_store/stores/new.html.erb
diff --git a/backend/engines/admin/app/views/spree/admin/stores/new.turbo_stream.erb b/backend/engines/multi_store/app/views/spree/admin/multi_store/stores/new.turbo_stream.erb
similarity index 100%
rename from backend/engines/admin/app/views/spree/admin/stores/new.turbo_stream.erb
rename to backend/engines/multi_store/app/views/spree/admin/multi_store/stores/new.turbo_stream.erb
diff --git a/backend/engines/multi_store/app/views/spree/admin/products/form/_stores.html.erb b/backend/engines/multi_store/app/views/spree/admin/products/form/_stores.html.erb
new file mode 100644
index 0000000000..77d205fdcb
--- /dev/null
+++ b/backend/engines/multi_store/app/views/spree/admin/products/form/_stores.html.erb
@@ -0,0 +1,29 @@
+<% if available_stores.count > 1 %>
+ <div class="card mb-6">
+ <div class="card-header">
+ <h5 class="card-title">
+ <%= Spree.t(:stores) %>
+ </h5>
+ </div>
+ <div class="card-body">
+ <div class="form-group">
+ <% if can? :modify, Spree::StoreProduct %>
+ <p class="form-text"><%= Spree.t('admin.products.stores.choose_stores') %>.</p>
+
+ <%= f.collection_check_boxes :store_ids, available_stores, :id, :name do |b| %>
+ <div class="custom-control form-checkbox mb-1">
+ <%= b.check_box(class: 'custom-control-input') %>
+ <%= b.label(class: 'custom-control-label') %>
+ </div>
+ <% end %>
+ <% elsif @product.stores.any? %>
+ <ul class="text_list">
+ <% @product.stores.pluck(:name).each do |store_name| %>
+ <li><%= store_name %></li>
+ <% end %>
+ </ul>
+ <% end %>
+ </div>
+ </div>
+ </div>
+<% end %>
diff --git a/backend/engines/multi_store/app/views/spree/admin/shared/sidebar/_store_dropdown.html.erb b/backend/engines/multi_store/app/views/spree/admin/shared/sidebar/_store_dropdown.html.erb
new file mode 100644
index 0000000000..cba68ac1e8
--- /dev/null
+++ b/backend/engines/multi_store/app/views/spree/admin/shared/sidebar/_store_dropdown.html.erb
@@ -0,0 +1,55 @@
+<div class="store-dropdown" id="store-menu">
+ <%= dropdown do %>
+ <%= dropdown_toggle class: 'store-dropdown-button dropdown-toggle' do %>
+ <%= store_admin_icon(current_store, height: 32, width: 32) %>
+ <span class="store-name hidden lg:block">
+ <%= truncate(current_store.name, length: 10) %>
+ </span>
+ <% end %>
+ <%= dropdown_menu do %>
+ <%= link_to current_store.formatted_url_or_custom_domain, class: 'dropdown-item', target: '_blank' do %>
+ <%= icon 'eye' %>
+ <%= Spree.t(:view_store) %>
+ <% end %>
+ <%= link_to spree.edit_admin_theme_path(current_store.default_theme), class: 'dropdown-item', data: { turbo_prefetch: false } do %>
+ <%= icon 'tools' %>
+ <%= Spree.t('admin.edit_theme') %>
+ <% end if current_store.respond_to?(:default_theme) && current_store.default_theme && can?(:manage, Spree::Theme) %>
+
+ <% if available_stores.count > 1 %>
+ <div class="dropdown-divider"></div>
+ <h6 class="dropdown-header">
+ <%= Spree.t(:switch_store) %>
+ </h6>
+ <div class="store-chooser">
+ <% available_stores.each do |store| %>
+ <% if store.id == current_store.id %>
+ <div class="store-chooser-item active">
+ <%= store_admin_icon(store, height: 32, width: 32) %>
+ <span class="flex flex-col mr-2">
+ <%= store.name %>
+ <small class="text-gray-600">
+ <%= store.url_or_custom_domain %>
+ </small>
+ </span>
+ <%= icon('check', class: 'ml-auto') %>
+ </div>
+ <% else %>
+ <%= link_to spree.admin_dashboard_url(host: store.url), class: 'store-chooser-item' do %>
+ <%= store_admin_icon(store, height: 32, width: 32) %>
+ <span class="flex flex-col">
+ <%= store.name %>
+ <small class="text-gray-600">
+ <%= store.url_or_custom_domain %>
+ </small>
+ </span>
+ <% end %>
+ <% end %>
+ <% end %>
+ </div>
+ <% end %>
+ <% end %>
+ <% end %>
+
+ <%= render 'spree/admin/shared/new_item_dropdown' %>
+</div>
diff --git a/backend/engines/multi_store/config/initializers/spree_multi_store.rb b/backend/engines/multi_store/config/initializers/spree_multi_store.rb
new file mode 100644
index 0000000000..301a39cd80
--- /dev/null
+++ b/backend/engines/multi_store/config/initializers/spree_multi_store.rb
@@ -0,0 +1,49 @@
+Rails.application.config.after_initialize do
+ # Override store finder to use URL-based resolution
+ Spree::Dependencies.current_store_finder = 'Spree::Stores::FindCurrent'
+
+ # Include Store with multi-store methods (custom domains, URL resolution, imports)
+ # include is needed to fire the `included` block (associations, scopes, callbacks)
+ Spree::Store.include Spree::Store::MultiStoreMethods
+
+ # Prepend overrides for instance methods that are defined in core's Store model
+ # (included module methods don't override methods defined directly on the class)
+ Spree::Store.prepend Spree::Store::MultiStoreOverrides
+
+ # Override singleton methods defined in core's Store model
+ # (singleton methods take precedence over methods from included/prepended modules)
+ Spree::Store.define_singleton_method(:current) do |url = nil|
+ if url.present?
+ Spree.current_store_finder.new(url: url).execute
+ else
+ Spree::Current.store
+ end
+ end
+
+ # Aggregate locales across all stores instead of just the default store
+ Spree::Store.define_singleton_method(:available_locales) do
+ Spree::Store.all.map(&:supported_locales_list).flatten.uniq
+ end
+
+ # Replace StoreScopedResource with MultiStoreResource on shared models
+ # MultiStoreResource adds validation that at least one store is assigned
+ Spree::Product.include Spree::MultiStoreResource
+ Spree::PaymentMethod.include Spree::MultiStoreResource
+ Spree::Promotion.include Spree::MultiStoreResource
+
+ # Add import permitted attributes for store creation
+ Spree::PermittedAttributes.store_attributes.push(
+ :import_products_from_store_id,
+ :import_payment_methods_from_store_id
+ )
+
+ # Register admin partials for multi-store UI
+ if defined?(Spree::Admin)
+ Spree.admin.partials.product_form_sidebar << 'spree/admin/products/form/stores'
+ end
+
+ # Add CustomDomain to metafields enabled resources if metafields are available
+ if Spree::Config.respond_to?(:metafields_enabled_resources)
+ Spree::Config.metafields_enabled_resources << Spree::CustomDomain unless Spree::Config.metafields_enabled_resources.include?(Spree::CustomDomain)
+ end
+end
diff --git a/backend/engines/multi_store/config/initializers/spree_multi_store_navigation.rb b/backend/engines/multi_store/config/initializers/spree_multi_store_navigation.rb
new file mode 100644
index 0000000000..8277d78487
--- /dev/null
+++ b/backend/engines/multi_store/config/initializers/spree_multi_store_navigation.rb
@@ -0,0 +1,14 @@
+Rails.application.config.after_initialize do
+ next unless defined?(Spree::Admin)
+
+ settings_nav = Spree.admin.navigation.settings
+
+ # Domains
+ settings_nav.add :domains,
+ label: :domains,
+ url: :admin_custom_domains_path,
+ icon: 'world-www',
+ position: 60,
+ active: -> { controller_name == 'custom_domains' },
+ if: -> { can?(:manage, Spree::CustomDomain) }
+end
diff --git a/backend/engines/multi_store/config/locales/en.yml b/backend/engines/multi_store/config/locales/en.yml
new file mode 100644
index 0000000000..5b412bbd23
--- /dev/null
+++ b/backend/engines/multi_store/config/locales/en.yml
@@ -0,0 +1,5 @@
+---
+en:
+ spree:
+ multi_store:
+ name: "Multi-Store"
diff --git a/backend/engines/multi_store/config/routes.rb b/backend/engines/multi_store/config/routes.rb
new file mode 100644
index 0000000000..b710ac890a
--- /dev/null
+++ b/backend/engines/multi_store/config/routes.rb
@@ -0,0 +1,6 @@
+Spree::Core::Engine.add_routes do
+ namespace :admin, path: Spree.admin_path do
+ resources :stores, only: [:new, :create], controller: 'multi_store/stores'
+ resources :custom_domains, except: :show
+ end
+end
diff --git a/backend/engines/multi_store/lib/spree/multi_store.rb b/backend/engines/multi_store/lib/spree/multi_store.rb
new file mode 100644
index 0000000000..2caa9ff4a5
--- /dev/null
+++ b/backend/engines/multi_store/lib/spree/multi_store.rb
@@ -0,0 +1,2 @@
+require 'spree_core'
+require 'spree/multi_store/engine'
diff --git a/backend/engines/multi_store/lib/spree/multi_store/engine.rb b/backend/engines/multi_store/lib/spree/multi_store/engine.rb
new file mode 100644
index 0000000000..76b8312038
--- /dev/null
+++ b/backend/engines/multi_store/lib/spree/multi_store/engine.rb
@@ -0,0 +1,22 @@
+module Spree
+ module MultiStore
+ class Engine < ::Rails::Engine
+ isolate_namespace Spree
+ engine_name 'spree_multi_store'
+
+ # Rails 7.1 introduced a new feature that raises an error if a callback action is missing.
+ # We need to disable it as we use a lot of concerns that add callback actions.
+ initializer 'spree.multi_store.disable_raise_on_missing_callback_actions' do |app|
+ app.config.action_controller.raise_on_missing_callback_actions = false
+ end
+
+ initializer 'spree.multi_store.helpers' do
+ Rails.application.config.after_initialize do
+ if defined?(Spree::Admin::BaseController)
+ Spree::Admin::BaseController.helper Spree::Admin::MultiStoreHelper
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/backend/engines/multi_store/lib/spree_multi_store.rb b/backend/engines/multi_store/lib/spree_multi_store.rb
new file mode 100644
index 0000000000..fc752b8240
--- /dev/null
+++ b/backend/engines/multi_store/lib/spree_multi_store.rb
@@ -0,0 +1 @@
+require 'spree/multi_store'
diff --git a/backend/engines/admin/spec/controllers/spree/admin/custom_domains_controller_spec.rb b/backend/engines/multi_store/spec/controllers/spree/admin/custom_domains_controller_spec.rb
similarity index 100%
rename from backend/engines/admin/spec/controllers/spree/admin/custom_domains_controller_spec.rb
rename to backend/engines/multi_store/spec/controllers/spree/admin/custom_domains_controller_spec.rb
diff --git a/backend/engines/multi_store/spec/controllers/spree/admin/multi_store/stores_controller_spec.rb b/backend/engines/multi_store/spec/controllers/spree/admin/multi_store/stores_controller_spec.rb
new file mode 100644
index 0000000000..7887be3372
--- /dev/null
+++ b/backend/engines/multi_store/spec/controllers/spree/admin/multi_store/stores_controller_spec.rb
@@ -0,0 +1,105 @@
+require 'spec_helper'
+
+describe Spree::Admin::MultiStore::StoresController, type: :controller do
+ stub_authorization!
+ render_views
+
+ let(:store) { Spree::Store.default }
+
+ before do
+ allow(controller).to receive(:current_store).and_return(store)
+ allow(Spree).to receive(:root_domain).and_return('lvh.me')
+ end
+
+ describe 'GET #new' do
+ subject { get :new }
+
+ it 'renders the new template' do
+ subject
+ expect(response).to have_http_status(:ok)
+ end
+
+ it 'initializes store with current store defaults' do
+ subject
+ new_store = assigns(:store)
+ expect(new_store.default_currency).to eq(store.default_currency)
+ end
+ end
+
+ describe 'POST #create' do
+ subject { post :create, params: { store: store_params } }
+
+ let(:store_params) do
+ {
+ name: 'New Store',
+ default_currency: 'USD',
+ default_locale: 'en'
+ }
+ end
+
+ context 'with valid params' do
+ it 'creates a new store' do
+ expect { subject }.to change(Spree::Store, :count).by(1)
+ end
+
+ it 'sets the mail_from_address from the current store' do
+ subject
+ expect(Spree::Store.last.mail_from_address).to eq(store.mail_from_address)
+ end
+
+ it 'copies users to the new store' do
+ admin_user = create(:admin_user)
+ store.add_user(admin_user)
+
+ subject
+
+ new_store = Spree::Store.last
+ expect(new_store.users).to include(admin_user)
+ end
+
+ it 'auto-generates URL from code and root_domain' do
+ subject
+ new_store = Spree::Store.last
+ expect(new_store.url).to include('lvh.me')
+ end
+
+ it 'redirects to the getting started page on the new store' do
+ subject
+ new_store = Spree::Store.last
+ expect(response).to redirect_to(spree.admin_getting_started_url(host: new_store.url))
+ end
+ end
+
+ context 'with invalid params' do
+ let(:store_params) { { name: '' } }
+
+ it 'does not create a store' do
+ expect { subject }.not_to change(Spree::Store, :count)
+ end
+
+ it 'renders the new template' do
+ subject
+ expect(response).to have_http_status(:unprocessable_content)
+ end
+ end
+
+ context 'with import_products_from_store_id' do
+ let!(:product) { create(:product, stores: [store]) }
+
+ let(:store_params) do
+ {
+ name: 'Store With Products',
+ default_currency: 'USD',
+ default_locale: 'en',
+ import_products_from_store_id: store.id
+ }
+ end
+
+ it 'imports products from the source store' do
+ subject
+ new_store = Spree::Store.last
+ expect(new_store.products).to include(product)
+ end
+ end
+ end
+end
diff --git a/backend/engines/admin/spec/features/spree/admin/settings/custom_domains_spec.rb b/backend/engines/multi_store/spec/features/spree/admin/settings/custom_domains_spec.rb
similarity index 100%
rename from backend/engines/admin/spec/features/spree/admin/settings/custom_domains_spec.rb
rename to backend/engines/multi_store/spec/features/spree/admin/settings/custom_domains_spec.rb
diff --git a/backend/engines/core/spec/finders/spree/stores/find_current_spec.rb b/backend/engines/multi_store/spec/finders/spree/stores/find_current_spec.rb
similarity index 100%
rename from backend/engines/core/spec/finders/spree/stores/find_current_spec.rb
rename to backend/engines/multi_store/spec/finders/spree/stores/find_current_spec.rb
diff --git a/backend/engines/admin/spec/helpers/spree/admin/stores_helper_spec.rb b/backend/engines/multi_store/spec/helpers/spree/admin/multi_store_helper_spec.rb
similarity index 94%
rename from backend/engines/admin/spec/helpers/spree/admin/stores_helper_spec.rb
rename to backend/engines/multi_store/spec/helpers/spree/admin/multi_store_helper_spec.rb
index 04c004913d..d76c6904c7 100644
--- a/backend/engines/admin/spec/helpers/spree/admin/stores_helper_spec.rb
+++ b/backend/engines/multi_store/spec/helpers/spree/admin/multi_store_helper_spec.rb
@@ -1,6 +1,6 @@
require 'spec_helper'
-RSpec.describe Spree::Admin::StoresHelper, type: :helper do
+RSpec.describe Spree::Admin::MultiStoreHelper, type: :helper do
describe '#available_stores' do
let(:user) { create(:admin_user) }
let!(:store1) { create(:store, name: 'Store 1') }
diff --git a/backend/engines/core/spec/models/spree/custom_domain_spec.rb b/backend/engines/multi_store/spec/models/spree/custom_domain_spec.rb
similarity index 100%
rename from backend/engines/core/spec/models/spree/custom_domain_spec.rb
rename to backend/engines/multi_store/spec/models/spree/custom_domain_spec.rb
diff --git a/backend/engines/multi_store/spec/models/spree/store/multi_store_methods_spec.rb b/backend/engines/multi_store/spec/models/spree/store/multi_store_methods_spec.rb
new file mode 100644
index 0000000000..f8660c095a
--- /dev/null
+++ b/backend/engines/multi_store/spec/models/spree/store/multi_store_methods_spec.rb
@@ -0,0 +1,295 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Store::MultiStoreMethods, type: :model do
+ let!(:default_store) { @default_store }
+
+ before do
+ allow(Spree).to receive(:root_domain).and_return('mydomain.dev')
+ end
+
+ describe 'URL-based store resolution' do
+ # Use a URL that won't substring-match via LIKE with other test URLs
+ let!(:store_2) { create(:store, url: 'mystore.test') }
+
+ before { Spree::Current.store = nil }
+
+ describe '.by_url' do
+ it 'finds stores matching URL' do
+ expect(Spree::Store.by_url('mystore.test')).to include(store_2)
+ end
+
+ it 'finds stores matching partial URL via LIKE' do
+ expect(Spree::Store.by_url('mystore.test')).to include(store_2)
+ end
+
+ it 'does not return non-matching stores' do
+ expect(Spree::Store.by_url('nomatch.xyz')).not_to include(store_2)
+ end
+ end
+
+ describe '.by_custom_domain' do
+ let!(:custom_domain) { create(:custom_domain, store: store_2, url: 'myshop.com') }
+
+ it 'finds store by custom domain' do
+ expect(Spree::Store.by_custom_domain('myshop.com')).to include(store_2)
+ end
+
+ it 'does not return non-matching stores' do
+ expect(Spree::Store.by_custom_domain('nomatch.xyz')).not_to include(store_2)
+ end
+ end
+
+ describe '.current' do
+ it 'resolves store by URL via finder' do
+ expect(Spree::Store.current('mystore.test')).to eq(store_2)
+ end
+
+ it 'falls back to default store for unknown URL' do
+ expect(Spree::Store.current('unknown.com')).to eq(default_store)
+ end
+
+ it 'sets Spree::Current.store' do
+ Spree::Store.current('mystore.test')
+ expect(Spree::Current.store).to eq(store_2)
+ end
+
+ context 'with custom domain' do
+ let!(:custom_domain) { create(:custom_domain, store: store_2, url: 'myshop.com') }
+
+ it 'resolves store by custom domain' do
+ expect(Spree::Store.current('myshop.com')).to eq(store_2)
+ end
+ end
+ end
+ end
+
+ describe 'custom domain associations' do
+ let(:store) { create(:store) }
+
+ it 'has_many custom_domains' do
+ domain = create(:custom_domain, store: store)
+ expect(store.custom_domains).to include(domain)
+ end
+
+ it 'returns default_custom_domain' do
+ # First domain created becomes default via ensure_default callback
+ first_domain = create(:custom_domain, store: store, url: 'first.com')
+ expect(first_domain.reload.default).to be true
+
+ # Second domain is not default
+ second_domain = create(:custom_domain, store: store, url: 'second.com')
+ expect(second_domain.reload.default).to be false
+
+ expect(store.reload.default_custom_domain).to eq(first_domain)
+ end
+
+ it 'destroys custom domains when store is destroyed' do
+ create(:custom_domain, store: store)
+ expect { store.destroy }.to change(Spree::CustomDomain, :count).by(-1)
+ end
+ end
+
+ describe '#url_or_custom_domain' do
+ let(:store) { create(:store, url: 'store.mysite.com') }
+
+ context 'without custom domain' do
+ it 'returns the store url' do
+ expect(store.url_or_custom_domain).to eq('store.mysite.com')
+ end
+ end
+
+ context 'with custom domain' do
+ let!(:domain) { create(:custom_domain, store: store, url: 'custom.shop.com') }
+
+ it 'returns the custom domain url' do
+ # First custom domain becomes default via ensure_default callback
+ expect(domain.reload.default).to be true
+ expect(store.reload.url_or_custom_domain).to eq('custom.shop.com')
+ end
+ end
+ end
+
+ describe 'product import on create' do
+ let!(:product) { create(:product, stores: [default_store]) }
+
+ it 'imports products from source store' do
+ new_store = build(:store, import_products_from_store_id: default_store.id)
+ new_store.save!
+
+ expect(new_store.products).to include(product)
+ end
+
+ it 'does not import products when source store is not set' do
+ new_store = create(:store)
+ expect(new_store.products).to be_empty
+ end
+ end
+
+ describe 'payment method import on create' do
+ let!(:payment_method) { create(:credit_card_payment_method, stores: [default_store]) }
+
+ it 'imports payment methods from source store' do
+ new_store = build(:store, import_payment_methods_from_store_id: default_store.id)
+ new_store.save!
+
+ expect(new_store.payment_methods).to include(payment_method)
+ end
+ end
+
+ describe '#set_code (via set_default_code override)' do
+ let(:store) { build(:store, name: 'My Store', code: nil) }
+
+ it 'generates code from name' do
+ store.valid?
+ expect(store.code).to eq('my-store')
+ end
+
+ context 'when code is already set' do
+ let(:store) { build(:store, name: 'Store', code: 'custom-code') }
+
+ it 'parameterizes the existing code' do
+ store.valid?
+ expect(store.code).to eq('custom-code')
+ end
+ end
+
+ context 'when code is already taken' do
+ let!(:existing_store) { create(:store, code: 'store') }
+ let(:store) { build(:store, name: 'Store', code: existing_store.code) }
+
+ it 'generates a unique code' do
+ store.valid?
+ expect(store.code).not_to eq(existing_store.code)
+ expect(store.code).to match(/store-\d+/)
+ end
+ end
+ end
+
+ describe '#set_url' do
+ let(:store) { build(:store, code: 'my_store', url: nil) }
+
+ context 'on create' do
+ it 'sets url from code and root domain' do
+ store.save!
+ expect(store.url).to eq('my_store.mydomain.dev')
+ end
+ end
+
+ context 'on update with code change' do
+ let!(:store) { create(:store, code: 'my_store', url: 'my_store.mydomain.dev') }
+
+ it 'updates url and keeps old code via FriendlyId history' do
+ expect(store.url).to eq('my_store.mydomain.dev')
+ store.update!(code: 'my_store_2')
+ expect(store.reload.url).to eq('my_store_2.mydomain.dev')
+ expect(Spree::Store.friendly.find('my_store').id).to eq(store.id)
+ end
+ end
+ end
+
+ describe 'code validation' do
+ it 'auto-generates unique code when code is taken' do
+ existing = create(:store, code: 'store')
+ new_store = create(:store, name: 'Store', code: existing.code)
+ expect(new_store.persisted?).to be true
+ expect(new_store.code).not_to eq(existing.code)
+ expect(new_store.code).to match(/store-\d+/)
+ end
+
+ it 'cannot create a store with reserved code' do
+ store = build(:store, code: 'admin')
+ expect(store).not_to be_valid
+ expect(store.errors[:code]).to be_present
+ end
+ end
+
+ describe '#ensure_default_exists_and_is_unique' do
+ it 'ensures there is only one default store' do
+ store_1 = create(:store, default: true)
+ store_2 = create(:store, default: true)
+
+ store_1.reload
+ expect(store_1.default).to be false
+ expect(store_2.default).to be true
+ end
+
+ it 'auto-sets default if no default store exists' do
+ Spree::Store.update_all(default: false)
+ store = create(:store)
+ expect(store.default).to be true
+ end
+
+ context 'when store fails to save' do
+ let!(:store_1) { create(:store, default: true) }
+
+ it 'does not change default flag of other stores' do
+ store_2 = build(:store, default: true, name: nil) # will fail validation
+ store_2.save
+
+ store_1.reload
+ expect(store_1.default).to be true
+ end
+ end
+ end
+
+ describe '#can_be_deleted?' do
+ it 'cannot delete the only store' do
+ Spree::Store.where.not(id: default_store.id).delete_all
+ expect(default_store.can_be_deleted?).to eq(false)
+ end
+
+ it 'can delete when there are more than 1 stores' do
+ create(:store)
+ expect(default_store.can_be_deleted?).to eq(true)
+ end
+ end
+
+ describe 'soft deletion with multi-store protection' do
+ context 'default store with multiple stores' do
+ let!(:store_to_destroy) { create(:store, default: true) }
+ let!(:another_store) { create(:store) }
+
+ before { Spree::Store.where.not(id: [store_to_destroy.id, another_store.id]).delete_all }
+
+ it 'can be deleted' do
+ expect { store_to_destroy.destroy }.to change(store_to_destroy, :deleted_at)
+ expect(store_to_destroy.deleted?).to eq(true)
+ end
+
+ it 'passes default flag to other store' do
+ expect(another_store.default?).to eq(false)
+ store_to_destroy.destroy
+ expect(store_to_destroy.default?).to eq(false)
+ expect(another_store.reload.default?).to eq(true)
+ expect(Spree::Store.default).to eq(another_store)
+ end
+ end
+
+ context 'single store' do
+ before { Spree::Store.where.not(id: default_store.id).delete_all }
+
+ it 'cannot be deleted' do
+ expect { default_store.destroy! }.to raise_error(ActiveRecord::RecordNotDestroyed)
+ expect(default_store.errors.full_messages.to_sentence).to eq('Cannot destroy the only Store.')
+ end
+ end
+
+ context 'non-default store' do
+ let!(:another_store) { create(:store) }
+
+ it 'soft-deletes when destroy is called' do
+ another_store.destroy!
+ expect(another_store.deleted_at).not_to be_nil
+ end
+ end
+ end
+
+ describe '.available_locales' do
+ let!(:store_en) { create(:store, default_locale: 'en', supported_locales: 'en,fr') }
+ let!(:store_de) { create(:store, default_locale: 'de', supported_locales: 'de') }
+
+ it 'aggregates locales from all stores' do
+ expect(Spree::Store.available_locales).to contain_exactly('en', 'fr', 'de')
+ end
+ end
+end
diff --git a/backend/engines/multi_store/spec/spec_helper.rb b/backend/engines/multi_store/spec/spec_helper.rb
new file mode 100644
index 0000000000..dffa2bbed6
--- /dev/null
+++ b/backend/engines/multi_store/spec/spec_helper.rb
@@ -0,0 +1,64 @@
+# This file is copied to ~/spec when you run 'ruby script/generate rspec'
+# from the project root directory.
+ENV['RAILS_ENV'] ||= 'test'
+
+begin
+ require File.expand_path('../dummy/config/environment', __FILE__)
+rescue LoadError
+ puts 'Could not load dummy application. Please ensure you have run `bundle exec rake test_app`'
+end
+
+require 'rspec/rails'
+require 'database_cleaner/active_record'
+require 'ffaker'
+
+Dir['./spec/support/**/*.rb'].sort.each { |f| require f }
+
+require 'spree/testing_support/factories'
+require 'spree/testing_support/jobs'
+require 'spree/testing_support/store'
+require 'spree/testing_support/preferences'
+require 'spree/testing_support/authorization_helpers'
+require 'spree/testing_support/capybara_ext'
+require 'spree/testing_support/controller_requests'
+require 'spree/testing_support/url_helpers'
+require 'spree/testing_support/kernel'
+require 'spree/testing_support/rspec_retry_config'
+
+RSpec.configure do |config|
+ config.color = true
+ config.default_formatter = 'progress'
+ config.fail_fast = ENV['FAIL_FAST'] || false
+ config.infer_spec_type_from_file_location!
+ config.mock_with :rspec
+ config.raise_errors_for_deprecations!
+
+ # If you're not using ActiveRecord, or you'd prefer not to run each of your
+ # examples within a transaction, comment the following line or assign false
+ # instead of true.
+ config.use_transactional_fixtures = true
+
+ config.before(:suite) do
+ Spree::Events.disable!
+ # Clean out the database state before the tests run
+ DatabaseCleaner.clean_with(:truncation)
+ end
+
+ # Re-enable events for specs that need them
+ config.around(:each, events: true) do |example|
+ Spree::Events.enable { example.run }
+ end
+
+ config.before(:each) do
+ reset_spree_preferences
+ end
+
+ config.include FactoryBot::Syntax::Methods
+ config.include Spree::TestingSupport::Preferences
+ config.include Spree::TestingSupport::Kernel
+ config.include Spree::TestingSupport::UrlHelpers
+ config.include Spree::TestingSupport::ControllerRequests, type: :controller
+
+ config.order = :random
+ Kernel.srand config.seed
+end
diff --git a/backend/engines/multi_store/spree_multi_store.gemspec b/backend/engines/multi_store/spree_multi_store.gemspec
new file mode 100644
index 0000000000..068a73c409
--- /dev/null
+++ b/backend/engines/multi_store/spree_multi_store.gemspec
@@ -0,0 +1,29 @@
+# encoding: UTF-8
+
+require_relative '../core/lib/spree/core/version.rb'
+
+Gem::Specification.new do |s|
+ s.platform = Gem::Platform::RUBY
+ s.name = 'spree_multi_store'
+ s.version = Spree.version
+ s.authors = ['Vendo Connect Inc.', 'Vendo Sp. z o.o.']
+ s.email = '[email protected]'
+ s.summary = 'Multi-store switching and resolution for Spree Commerce'
+ s.description = 'Adds multi-store switching, custom domains, and store resolution to Spree Commerce'
+ s.homepage = 'https://spreecommerce.org'
+ s.licenses = ['AGPL-3.0-or-later']
+
+ s.metadata = {
+ "bug_tracker_uri" => "https://github.com/spree/spree/issues",
+ "changelog_uri" => "https://github.com/spree/spree/releases/tag/v#{s.version}",
+ "documentation_uri" => "https://docs.spreecommerce.org/",
+ "source_code_uri" => "https://github.com/spree/spree/tree/v#{s.version}",
+ }
+
+ s.required_ruby_version = '>= 3.2'
+
+ s.files = Dir["{app,config,lib}/**/*", "LICENSE.md", "Rakefile", "README.md"]
+ s.require_path = 'lib'
+
+ s.add_dependency 'spree', s.version
+end