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.

Directly tie Taxon/Category with Store and loose taxonomy dependency

Damian Legawiec3ba8bd60749f
diff --git a/spree/core/app/models/spree/category.rb b/spree/core/app/models/spree/category.rb
index b52472101b..4b7d25567c 100644
--- a/spree/core/app/models/spree/category.rb
+++ b/spree/core/app/models/spree/category.rb
@@ -1,6 +1,16 @@
module Spree
- # Public API name for Taxon. Will become the base class in 6.0
- # when spree_taxons table is renamed to spree_categories.
+ # Public API name for Taxon, used by the admin dashboard. Unlike a legacy
+ # +Spree::Taxon+, a Category does not require a +Spree::Taxonomy+ — it is
+ # owned directly via +store_id+, so a parentless category is a genuine
+ # top-level node. (Legacy taxonomy-backed taxons are unaffected.) Will become
+ # the base class in 6.0 when +Spree::Taxonomy+ is dropped and +spree_taxons+
+ # is renamed to +spree_categories+.
class Category < Taxon
+ default_scope { manual }
+
+ # A category is owned directly via +store_id+ and never needs a taxonomy.
+ def requires_taxonomy?
+ false
+ end
end
end
diff --git a/spree/core/app/models/spree/taxon.rb b/spree/core/app/models/spree/taxon.rb
index 5325dab6e1..4bc7b9ab40 100644
--- a/spree/core/app/models/spree/taxon.rb
+++ b/spree/core/app/models/spree/taxon.rb
@@ -37,7 +37,7 @@ module Spree
# Associations
#
belongs_to :taxonomy, class_name: 'Spree::Taxonomy', inverse_of: :taxons
- has_one :store, through: :taxonomy
+ belongs_to :store, class_name: 'Spree::Store', optional: true
has_many :classifications, -> { order(:position) }, dependent: :destroy_async, inverse_of: :taxon
has_many :products, through: :classifications
@@ -57,7 +57,8 @@ module Spree
# Validations
#
validates :name, presence: true, uniqueness: { scope: %i[parent_id taxonomy_id], case_sensitive: false }
- validates :taxonomy, presence: true
+ validates :taxonomy, presence: true, if: :requires_taxonomy?
+ validates :store, presence: true
validates :permalink, uniqueness: { case_sensitive: false, scope: %i[parent_id taxonomy_id] }
validates :hide_from_nav, inclusion: { in: [true, false] }
validate :check_for_root, on: :create
@@ -74,6 +75,7 @@ module Spree
#
before_validation :set_permalink, if: :name
before_validation :copy_taxonomy_from_parent
+ before_validation :set_store
before_save :set_pretty_name
after_save :touch_ancestors_and_taxonomy
after_update :sync_taxonomy_name
@@ -85,9 +87,17 @@ module Spree
#
# Scopes
#
- scope :for_store, ->(store) { joins(:taxonomy).where(spree_taxonomies: { store_id: store.id }) }
- scope :for_stores, ->(stores) { joins(:taxonomy).where(spree_taxonomies: { store_id: stores.ids }) }
+ # Prefer the direct store_id column; fall back to the taxonomy join for rows
+ # not yet backfilled (store_id IS NULL) so legacy behaviour is preserved.
+ scope :for_store, ->(store) { for_stores([store]) }
+ scope :for_stores, lambda { |stores|
+ store_ids = Array(stores).map(&:id)
+ taxonomy_ids = Spree::Taxonomy.where(store_id: store_ids).select(:id)
+ where(store_id: store_ids).or(where(store_id: nil, taxonomy_id: taxonomy_ids))
+ }
scope :for_taxonomy, lambda { |taxonomy_name|
+ Spree::Deprecation.warn('Spree::Taxon.for_taxonomy is deprecated and will be removed in Spree 6. Please use for_store instead.')
+
if Spree.use_translations?
joins(:taxonomy)
.join_translation_table(Taxonomy)
@@ -155,6 +165,18 @@ module Spree
!automatic?
end
+ # The owning store. Prefers the direct +store_id+; falls back to the
+ # taxonomy's store for legacy rows not yet backfilled.
+ def store
+ super || taxonomy&.store
+ end
+
+ # Whether a taxonomy is mandatory. Legacy +Spree::Taxon+ requires one;
+ # +Spree::Category+ overrides this to false (it is owned via +store_id+).
+ def requires_taxonomy?
+ true
+ end
+
def manual_sort_order?
sort_order == 'manual'
end
@@ -409,6 +431,16 @@ module Spree
self.taxonomy = parent.taxonomy if parent.present? && taxonomy.blank?
end
+ # Every taxon is store-owned. Resolve the store from the taxonomy, then the
+ # parent, finally the current store — so the direct column is always
+ # populated for new records. Guards on the raw +store_id+ column rather than
+ # +#store+, whose reader masks an unset column with the taxonomy fallback.
+ def set_store
+ return if store_id.present?
+
+ self.store = taxonomy&.store || parent&.store || Spree::Store.current
+ end
+
def regenerate_translations_pretty_name_and_permalink
translations.each(&:regenerate_pretty_name_and_permalink)
end
diff --git a/spree/core/db/migrate/20260626000001_add_store_id_to_spree_taxons.rb b/spree/core/db/migrate/20260626000001_add_store_id_to_spree_taxons.rb
new file mode 100644
index 0000000000..7cc0c16aa5
--- /dev/null
+++ b/spree/core/db/migrate/20260626000001_add_store_id_to_spree_taxons.rb
@@ -0,0 +1,11 @@
+class AddStoreIdToSpreeTaxons < ActiveRecord::Migration[7.2]
+ # NOTE: After running this migration, existing taxons have +store_id IS NULL+
+ # and keep resolving their store through their taxonomy (Taxon.for_store falls
+ # back to the taxonomy join). Run the backfill to populate +store_id+ directly,
+ # which is what taxonomy-less categories (Spree::Category) rely on:
+ #
+ # bundle exec rake spree:taxons:backfill_store_id
+ def change
+ add_reference :spree_taxons, :store, null: true, if_not_exists: true
+ end
+end
diff --git a/spree/core/lib/tasks/taxons.rake b/spree/core/lib/tasks/taxons.rake
index c89a41deef..d5f16c43f4 100644
--- a/spree/core/lib/tasks/taxons.rake
+++ b/spree/core/lib/tasks/taxons.rake
@@ -1,6 +1,17 @@
namespace :spree do
namespace :taxons do
desc 'Reset counter caches (children_count, classification_count) on taxons'
+ desc 'Backfill spree_taxons.store_id from each taxon\'s taxonomy'
+ task backfill_store_id: :environment do |_t, _args|
+ puts 'Backfilling taxon store_id from taxonomy...'
+
+ Spree::Taxon.unscoped.where(store_id: nil).in_batches(of: 1000) do |batch|
+ batch.joins(:taxonomy).update_all('store_id = spree_taxonomies.store_id')
+ print '.'
+ end
+
+ puts "\nDone!"
+ end
task reset_counter_caches: :environment do |_t, _args|
puts 'Resetting taxon counter caches...'
diff --git a/spree/core/spec/models/spree/category_spec.rb b/spree/core/spec/models/spree/category_spec.rb
new file mode 100644
index 0000000000..780d3aa4db
--- /dev/null
+++ b/spree/core/spec/models/spree/category_spec.rb
@@ -0,0 +1,90 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Category, type: :model do
+ let(:store) { @default_store }
+
+ describe 'creation without a taxonomy' do
+ it 'creates a parentless, store-owned top-level category' do
+ category = described_class.new(name: 'Kitchen', store: store)
+ expect(category).to be_valid
+ category.save!
+
+ expect(category.taxonomy).to be_nil
+ expect(category.parent).to be_nil
+ expect(category.store).to eq(store)
+ end
+
+ it 'does not require a taxonomy' do
+ category = described_class.new(name: 'Kitchen', store: store)
+ expect(category).to be_valid
+ expect(category.errors[:taxonomy]).to be_empty
+ end
+
+ it 'copies the store from its parent' do
+ parent = described_class.create!(name: 'Kitchen', store: store)
+ child = described_class.create!(name: 'Pots', parent: parent)
+
+ expect(child.store).to eq(store)
+ expect(child.taxonomy).to be_nil
+ end
+ end
+
+ describe '#requires_taxonomy?' do
+ it 'is false' do
+ expect(described_class.new.requires_taxonomy?).to be(false)
+ end
+ end
+
+ describe 'store auto-resolution (#set_store)' do
+ it 'falls back to the current store when none is given' do
+ Spree::Current.store = store
+ category = described_class.create!(name: 'Kitchen')
+
+ expect(category.store).to eq(store)
+ end
+
+ it 'prefers an explicit store over the current store' do
+ other = create(:store)
+ Spree::Current.store = other
+
+ category = described_class.create!(name: 'Kitchen', store: store)
+
+ expect(category.store).to eq(store)
+ end
+
+ it 'prefers the parent store over the current store' do
+ other = create(:store)
+ Spree::Current.store = other
+ parent = described_class.create!(name: 'Kitchen', store: store)
+
+ child = described_class.create!(name: 'Pots', parent: parent)
+
+ expect(child.store).to eq(store)
+ end
+ end
+
+ describe '.for_store / .for_stores' do
+ it 'finds taxonomy-less categories by store_id' do
+ category = described_class.create!(name: 'Kitchen', store: store)
+ expect(described_class.for_store(store)).to include(category)
+ end
+
+ it 'excludes categories owned by another store' do
+ other = create(:store)
+ mine = described_class.create!(name: 'Mine', store: store)
+ theirs = described_class.create!(name: 'Theirs', store: other)
+
+ result = described_class.for_store(store)
+ expect(result).to include(mine)
+ expect(result).not_to include(theirs)
+ end
+
+ it 'finds categories across multiple stores' do
+ other = create(:store)
+ mine = described_class.create!(name: 'Mine', store: store)
+ theirs = described_class.create!(name: 'Theirs', store: other)
+
+ expect(described_class.for_stores([store, other])).to include(mine, theirs)
+ end
+ end
+end
diff --git a/spree/core/spec/models/spree/taxon_spec.rb b/spree/core/spec/models/spree/taxon_spec.rb
index cbcfdf72fc..f837a0cc26 100644
--- a/spree/core/spec/models/spree/taxon_spec.rb
+++ b/spree/core/spec/models/spree/taxon_spec.rb
@@ -40,6 +40,52 @@ describe Spree::Taxon, type: :model do
end
end
+ context 'Store ownership' do
+ describe '#requires_taxonomy?' do
+ it 'is true for a legacy taxon' do
+ expect(described_class.new.requires_taxonomy?).to be(true)
+ end
+ end
+
+ describe 'taxonomy presence' do
+ it 'requires a taxonomy' do
+ legacy = described_class.new(name: 'Legacy')
+ expect(legacy).not_to be_valid
+ expect(legacy.errors[:taxonomy]).to be_present
+ end
+ end
+
+ describe '#set_store' do
+ it 'derives store from the taxonomy on create' do
+ taxon = create(:taxon, taxonomy: taxonomy, parent: taxonomy.root)
+ expect(taxon.store_id).to eq(taxonomy.store_id)
+ end
+ end
+
+ describe '#store' do
+ it 'falls back to the taxonomy store for not-yet-backfilled rows' do
+ taxon = create(:taxon, taxonomy: taxonomy, parent: taxonomy.root)
+ taxon.update_columns(store_id: nil)
+
+ expect(taxon.reload.store).to eq(taxonomy.store)
+ end
+ end
+
+ describe '.for_store' do
+ it 'finds a taxon via its direct store_id' do
+ taxon = create(:taxon, taxonomy: taxonomy, parent: taxonomy.root)
+ expect(described_class.for_store(store)).to include(taxon)
+ end
+
+ it 'finds a legacy taxon (store_id nil) via its taxonomy' do
+ taxon = create(:taxon, taxonomy: taxonomy, parent: taxonomy.root)
+ taxon.update_columns(store_id: nil)
+
+ expect(described_class.for_store(store)).to include(taxon)
+ end
+ end
+ end
+
context 'Scopes' do
describe '.for_taxonomy' do
let!(:categories_taxonomy) { store.taxonomies.find_by(name: 'Categories') || create(:taxonomy, name: 'Categories') }