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.

Make Taxonomy data-only and fix category uniqueness (#14409)

Damian Legawiec65a946576aa2
diff --git a/docs/plans/6.0-replace-taxons-with-categories.md b/docs/plans/6.0-replace-taxons-with-categories.md
index fb4582acfa..6524fd851d 100644
--- a/docs/plans/6.0-replace-taxons-with-categories.md
+++ b/docs/plans/6.0-replace-taxons-with-categories.md
@@ -1,6 +1,6 @@
# Replace Taxons with Categories & Add Collections
-**Status:** Shipped to `6-0-dev` (PR #14302, merged 2026-07-19). The Category read/write surface landed earlier (5.5–5.6); PR #14302 completed the 6.0 core: the `spree_taxons` → `spree_categories` table rename + inheritance flip (`Spree::Category < Spree.base_class`, `Spree::Taxon` retained as a deprecation alias), the full `Collection` stack (model + join + rules + DB/Meilisearch manual sort + API), the taxon→category/collection data-migration task, and de-ruling Category. **Remaining (all 6.1):** the channel-aware reimplementation of `CollectionRules::AvailableOn` — Phase 5, which ships now with an interim legacy-`available_on` `apply` and rides with `6.1-channels-catalogs-b2b.md`; and dropping `spree_taxon_rules` + `Spree::TaxonRule`/`TaxonRules::*` + the `Taxonomy` model (Phase 6). Nothing 6.0-blocking is left.
+**Status:** Shipped to `6-0-dev` (PR #14302, merged 2026-07-19). The Category read/write surface landed earlier (5.5–5.6); PR #14302 completed the 6.0 core: the `spree_taxons` → `spree_categories` table rename + inheritance flip (`Spree::Category < Spree.base_class`, `Spree::Taxon` retained as a deprecation alias), the full `Collection` stack (model + join + rules + DB/Meilisearch manual sort + API), the taxon→category/collection data-migration task, and de-ruling Category. **Remaining (all 6.1):** the channel-aware reimplementation of `CollectionRules::AvailableOn` — Phase 5, which ships now with an interim legacy-`available_on` `apply` and rides with `6.1-channels-catalogs-b2b.md`; and dropping `spree_taxon_rules` + `Spree::TaxonRule`/`TaxonRules::*` + the `Taxonomy` model and table (Phase 6). Taxonomy became **data-only** in 6.0 — Phase 5b removed every runtime path that created, touched or validated one, leaving only the upgrade task's reader and the pre-upgrade store-resolution fallbacks. Nothing 6.0-blocking is left.
**Target:** Spree 6.0 (Category surface landed in 5.5–5.6; 6.0 renames the table, splits out `Collection`, and drops `Taxonomy`)
**Depends on:** API v3 conventions, ProductType plan (6.0-product-types.md), Rich Text Descriptions plan (6.0-rich-text-descriptions.md), Admin SPA plan (6.0-admin-spa.md — dashboard UI), Channels/Publications plan (6.0-channels-catalogs-b2b.md — the `CollectionRules::AvailableOn` rule's channel-aware implementation depends on its `available_on` → `ProductPublication#published_at` migration; see Migration Phase 5)
**Author:** Damian + Claude
@@ -673,9 +673,40 @@ Gated on the channels plan finishing the `available_on` → `ProductPublication#
- **No data migration, no registry change** — `AvailableOn` is already registered (Phase 1) and its `CollectionRule` rows already carry the right `value`/`match_policy` from Phase 4; only `apply` changes.
- **Cross-plan ordering constraint (hard).** `CollectionRules::AvailableOn` is a reader of `spree_products.available_on`, which `6.0-channels-catalogs-b2b.md` Phase 3 ("6.0 cleanup") drops. This phase **must land before** that column drop — the channels plan's cleanup is blocked on this reimplementation.
+### Phase 5b: Taxonomy runtime removal (6.0) — **shipped**
+
+Taxonomy is **data-only** from 6.0. Nothing creates, touches, or manages a taxonomy at runtime; the model and table survive purely so the 5.6 → 6.0 upgrade task can read existing rows (and so an interrupted upgrade can roll back). What landed:
+
+- **`Imports::CreateCategoriesJob` builds store-owned categories** — a CSV import no longer creates a taxonomy per path root. Every `A -> B -> C` segment is a category, the first one top-level, which matches what the data migration produces.
+- **Taxonomy timestamp touching removed** — `Product#taxonomies` (the `has_many through:`) and the `taxonomy_ids` memoized method are gone; `TouchCategoriesJob` takes category ids only (second argument retained as ignored, so jobs enqueued before the deploy still deserialize).
+- **Taxonomy write-back callbacks removed** — `Taxonomy#set_root` / `#set_root_taxon_name` and `Category#sync_taxonomy_name`. Renaming a category no longer rewrites a taxonomy's name.
+- **`Category#requires_taxonomy?` and the taxonomy presence validation removed.** The method always returned `false`, so the taxonomy-scoped uniqueness branches were dead code — uniqueness is now unconditionally store-scoped, and `check_for_root` and `parent_belongs_to_same_taxonomy` are gone. `copy_taxonomy_from_parent` **stays**: a category created under an un-migrated parent still inherits its taxonomy so `#ensure_store` and `.for_store` can resolve a store pre-upgrade.
+
+ `parent_belongs_to_same_taxonomy` did carry one guarantee worth keeping, though — it stopped a category being nested under a parent from another store, as a side effect of taxonomies being store-owned. **`parent_belongs_to_same_store` replaces it directly**: a tree belongs to one store, and while the API scopes parents by store, imports, extensions and direct model calls do not. It reads `#store` rather than the raw column so legacy rows still resolving their store through a taxonomy compare correctly.
+- **Unique indexes re-scoped to the store** (`20260807150001_reindex_spree_categories_by_store`). The `(name, parent_id, taxonomy_id)` / `(permalink, parent_id, taxonomy_id)` indexes stop constraining anything once `spree:migrate_taxons_to_categories_and_collections` nulls `taxonomy_id` — SQL treats NULLs as distinct in unique indexes — and they never matched the store-scoped validation anyway.
+
+ The replacement is a single unique index on **`(permalink, store_id)`**, and **name uniqueness is dropped entirely**. The permalink is the category's full path (`men/clothing/shirts`, built by `generate_slug` from the parent chain), so it already encodes the hierarchy: scoping it by `parent_id` was always redundant, and two sibling categories that would clash are already caught because their paths are identical. Names need no constraint at all — the same name in two branches is legitimate, and the permalinks distinguish them.
+
+ This also sidesteps the NULL problem that a `parent_id`-scoped index creates. Top-level categories have `parent_id IS NULL`, and unique indexes treat NULLs as distinct, so any index including `parent_id` silently fails to constrain roots — the common case in 6.0. Leaving `parent_id` out of the index removes the issue instead of working around it, so no partial indexes, no generated columns, and no per-adapter branching: one plain index behaves identically on PostgreSQL, SQLite, MySQL and MariaDB.
+
+ **Ordering is the normal one — migrations first, then rake tasks.** That means the migration cannot depend on a rake task having cleaned the data for it; it resolves duplicate permalinks itself before adding the index (see below).
+
+- **Duplicate permalinks are resolved by the migration itself** (`Spree::CategoryPermalinkDeduplicator`, `spree/core/lib/spree/`). Store-scoped uniqueness surfaced a real data hazard: pre-6.0 permalinks were unique *per taxonomy*, so one store could legally hold the same permalink under two taxonomies.
+
+ This cannot be left to the upgrade rake task. `spree:taxons:backfill_store_id` already runs in the **5.5 -> 5.6** upgrade, so a 5.6 store reaches 6.0's `db:migrate` with `store_id` populated — a collision would fail index creation and block the deploy before any rake task could run. The migration therefore deduplicates before adding the index.
+
+ The deduplicator works on **raw SQL**, not `Spree::Category`: a migration cannot assume the model's callbacks, validations and default scopes match the schema on disk. It groups by the store a category belongs to **or resolves through its taxonomy**, so rows still awaiting the backfill are covered too — which is why `spree:migrate_taxons_to_categories_and_collections` still calls it (its own backfill is what gives those rows a store, and assignment would otherwise trip the index row by row). The first category of each group keeps its permalink; the rest get their taxonomy's name appended, then a numeric suffix. Rewriting a permalink **re-prefixes the whole subtree**, so a child of `catalog` follows its parent to `catalog-shop-b/kids` rather than keeping a path its parent no longer has. Idempotent — a second run is a no-op.
+- **`Taxonomy` deregistered** from `translatable_resources` and `metafields.enabled_resources`, and dropped from the three permission sets — a model being removed shouldn't carry a user-manageable custom-field/translation surface.
+- **`PermittedAttributes`**: `taxonomy_attributes` deleted (unused); `taxon_attributes` renamed to **`category_attributes`** with a deprecated `taxon_attributes` reader delegating to it (removed 6.1). The `taxonomy_id` and `taxon_rules_attributes` keys are gone from it.
+- **Factories split** — `:category` is now its own store-owned factory that creates no taxonomy; `:taxon` remains the legacy taxonomy-backed factory (it builds its own root, since the model no longer does) for the upgrade-path specs.
+
+**No NULL-`store_id` fallbacks.** Every category has a `store_id`: `spree:taxons:backfill_store_id` runs in the **5.5 -> 5.6** manifest, and `#ensure_store` — which landed in the same commit that made taxonomy optional — stamps one on create, so no row predates both. `#store`'s taxonomy fallback, the `.for_store` taxonomy join, the `[:taxonomy]` preload and the serializers' `c.taxonomy&.store_id` filter were all written for a state that cannot occur, and are gone. This is what lets `acts_as_nested_set` be scoped by `store` (see above).
+
+**What deliberately stays until 6.1:** the `Taxonomy` model + table (the upgrade task's source and rollback path), `Category#taxonomy`, and `copy_taxonomy_from_parent`, which keeps `taxonomy_id` consistent for that task. All three go with the column.
+
### Phase 6: Cleanup (6.1)
-- Remove all aliases and deprecation shims (incl. the `Spree::Taxon` alias and the `classification_count` `alias_attribute`)
+- Remove all aliases and deprecation shims (incl. the `Spree::Taxon` alias, the `classification_count` `alias_attribute`, and `PermittedAttributes.taxon_attributes`)
- Remove `taxonomy_id` column from categories
- Remove `spree_taxonomies` table + the `taxonomy_id` column + the `taxonomy_brands_name` locale key (brands out of scope — no replacement; `Product#brand_taxon`/`#brand_name` were already removed in 6.0/Phase 3)
- Remove `automatic`, `rules_match_policy`, `sort_order` columns from categories (moved to collections), plus the dead `hide_from_nav` column (its Ruby/SDK surface went in 6.0 — see Key Decisions → Category)
@@ -689,7 +720,7 @@ Gated on the channels plan finishing the `available_on` → `ProductPublication#
- **Use `categories` in all new API code.** The Store API already does this. Don't introduce new `taxon` references in API v3.
- **Use `Spree::Category` in new code.** It is the canonical class now; `Spree::Taxon` is a deprecation alias (removed in 6.1).
- **Don't add new TaxonRule subtypes.** `TaxonRule` is data-only from 6.0 (matching removed); new rule types go on `CollectionRule`.
-- **Don't add new Taxonomy-dependent features.** Taxonomy is going away.
+- **Don't add new Taxonomy-dependent features.** Taxonomy is data-only from 6.0 (see Phase 5b) and dropped in 6.1 — don't read it, write it, or register it anywhere. The only permitted references are the upgrade rake tasks and the pre-upgrade store-resolution fallbacks.
- **Keep the `ctg_` prefix.** Already in use, will carry over.
- **New "collection-like" (rule-based) features go on `Spree::Collection`** — the taxon automatic-match path was removed in Phase 2.
- **`ProductTypeCategory` join table** (from 6.0-product-types.md) should be named anticipating the Category rename — use `category_id` column, not `taxon_id`. (Already satisfied: `6.0-product-types.md` specifies `category_id`.)
diff --git a/packages/dashboard/e2e/global-setup.ts b/packages/dashboard/e2e/global-setup.ts
index 03633d3af7..6e2eeb0bc8 100644
--- a/packages/dashboard/e2e/global-setup.ts
+++ b/packages/dashboard/e2e/global-setup.ts
@@ -3,6 +3,7 @@ import { unlinkSync, writeFileSync } from 'node:fs'
import { resolve } from 'node:path'
import {
FIXTURE_BULK_CATEGORY,
+ FIXTURE_BULK_CATEGORY_PERMALINK,
FIXTURE_BULK_CHANNEL_CODE,
FIXTURE_BULK_CHANNEL_NAME,
FIXTURE_BULK_PRODUCT_A,
@@ -25,6 +26,7 @@ import {
FIXTURE_PROMO_CUSTOMER_GROUP,
FIXTURE_PROMO_PRODUCT,
FIXTURE_PROMO_TAXON,
+ FIXTURE_PROMO_TAXON_PERMALINK,
} from './helpers'
import { ASYNC_JOBS_INITIALIZER, CREDENTIALS_FILE, E2E_DIR, RAILS_PID_FILE } from './paths'
@@ -52,8 +54,9 @@ const BOOTSTRAP_RUBY = [
's.add_user(admin, Spree::Role.default_admin_role) unless s.role_users.exists?(user: admin)',
// Idempotent fixtures for promotion rule/action editor specs. Customer
// groups have no admin UI yet, so this is the only path to seed one.
- `taxonomy = s.taxonomies.find_or_create_by!(name: 'Categories')`,
- `category = taxonomy.taxons.where(name: '${FIXTURE_PROMO_TAXON}').first_or_create!(parent: taxonomy.root)`,
+ // Looked up by permalink, not name: category names are not unique, so a name
+ // lookup could match an unrelated (or child) category.
+ `category = s.categories.where(permalink: '${FIXTURE_PROMO_TAXON_PERMALINK}').first_or_create!(name: '${FIXTURE_PROMO_TAXON}', parent: nil)`,
`shipping_category = Spree::ShippingCategory.first || Spree::ShippingCategory.create!(name: 'Default')`,
`product = Spree::Product.where(name: '${FIXTURE_PROMO_PRODUCT}').first_or_create!(shipping_category: shipping_category, store: s, status: 'active')`,
`product.default_variant.set_price(s.default_currency, 19.99)`,
@@ -88,9 +91,9 @@ const BOOTSTRAP_RUBY = [
// Pre-list M/N on the bulk channel so the remove-from-channels test
// has something to undo.
`bulk_channel.add_products([Spree::Product.find_by(name: '${FIXTURE_BULK_PRODUCT_M}').id, Spree::Product.find_by(name: '${FIXTURE_BULK_PRODUCT_N}').id])`,
- // Category used by the bulk-add-to-categories test. Sits in the same
- // `Categories` taxonomy as the promo taxon above.
- `taxonomy.taxons.where(name: '${FIXTURE_BULK_CATEGORY}').first_or_create!(parent: taxonomy.root)`,
+ // Category used by the bulk-add-to-categories test, found by permalink for the
+ // same reason as the promo category above.
+ `s.categories.where(permalink: '${FIXTURE_BULK_CATEGORY_PERMALINK}').first_or_create!(name: '${FIXTURE_BULK_CATEGORY}', parent: nil)`,
`Spree.user_class.where(email: '${FIXTURE_PROMO_CUSTOMER_EMAIL}').first_or_create! { |u| u.password = 'customer123'; u.password_confirmation = 'customer123'; u.first_name = '${FIXTURE_PROMO_CUSTOMER_FIRST_NAME}'; u.last_name = '${FIXTURE_PROMO_CUSTOMER_LAST_NAME}' }`,
`s.customer_groups.where(name: '${FIXTURE_PROMO_CUSTOMER_GROUP}').first_or_create!`,
// Make the store multi-currency so the money-entry forms (store credit,
diff --git a/packages/dashboard/e2e/helpers.ts b/packages/dashboard/e2e/helpers.ts
index 0780ae5d30..bc1d541192 100644
--- a/packages/dashboard/e2e/helpers.ts
+++ b/packages/dashboard/e2e/helpers.ts
@@ -8,6 +8,9 @@ import { CREDENTIALS_FILE } from './paths'
* by name to make matching deterministic.
*/
export const FIXTURE_PROMO_TAXON = 'E2E Promo Category'
+// Permalinks are unique per store, names are not — the seed looks fixtures up by
+// permalink so it can never match an unrelated category.
+export const FIXTURE_PROMO_TAXON_PERMALINK = 'e2e-promo-category'
export const FIXTURE_PROMO_PRODUCT = 'E2E Promo Product'
// Active products used by products-bulk.spec.ts. Each test owns a disjoint
// pair so the serial suite doesn't cross-contaminate (status mutations on
@@ -31,9 +34,10 @@ export const FIXTURE_BULK_PRODUCT_K = 'E2E Bulk Product K'
export const FIXTURE_BULK_PRODUCT_L = 'E2E Bulk Product L'
export const FIXTURE_BULK_PRODUCT_M = 'E2E Bulk Product M'
export const FIXTURE_BULK_PRODUCT_N = 'E2E Bulk Product N'
-// Dedicated category seeded for the bulk-add-to-categories test; lives under
-// the `Categories` taxonomy alongside `FIXTURE_PROMO_TAXON`.
+// Dedicated category seeded for the bulk-add-to-categories test; a top-level
+// store category alongside `FIXTURE_PROMO_TAXON`.
export const FIXTURE_BULK_CATEGORY = 'E2E Bulk Category'
+export const FIXTURE_BULK_CATEGORY_PERMALINK = 'e2e-bulk-category'
// Second channel beyond the seeded default `online`. Used by the
// channels bulk-action and filter specs.
export const FIXTURE_BULK_CHANNEL_CODE = 'e2e-bulk'
diff --git a/packages/sdk/tests/integration/global-setup.ts b/packages/sdk/tests/integration/global-setup.ts
index a7e658a69a..dc4cee9bc4 100644
--- a/packages/sdk/tests/integration/global-setup.ts
+++ b/packages/sdk/tests/integration/global-setup.ts
@@ -30,7 +30,7 @@ const CREDENTIALS_RUBY = [
'sec = Spree::Api::Config[:jwt_secret_key].presence || Rails.application.credentials.jwt_secret_key || ENV["JWT_SECRET_KEY"] || Rails.application.secret_key_base',
'jwt = JWT.encode({ user_id: u.id, user_type: "customer", jti: SecureRandom.uuid, iss: "spree", aud: "store_api", exp: 1.hour.from_now.to_i }, sec, "HS256")',
'p = s.products.available.first',
- 'c = s.taxonomies.first&.root&.children&.first',
+ 'c = s.categories.first',
'port = ENV.fetch("PORT", 3010)',
'puts JSON.generate(base_url: "http://localhost:#{port}", publishable_key: k.token, jwt_token: jwt, user_email: u.email, user_password: "spree123", product_slug: p&.slug, product_id: p&.prefixed_id, variant_id: p&.default_variant&.prefixed_id, category_id: c&.prefixed_id, category_permalink: c&.permalink, country_iso: "US", store_name: s.name, bogus_payment_method_id: pm.prefixed_id, check_payment_method_id: check.prefixed_id)',
].join('; ')
diff --git a/spree/api/app/controllers/spree/api/v3/admin/categories_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/categories_controller.rb
index 020ecf964a..780d4c72ca 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/categories_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/categories_controller.rb
@@ -110,10 +110,11 @@ module Spree
end
# Positions the node at +index+ among the store's root categories.
- # awesome_nested_set's move_to_child_with_index(:root, ...) can't be
- # used here: its +roots+ are global (every parentless taxon across all
- # stores and taxonomies), whereas we order only this store's manual
- # roots (the +scope+). So position relative to the target root sibling.
+ # Deliberately not move_to_child_with_index(:root, ...): that indexes
+ # into awesome_nested_set's class-level +roots+, which stays global
+ # across stores even though the set is scoped per store. Positioning
+ # relative to the target sibling keeps the index meaning what the
+ # caller computed it from — this store's roots.
def move_to_root_at_index(node, index)
others = scope.where(parent_id: nil).where.not(id: node.id).order(:lft).to_a
target = others[index]
diff --git a/spree/api/app/controllers/spree/api/v3/store/categories_controller.rb b/spree/api/app/controllers/spree/api/v3/store/categories_controller.rb
index a90fd44068..ac7937a0b6 100644
--- a/spree/api/app/controllers/spree/api/v3/store/categories_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/store/categories_controller.rb
@@ -32,9 +32,6 @@ module Spree
end
end
- def collection_includes
- [:taxonomy]
- end
end
end
end
diff --git a/spree/api/app/serializers/spree/api/v3/admin/product_serializer.rb b/spree/api/app/serializers/spree/api/v3/admin/product_serializer.rb
index 0733f050a2..f4dfc2ce62 100644
--- a/spree/api/app/serializers/spree/api/v3/admin/product_serializer.rb
+++ b/spree/api/app/serializers/spree/api/v3/admin/product_serializer.rb
@@ -71,7 +71,7 @@ module Spree
many :categories,
proc { |categories, params|
store_id = params[:store].id
- categories.select { |c| c.store_id == store_id || c.taxonomy&.store_id == store_id }
+ categories.select { |c| c.store_id == store_id }
},
resource: proc { Spree.api.admin_category_serializer },
if: proc { expand?('categories') }
diff --git a/spree/api/app/serializers/spree/api/v3/product_serializer.rb b/spree/api/app/serializers/spree/api/v3/product_serializer.rb
index 0325ab2a50..831f1e9dcb 100644
--- a/spree/api/app/serializers/spree/api/v3/product_serializer.rb
+++ b/spree/api/app/serializers/spree/api/v3/product_serializer.rb
@@ -115,7 +115,7 @@ module Spree
many :categories,
proc { |categories, params|
store_id = params[:store].id
- categories.select { |c| c.store_id == store_id || c.taxonomy&.store_id == store_id }
+ categories.select { |c| c.store_id == store_id }
},
resource: proc { Spree.api.category_serializer },
if: proc { expand?('categories') }
diff --git a/spree/api/spec/controllers/concerns/spree/api/v3/nested_expand_spec.rb b/spree/api/spec/controllers/concerns/spree/api/v3/nested_expand_spec.rb
index c0d824a241..e0819f4b1a 100644
--- a/spree/api/spec/controllers/concerns/spree/api/v3/nested_expand_spec.rb
+++ b/spree/api/spec/controllers/concerns/spree/api/v3/nested_expand_spec.rb
@@ -74,12 +74,11 @@ RSpec.describe Spree::Api::V3::Store::ProductsController, type: :controller do
end
describe 'nested expand on aliased associations (key differs from name)' do
- let!(:taxonomy) { create(:taxonomy, store: store) }
- let!(:parent_taxon) { create(:taxon, taxonomy: taxonomy, parent: taxonomy.root) }
- let!(:child_taxon) { create(:taxon, taxonomy: taxonomy, parent: parent_taxon) }
+ let!(:parent_category) { create(:category) }
+ let!(:child_category) { create(:category, parent: parent_category) }
before do
- product.taxons << child_taxon
+ product.categories << child_category
end
it 'expands categories with nested ancestors' do
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/categories/products_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/categories/products_controller_spec.rb
index 5eb86a38b9..81767ce19b 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/categories/products_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/categories/products_controller_spec.rb
@@ -5,7 +5,7 @@ RSpec.describe Spree::Api::V3::Admin::Categories::ProductsController, type: :con
include_context 'API v3 Admin authenticated'
- let!(:category) { Spree::Category.create!(name: 'Clothing', store: store) }
+ let!(:category) { create(:category, name: 'Clothing', store: store) }
before { request.headers.merge!(headers) }
@@ -18,14 +18,14 @@ RSpec.describe Spree::Api::V3::Admin::Categories::ProductsController, type: :con
let!(:product_b) { create(:product, store: store) }
let!(:product_c) { create(:product, store: store) }
let!(:other_category_product) { create(:product, store: store) }
- let!(:other_category) { Spree::Category.create!(name: 'Other', store: store) }
+ let!(:other_category) { create(:category, name: 'Other', store: store) }
before do
- Spree::ProductCategory.create!(taxon: category, product: product_b, position: 1)
- Spree::ProductCategory.create!(taxon: category, product: product_a, position: 2)
- Spree::ProductCategory.create!(taxon: category, product: product_c, position: 3)
+ create(:product_category, category: category, product: product_b, position: 1)
+ create(:product_category, category: category, product: product_a, position: 2)
+ create(:product_category, category: category, product: product_c, position: 3)
# A product in a different category must not leak into this list.
- Spree::ProductCategory.create!(taxon: other_category, product: other_category_product, position: 1)
+ create(:product_category, category: other_category, product: other_category_product, position: 1)
end
it 'lists only the category products, ordered by classification position' do
@@ -40,7 +40,7 @@ RSpec.describe Spree::Api::V3::Admin::Categories::ProductsController, type: :con
end
it 'returns an empty list for a category with no products' do
- empty = Spree::Category.create!(name: 'Empty', store: store)
+ empty = create(:category, name: 'Empty', store: store)
get :index, params: { category_id: empty.prefixed_id }, as: :json
expect(response).to have_http_status(:ok)
@@ -48,25 +48,6 @@ RSpec.describe Spree::Api::V3::Admin::Categories::ProductsController, type: :con
end
end
- # Automatic (rule-based) rows are hidden by the categories CRUD controller and
- # migrate to Spree::Collection; membership can't be read or mutated here either.
- describe 'automatic (rule-based) categories' do
- let!(:automatic_category) { Spree::Category.create!(name: 'On Sale', store: store, automatic: true) }
- let!(:product) { create(:product, store: store) }
-
- it 'are not listable through the nested products endpoint' do
- get :index, params: { category_id: automatic_category.prefixed_id }, as: :json
- expect(response).to have_http_status(:not_found)
- end
-
- it 'do not accept product membership' do
- post :create, params: { category_id: automatic_category.prefixed_id, product_id: product.prefixed_id }, as: :json
-
- expect(response).to have_http_status(:not_found)
- expect(Spree::ProductCategory.where(category_id: automatic_category.id)).to be_empty
- end
- end
-
describe 'POST #create' do
let!(:product) { create(:product, store: store) }
@@ -88,7 +69,7 @@ RSpec.describe Spree::Api::V3::Admin::Categories::ProductsController, type: :con
describe 'DELETE #destroy' do
let!(:product) { create(:product, store: store) }
- before { Spree::ProductCategory.create!(taxon: category, product: product, position: 1) }
+ before { create(:product_category, category: category, product: product, position: 1) }
it 'removes the product from the category' do
delete :destroy, params: { category_id: category.prefixed_id, id: product.prefixed_id }, as: :json
@@ -111,9 +92,9 @@ RSpec.describe Spree::Api::V3::Admin::Categories::ProductsController, type: :con
let!(:third) { create(:product, store: store) }
before do
- Spree::ProductCategory.create!(taxon: category, product: first, position: 1)
- Spree::ProductCategory.create!(taxon: category, product: second, position: 2)
- Spree::ProductCategory.create!(taxon: category, product: third, position: 3)
+ create(:product_category, category: category, product: first, position: 1)
+ create(:product_category, category: category, product: second, position: 2)
+ create(:product_category, category: category, product: third, position: 3)
end
it 'moves a product to the requested index' do
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/categories_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/categories_controller_spec.rb
index 26fda8ed1b..5bec89779e 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/categories_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/categories_controller_spec.rb
@@ -6,10 +6,7 @@ RSpec.describe Spree::Api::V3::Admin::CategoriesController, type: :controller do
include_context 'API v3 Admin authenticated'
# A top-level category — parentless, store-owned, no taxonomy.
- let!(:category) { Spree::Category.create!(name: 'Clothing', store: store) }
- # A legacy taxonomy is still needed to host automatic (collection) taxons,
- # which the category API must exclude.
- let(:taxonomy) { create(:taxonomy, store: store) }
+ let!(:category) { create(:category, name: 'Clothing', store: store) }
before { request.headers.merge!(headers) }
@@ -18,15 +15,11 @@ RSpec.describe Spree::Api::V3::Admin::CategoriesController, type: :controller do
end
describe 'GET #index' do
- let!(:automatic) { create(:automatic_taxon, name: 'On Sale', taxonomy: taxonomy, parent: taxonomy.root) }
-
- it 'lists manual categories and excludes automatic (collection) taxons' do
+ it 'lists the store categories' do
get :index, params: {}, as: :json
expect(response).to have_http_status(:ok)
- ids = json_response['data'].map { |c| c['id'] }
- expect(ids).to include(category.prefixed_id)
- expect(ids).not_to include(automatic.prefixed_id)
+ expect(json_response['data'].map { |c| c['id'] }).to include(category.prefixed_id)
end
end
@@ -39,14 +32,14 @@ RSpec.describe Spree::Api::V3::Admin::CategoriesController, type: :controller do
end
it 'exposes the product count' do
- create_list(:product, 2).each { |p| p.taxons << category }
+ create_list(:product, 2).each { |p| p.categories << category }
get :show, params: { id: category.prefixed_id }, as: :json
expect(json_response['products_count']).to eq(2)
end
it 'rolls subcategory products up into the product count' do
- child = Spree::Category.create!(name: 'Shirts', parent: category)
+ child = create(:category, name: 'Shirts', parent: category)
create(:product).taxons << category
create(:product).taxons << child
@@ -54,12 +47,6 @@ RSpec.describe Spree::Api::V3::Admin::CategoriesController, type: :controller do
expect(json_response['products_count']).to eq(2) # 1 direct + 1 from child
end
-
- it 'does not expose an automatic (collection) taxon as a category' do
- automatic = create(:automatic_taxon, taxonomy: taxonomy, parent: taxonomy.root)
- get :show, params: { id: automatic.prefixed_id }, as: :json
- expect(response).to have_http_status(:not_found)
- end
end
describe 'POST #create' do
@@ -85,13 +72,6 @@ RSpec.describe Spree::Api::V3::Admin::CategoriesController, type: :controller do
post :create, params: { name: '', parent_id: category.prefixed_id }, as: :json
expect(response).to have_http_status(:unprocessable_content)
end
-
- it 'ignores collection-bound params (automatic stays false)' do
- post :create, params: { name: 'Sale-ish', parent_id: category.prefixed_id, automatic: true, sort_order: 'best_selling' }, as: :json
-
- expect(response).to have_http_status(:created)
- expect(created_category.automatic).to be(false)
- end
end
describe 'PATCH #update' do
@@ -102,16 +82,10 @@ RSpec.describe Spree::Api::V3::Admin::CategoriesController, type: :controller do
expect(category.reload.name).to eq('Apparel')
end
- it 'cannot target an automatic (collection) taxon' do
- automatic = create(:automatic_taxon, taxonomy: taxonomy, parent: taxonomy.root)
- patch :update, params: { id: automatic.prefixed_id, name: 'Hijacked' }, as: :json
- expect(response).to have_http_status(:not_found)
- end
-
it 'purges the image when image is set to null' do
- # Attach reliably persists on a factory-built taxon; the controller treats
+ # Attach reliably persists on a factory-built category; the controller treats
# it the same as a store-owned Category (both resolve through `scope`).
- imaged = create(:taxon, :with_header_image, taxonomy: taxonomy, parent: taxonomy.root)
+ imaged = create(:category, :with_header_image)
expect(imaged.reload.image).to be_attached
patch :update, params: { id: imaged.prefixed_id, image: nil }, as: :json
@@ -197,19 +171,13 @@ RSpec.describe Spree::Api::V3::Admin::CategoriesController, type: :controller do
expect(response).to have_http_status(:no_content)
expect(Spree::Category.find_by_prefix_id(category.prefixed_id)).to be_nil
end
-
- it 'cannot delete an automatic (collection) taxon' do
- automatic = create(:automatic_taxon, taxonomy: taxonomy, parent: taxonomy.root)
- delete :destroy, params: { id: automatic.prefixed_id }, as: :json
- expect(response).to have_http_status(:not_found)
- end
end
describe 'PATCH #reposition' do
# Top-level categories (parentless, store-owned) created in order.
- let!(:first) { Spree::Category.create!(name: 'A First', store: store) }
- let!(:second) { Spree::Category.create!(name: 'B Second', store: store) }
- let!(:third) { Spree::Category.create!(name: 'C Third', store: store) }
+ let!(:first) { create(:category, name: 'A First', store: store) }
+ let!(:second) { create(:category, name: 'B Second', store: store) }
+ let!(:third) { create(:category, name: 'C Third', store: store) }
# Helper: ordered ids of a parent's children (nested-set order).
def child_ids(parent)
@@ -226,7 +194,7 @@ RSpec.describe Spree::Api::V3::Admin::CategoriesController, type: :controller do
end
it 'inserts among existing children at the requested index' do
- existing = Spree::Category.create!(name: 'Existing', parent: first)
+ existing = create(:category, name: 'Existing', parent: first)
patch :reposition, params: { id: third.prefixed_id, new_parent_id: first.prefixed_id, new_position: 0 }, as: :json
@@ -235,8 +203,8 @@ RSpec.describe Spree::Api::V3::Admin::CategoriesController, type: :controller do
end
it 'reorders among an existing parent\'s children' do
- a = Spree::Category.create!(name: 'A', parent: first)
- b = Spree::Category.create!(name: 'B', parent: first)
+ a = create(:category, name: 'A', parent: first)
+ b = create(:category, name: 'B', parent: first)
patch :reposition, params: { id: b.prefixed_id, new_parent_id: first.prefixed_id, new_position: 0 }, as: :json
@@ -248,7 +216,7 @@ RSpec.describe Spree::Api::V3::Admin::CategoriesController, type: :controller do
# Regression: new_position past the child count dereferenced a nil
# sibling and returned 404. With second already a child, moving third in
# at an out-of-range index must succeed (append) rather than 404.
- Spree::Category.create!(name: 'Existing child', parent: first)
+ create(:category, name: 'Existing child', parent: first)
patch :reposition, params: { id: third.prefixed_id, new_parent_id: first.prefixed_id, new_position: 999 }, as: :json
@@ -257,7 +225,7 @@ RSpec.describe Spree::Api::V3::Admin::CategoriesController, type: :controller do
end
it 'promotes a nested category to the top level when no parent is given' do
- nested = Spree::Category.create!(name: 'Nested', parent: first)
+ nested = create(:category, name: 'Nested', parent: first)
patch :reposition, params: { id: nested.prefixed_id, new_position: 0 }, as: :json
@@ -266,20 +234,12 @@ RSpec.describe Spree::Api::V3::Admin::CategoriesController, type: :controller do
end
it 'returns 422 for an impossible move (into its own descendant)' do
- child = Spree::Category.create!(name: 'Child', parent: first)
+ child = create(:category, name: 'Child', parent: first)
patch :reposition, params: { id: first.prefixed_id, new_parent_id: child.prefixed_id, new_position: 0 }, as: :json
expect(response).to have_http_status(:unprocessable_content)
end
-
- it 'cannot reposition an automatic (collection) taxon' do
- automatic = create(:automatic_taxon, taxonomy: taxonomy, parent: taxonomy.root)
-
- patch :reposition, params: { id: automatic.prefixed_id, new_position: 0 }, as: :json
-
- expect(response).to have_http_status(:not_found)
- end
end
context 'reordering top-level categories (no parent change)' do
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/custom_fields_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/custom_fields_controller_spec.rb
index ce3e5c9b6f..b290b8b6e7 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/custom_fields_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/custom_fields_controller_spec.rb
@@ -288,11 +288,11 @@ RSpec.describe Spree::Api::V3::Admin::CustomFieldsController, type: :controller
end
context 'with a category parent' do
- # Regression: the parent map keyed taxons by `taxon`, but the routes
+ # Regression: the parent map keyed taxons by `category`, but the routes
# expose `category_id` — category custom-field endpoints 404'd for
# every caller until the `category` alias was added.
let(:granted_scope) { 'write_categories' }
- let(:category) { create(:taxon) }
+ let(:category) { create(:category) }
let(:category_definition) do
create(:metafield_definition, :short_text_field, resource_type: 'Spree::Category')
end
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/products_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/products_controller_spec.rb
index 06b05cabcb..b90b7919a3 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/products_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/products_controller_spec.rb
@@ -316,9 +316,8 @@ RSpec.describe Spree::Api::V3::Admin::ProductsController, type: :controller do
before { request.headers.merge!(headers) }
let(:tax_category) { create(:tax_category) }
- let(:taxonomy) { create(:taxonomy, store: store) }
- let(:category1) { create(:taxon, taxonomy: taxonomy) }
- let(:category2) { create(:taxon, taxonomy: taxonomy) }
+ let(:category1) { create(:category) }
+ let(:category2) { create(:category) }
it 'creates a minimal product' do
expect {
@@ -712,10 +711,8 @@ RSpec.describe Spree::Api::V3::Admin::ProductsController, type: :controller do
describe 'PATCH #update' do
before { request.headers.merge!(headers) }
-
- let(:taxonomy) { create(:taxonomy, store: store) }
- let(:category1) { create(:taxon, taxonomy: taxonomy) }
- let(:category2) { create(:taxon, taxonomy: taxonomy) }
+ let(:category1) { create(:category) }
+ let(:category2) { create(:category) }
let(:tax_category) { create(:tax_category) }
it 'updates basic product attributes' do
@@ -872,42 +869,42 @@ RSpec.describe Spree::Api::V3::Admin::ProductsController, type: :controller do
}, as: :json
expect(response).to have_http_status(:ok)
- expect(product.reload.taxons).to match_array([category1, category2])
+ expect(product.reload.categories).to match_array([category1, category2])
end
it 'replaces existing categories' do
- product.taxons << category1
+ product.categories << category1
patch :update, params: {
id: product.prefixed_id,
category_ids: [category2.prefixed_id]
}, as: :json
expect(response).to have_http_status(:ok)
- expect(product.reload.taxons.where(taxonomy: taxonomy)).to eq([category2])
+ expect(product.reload.categories).to eq([category2])
end
it 'clears categories when empty array' do
- product.taxons << category1
+ product.categories << category1
patch :update, params: {
id: product.prefixed_id,
category_ids: []
}, as: :json
expect(response).to have_http_status(:ok)
- expect(product.reload.taxons.where(taxonomy: taxonomy)).to be_empty
+ expect(product.reload.categories).to be_empty
end
- it "ignores a category that belongs to another store's taxonomy" do
- foreign_taxon = create(:taxon, taxonomy: create(:taxonomy, store: create(:store)))
+ it 'ignores a category that belongs to another store' do
+ foreign_category = create(:category, store: create(:store))
patch :update, params: {
id: product.prefixed_id,
- category_ids: [category1.prefixed_id, foreign_taxon.prefixed_id]
+ category_ids: [category1.prefixed_id, foreign_category.prefixed_id]
}, as: :json
expect(response).to have_http_status(:ok)
- expect(product.reload.taxons).to include(category1)
- expect(product.reload.taxons).not_to include(foreign_taxon)
+ expect(product.reload.categories).to include(category1)
+ expect(product.reload.categories).not_to include(foreign_category)
end
end
@@ -1510,9 +1507,8 @@ RSpec.describe Spree::Api::V3::Admin::ProductsController, type: :controller do
end
describe 'POST #bulk_add_to_categories' do
- let(:taxonomy) { create(:taxonomy, store: store) }
- let(:category) { create(:taxon, taxonomy: taxonomy) }
- let(:other_category) { create(:taxon, taxonomy: taxonomy) }
+ let(:category) { create(:category) }
+ let(:other_category) { create(:category) }
let!(:second_product) { create(:product) }
before { request.headers.merge!(headers) }
@@ -1525,14 +1521,14 @@ RSpec.describe Spree::Api::V3::Admin::ProductsController, type: :controller do
expect(response).to have_http_status(:ok)
expect(json_response).to eq('product_count' => 2, 'category_count' => 2)
- expect(product.reload.taxons).to include(category, other_category)
- expect(second_product.reload.taxons).to include(category, other_category)
+ expect(product.reload.categories).to include(category, other_category)
+ expect(second_product.reload.categories).to include(category, other_category)
end
it 'attaches to a taxonomy-less, store-owned category' do
# Regression: categories were scoped via the through-taxonomy association,
# which misses store-owned categories that have no taxonomy.
- store_category = Spree::Category.create!(name: 'Store Owned', store: store)
+ store_category = create(:category, name: 'Store Owned', store: store)
post :bulk_add_to_categories, params: {
ids: [product.prefixed_id],
@@ -1541,12 +1537,11 @@ RSpec.describe Spree::Api::V3::Admin::ProductsController, type: :controller do
expect(response).to have_http_status(:ok)
expect(json_response['category_count']).to eq(1)
- expect(product.reload.taxons).to include(store_category)
+ expect(product.reload.categories).to include(store_category)
end
it 'silently ignores categories from other stores' do
- foreign_taxonomy = create(:taxonomy, store: create(:store))
- foreign_category = create(:taxon, taxonomy: foreign_taxonomy)
+ foreign_category = create(:category, store: create(:store))
post :bulk_add_to_categories, params: {
ids: [product.prefixed_id],
@@ -1555,8 +1550,8 @@ RSpec.describe Spree::Api::V3::Admin::ProductsController, type: :controller do
expect(response).to have_http_status(:ok)
expect(json_response['category_count']).to eq(1)
- expect(product.reload.taxons).to include(category)
- expect(product.reload.taxons).not_to include(foreign_category)
+ expect(product.reload.categories).to include(category)
+ expect(product.reload.categories).not_to include(foreign_category)
end
it 'silently drops products from other stores' do
@@ -1570,17 +1565,17 @@ RSpec.describe Spree::Api::V3::Admin::ProductsController, type: :controller do
expect(response).to have_http_status(:ok)
expect(json_response['product_count']).to eq(1)
- expect(other_store_product.reload.taxons).to be_empty
+ expect(other_store_product.reload.categories).to be_empty
end
it 'is idempotent — re-adding existing categories is a no-op' do
- product.taxons << category
+ product.categories << category
expect do
post :bulk_add_to_categories, params: {
ids: [product.prefixed_id], category_ids: [category.prefixed_id]
}, as: :json
- end.not_to change { product.reload.taxons.count }
+ end.not_to change { product.reload.categories.count }
expect(response).to have_http_status(:ok)
end
@@ -1592,7 +1587,7 @@ RSpec.describe Spree::Api::V3::Admin::ProductsController, type: :controller do
expect(response).to have_http_status(:ok)
expect(json_response).to eq('product_count' => 1, 'category_count' => 0)
- expect(product.reload.taxons).to be_empty
+ expect(product.reload.categories).to be_empty
end
it 'is a no-op when ids is empty' do
@@ -1648,15 +1643,14 @@ RSpec.describe Spree::Api::V3::Admin::ProductsController, type: :controller do
end
describe 'POST #bulk_remove_from_categories' do
- let(:taxonomy) { create(:taxonomy, store: store) }
- let(:category) { create(:taxon, taxonomy: taxonomy) }
- let(:other_category) { create(:taxon, taxonomy: taxonomy) }
+ let(:category) { create(:category) }
+ let(:other_category) { create(:category) }
let!(:second_product) { create(:product) }
before do
request.headers.merge!(headers)
- product.taxons << [category, other_category]
- second_product.taxons << category
+ product.categories << [category, other_category]
+ second_product.categories << category
end
it 'detaches every product from every category' do
@@ -1667,9 +1661,9 @@ RSpec.describe Spree::Api::V3::Admin::ProductsController, type: :controller do
expect(response).to have_http_status(:ok)
expect(json_response).to eq('product_count' => 2, 'category_count' => 1)
- expect(product.reload.taxons).not_to include(category)
- expect(product.reload.taxons).to include(other_category)
- expect(second_product.reload.taxons).not_to include(category)
+ expect(product.reload.categories).not_to include(category)
+ expect(product.reload.categories).to include(other_category)
+ expect(second_product.reload.categories).not_to include(category)
end
it 'is a no-op for products not in the category' do
@@ -1680,7 +1674,7 @@ RSpec.describe Spree::Api::V3::Admin::ProductsController, type: :controller do
}, as: :json
expect(response).to have_http_status(:ok)
- expect(stray.reload.taxons).to be_empty
+ expect(stray.reload.categories).to be_empty
end
it 'touches the products' do
@@ -1718,8 +1712,8 @@ RSpec.describe Spree::Api::V3::Admin::ProductsController, type: :controller do
it 'reassigns the positions of surviving products on the category list' do
survivor = create(:product)
latecomer = create(:product)
- survivor.taxons << category
- latecomer.taxons << category
+ survivor.categories << category
+ latecomer.categories << category
post :bulk_remove_from_categories, params: {
ids: [product.prefixed_id, second_product.prefixed_id],
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/promotions_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/promotions_controller_spec.rb
index 9c0adc97b7..91de6763a0 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/promotions_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/promotions_controller_spec.rb
@@ -121,14 +121,13 @@ RSpec.describe Spree::Api::V3::Admin::PromotionsController, type: :controller do
end
it 'creates a Taxon rule with prefixed category_ids' do
- taxonomy = create(:taxonomy, store: store)
- taxon = create(:taxon, taxonomy: taxonomy)
+ category = create(:category)
post :create,
params: base_params.merge(
rules: [{
type: 'category',
- category_ids: [taxon.prefixed_id]
+ category_ids: [category.prefixed_id]
}]
),
as: :json
@@ -136,7 +135,7 @@ RSpec.describe Spree::Api::V3::Admin::PromotionsController, type: :controller do
expect(response).to have_http_status(:created)
rule = Spree::Promotion.find_by_prefix_id(json_response['id']).rules.first
expect(rule).to be_a(Spree::Promotion::Rules::Category)
- expect(rule.taxon_ids).to eq([taxon.id])
+ expect(rule.category_ids).to eq([category.id])
end
it 'creates multiple rules of different types in one request' do
@@ -260,19 +259,18 @@ RSpec.describe Spree::Api::V3::Admin::PromotionsController, type: :controller do
end
it 'resolves shorthand for rules with overridden api_type' do
- taxonomy = create(:taxonomy, store: store)
- taxon = create(:taxon, taxonomy: taxonomy)
+ category = create(:category)
post :create,
params: base_params.merge(
- rules: [{ type: 'category', category_ids: [taxon.prefixed_id] }]
+ rules: [{ type: 'category', category_ids: [category.prefixed_id] }]
),
as: :json
expect(response).to have_http_status(:created)
rule = Spree::Promotion.find_by_prefix_id(json_response['id']).rules.first
expect(rule).to be_a(Spree::Promotion::Rules::Category)
- expect(rule.taxon_ids).to eq([taxon.id])
+ expect(rule.category_ids).to eq([category.id])
end
it 'resolves shorthand for actions and calculators' do
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/translations_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/translations_controller_spec.rb
index 0948ed7d1d..25a9a6539d 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/translations_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/translations_controller_spec.rb
@@ -57,9 +57,11 @@ RSpec.describe Spree::Api::V3::Admin::TranslationsController, type: :controller
end
end
- context 'when the parent is a category (Spree::Category < Spree::Category)' do
+ context 'when the parent is a category' do
+ # Taxonomy-backed on purpose: the last example covers the pre-upgrade
+ # fallback, where store_id is NULL and the store resolves via the taxonomy.
let!(:taxonomy) { create(:taxonomy, store: store) }
- let!(:category) { create(:taxon, name: 'Clothing', taxonomy: taxonomy, parent: taxonomy.root) }
+ let!(:category) { create(:category, name: 'Clothing', taxonomy: taxonomy, parent: taxonomy.root) }
it 'returns the translation matrix for the category' do
get :index, params: { category_id: category.prefixed_id }, as: :json
@@ -74,16 +76,6 @@ RSpec.describe Spree::Api::V3::Admin::TranslationsController, type: :controller
get :index, params: { category_id: taxonomy.root.prefixed_id }, as: :json
expect(response).to have_http_status(:ok)
end
-
- it 'resolves a legacy taxonomy-backed taxon whose store_id was never backfilled' do
- # Regression: for_store must fall back to the taxonomy join for rows with
- # store_id IS NULL, otherwise the parent lookup 404s.
- category.update_columns(store_id: nil)
-
- get :index, params: { category_id: category.prefixed_id }, as: :json
-
- expect(response).to have_http_status(:ok)
- end
end
context 'when the parent param resolves to nothing translatable' do
diff --git a/spree/api/spec/controllers/spree/api/v3/params_normalizer_spec.rb b/spree/api/spec/controllers/spree/api/v3/params_normalizer_spec.rb
index 15d5235f41..fd116f3cf9 100644
--- a/spree/api/spec/controllers/spree/api/v3/params_normalizer_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/params_normalizer_spec.rb
@@ -50,12 +50,12 @@ RSpec.describe Spree::Api::V3::ParamsNormalizer do
end
it 'decodes _ids array params' do
- taxon1 = create(:taxon)
- taxon2 = create(:taxon)
- hash = { 'taxon_ids' => [taxon1.prefixed_id, taxon2.prefixed_id] }.with_indifferent_access
+ category1 = create(:category)
+ category2 = create(:category)
+ hash = { 'category_ids' => [category1.prefixed_id, category2.prefixed_id] }.with_indifferent_access
result = controller.resolve_prefixed_ids(hash)
- expect(result['taxon_ids']).to eq([taxon1.id, taxon2.id])
+ expect(result['category_ids']).to eq([category1.id, category2.id])
end
it 'recurses into nested hashes' do
diff --git a/spree/api/spec/controllers/spree/api/v3/store/categories_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/store/categories_controller_spec.rb
index fd33e1bb45..3e85a59df9 100644
--- a/spree/api/spec/controllers/spree/api/v3/store/categories_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/store/categories_controller_spec.rb
@@ -4,14 +4,11 @@ RSpec.describe Spree::Api::V3::Store::CategoriesController, type: :controller do
render_views
include_context 'API v3 Store'
-
- let(:taxonomy) { create(:taxonomy, store: store) }
- let!(:taxon) { create(:taxon, taxonomy: taxonomy) }
- let!(:child_taxon) { create(:taxon, taxonomy: taxonomy, parent: taxon) }
- let!(:grandchild_taxon) { create(:taxon, taxonomy: taxonomy, parent: child_taxon) }
+ let!(:category) { create(:category) }
+ let!(:child_category) { create(:category, parent: category) }
+ let!(:grandchild_category) { create(:category, parent: child_category) }
let!(:other_store) { create(:store) }
- let!(:other_taxonomy) { create(:taxonomy, store: other_store) }
- let!(:other_store_taxon) { create(:taxon, taxonomy: other_taxonomy) }
+ let!(:other_store_category) { create(:category, store: other_store) }
before do
request.headers['X-Spree-Api-Key'] = api_key.token
@@ -26,8 +23,8 @@ RSpec.describe Spree::Api::V3::Store::CategoriesController, type: :controller do
get :index
expect(response).to have_http_status(:ok)
- expect(json_response['data'].pluck('id')).to include(taxon.prefixed_id, child_taxon.prefixed_id)
- expect(json_response['data'].pluck('id')).not_to include(other_store_taxon.prefixed_id)
+ expect(json_response['data'].pluck('id')).to include(category.prefixed_id, child_category.prefixed_id)
+ expect(json_response['data'].pluck('id')).not_to include(other_store_category.prefixed_id)
end
it 'returns pagination metadata' do
@@ -39,42 +36,42 @@ RSpec.describe Spree::Api::V3::Store::CategoriesController, type: :controller do
it 'returns category attributes' do
get :index
- category_data = json_response['data'].find { |t| t['id'] == taxon.prefixed_id }
+ category_data = json_response['data'].find { |t| t['id'] == category.prefixed_id }
expect(category_data).to include('name', 'permalink', 'position', 'depth')
expect(category_data).to include('parent_id', 'children_count')
expect(category_data).not_to include('lft', 'rgt')
end
context 'with images' do
- let!(:taxon_with_image) { create(:taxon, :with_header_image, taxonomy: taxonomy) }
+ let!(:category_with_image) { create(:category, :with_header_image, store: store) }
it 'returns image URLs' do
get :index
- category_data = json_response['data'].find { |t| t['id'] == taxon_with_image.prefixed_id }
+ category_data = json_response['data'].find { |t| t['id'] == category_with_image.prefixed_id }
expect(category_data['image_url']).to be_present
end
end
context 'filtering' do
it 'filters by depth' do
- get :index, params: { q: { depth_eq: grandchild_taxon.depth } }
+ get :index, params: { q: { depth_eq: grandchild_category.depth } }
ids = json_response['data'].pluck('id')
- expect(ids).to include(grandchild_taxon.prefixed_id)
+ expect(ids).to include(grandchild_category.prefixed_id)
end
it 'filters by parent_id' do
- get :index, params: { q: { parent_id_eq: child_taxon.id } }
+ get :index, params: { q: { parent_id_eq: child_category.id } }
ids = json_response['data'].pluck('id')
- expect(ids).to include(grandchild_taxon.prefixed_id)
+ expect(ids).to include(grandchild_category.prefixed_id)
end
end
context 'sorting' do
- let!(:taxon_b) { create(:taxon, taxonomy: taxonomy, name: 'Bags') }
- let!(:taxon_z) { create(:taxon, taxonomy: taxonomy, name: 'Zippers') }
+ let!(:taxon_b) { create(:category, name: 'Bags') }
+ let!(:taxon_z) { create(:category, name: 'Zippers') }
it 'sorts by name ascending' do
get :index, params: { sort: 'name' }
@@ -111,67 +108,65 @@ RSpec.describe Spree::Api::V3::Store::CategoriesController, type: :controller do
describe 'GET #show' do
context 'finding by permalink' do
it 'returns the category by permalink' do
- get :show, params: { id: taxon.permalink }
+ get :show, params: { id: category.permalink }
expect(response).to have_http_status(:ok)
- expect(json_response['id']).to eq(taxon.prefixed_id)
- expect(json_response['name']).to eq(taxon.name)
- expect(json_response['permalink']).to eq(taxon.permalink)
+ expect(json_response['id']).to eq(category.prefixed_id)
+ expect(json_response['name']).to eq(category.name)
+ expect(json_response['permalink']).to eq(category.permalink)
end
it 'returns nested category by full permalink path' do
- get :show, params: { id: child_taxon.permalink }
+ get :show, params: { id: child_category.permalink }
expect(response).to have_http_status(:ok)
- expect(json_response['id']).to eq(child_taxon.prefixed_id)
- expect(json_response['name']).to eq(child_taxon.name)
+ expect(json_response['id']).to eq(child_category.prefixed_id)
+ expect(json_response['name']).to eq(child_category.name)
end
end
context 'finding by prefix_id' do
it 'returns the category by prefix_id' do
- get :show, params: { id: taxon.prefixed_id }
+ get :show, params: { id: category.prefixed_id }
expect(response).to have_http_status(:ok)
- expect(json_response['id']).to eq(taxon.prefixed_id)
- expect(json_response['name']).to eq(taxon.name)
- expect(json_response['permalink']).to eq(taxon.permalink)
+ expect(json_response['id']).to eq(category.prefixed_id)
+ expect(json_response['name']).to eq(category.name)
+ expect(json_response['permalink']).to eq(category.permalink)
end
end
it 'returns category attributes' do
- get :show, params: { id: taxon.permalink }
+ get :show, params: { id: category.permalink }
expect(json_response).to include('id', 'name', 'permalink')
end
it 'includes parent information for child category' do
- get :show, params: { id: child_taxon.permalink }
+ get :show, params: { id: child_category.permalink }
expect(response).to have_http_status(:ok)
- expect(json_response['parent_id']).to eq(taxon.prefixed_id)
+ expect(json_response['parent_id']).to eq(category.prefixed_id)
end
it 'does not include lft and rgt in store API' do
- get :show, params: { id: taxon.prefixed_id }
+ get :show, params: { id: category.prefixed_id }
expect(json_response).not_to include('lft', 'rgt')
end
context 'with expand=ancestors' do
it 'returns ancestors for breadcrumbs' do
- get :show, params: { id: grandchild_taxon.prefixed_id, expand: 'ancestors' }
+ get :show, params: { id: grandchild_category.prefixed_id, expand: 'ancestors' }
expect(response).to have_http_status(:ok)
expect(json_response['ancestors']).to be_an(Array)
ancestor_ids = json_response['ancestors'].pluck('id')
- expect(ancestor_ids).to include(taxon.prefixed_id, child_taxon.prefixed_id)
+ expect(ancestor_ids).to include(category.prefixed_id, child_category.prefixed_id)
end
- it 'returns empty ancestors for root category' do
- root_taxon = taxonomy.root
-
- get :show, params: { id: root_taxon.prefixed_id, expand: 'ancestors' }
+ it 'returns empty ancestors for a top-level category' do
+ get :show, params: { id: category.prefixed_id, expand: 'ancestors' }
expect(response).to have_http_status(:ok)
expect(json_response['ancestors']).to eq([])
@@ -180,17 +175,17 @@ RSpec.describe Spree::Api::V3::Store::CategoriesController, type: :controller do
context 'with expand=children' do
it 'returns children' do
- get :show, params: { id: taxon.prefixed_id, expand: 'children' }
+ get :show, params: { id: category.prefixed_id, expand: 'children' }
expect(response).to have_http_status(:ok)
expect(json_response['children']).to be_an(Array)
- expect(json_response['children'].pluck('id')).to include(child_taxon.prefixed_id)
+ expect(json_response['children'].pluck('id')).to include(child_category.prefixed_id)
end
end
context 'with translations', if: Spree::Category.include?(Spree::TranslatableResource) do
let!(:translated_taxon) do
- create(:taxon, taxonomy: taxonomy, name: 'Clothing', permalink: 'clothing').tap do |t|
+ create(:category, name: 'Clothing', permalink: 'clothing').tap do |t|
Mobility.with_locale(:fr) do
t.name = 'Vêtements'
t.permalink = 'vetements'
@@ -240,7 +235,7 @@ RSpec.describe Spree::Api::V3::Store::CategoriesController, type: :controller do
context 'locale fallback' do
let!(:english_only_taxon) do
- create(:taxon, taxonomy: taxonomy, name: 'Electronics')
+ create(:category, name: 'Electronics')
end
it 'falls back to default locale when category has no translation in requested locale' do
@@ -273,7 +268,7 @@ RSpec.describe Spree::Api::V3::Store::CategoriesController, type: :controller do
end
it 'returns not found for category from another store' do
- get :show, params: { id: other_store_taxon.permalink }
+ get :show, params: { id: other_store_category.permalink }
expect(response).to have_http_status(:not_found)
expect(json_response['error']['code']).to eq('record_not_found')
@@ -291,7 +286,7 @@ RSpec.describe Spree::Api::V3::Store::CategoriesController, type: :controller do
before { request.headers['X-Spree-Api-Key'] = nil }
it 'returns unauthorized' do
- get :show, params: { id: taxon.permalink }
+ get :show, params: { id: category.permalink }
expect(response).to have_http_status(:unauthorized)
expect(json_response['error']['code']).to eq('invalid_token')
@@ -309,7 +304,7 @@ RSpec.describe Spree::Api::V3::Store::CategoriesController, type: :controller do
it 'sets locale from header' do
request.headers['x-spree-locale'] = 'fr'
- get :show, params: { id: taxon.prefixed_id }
+ get :show, params: { id: category.prefixed_id }
expect(response).to have_http_status(:ok)
expect(I18n.locale).to eq(:fr)
@@ -317,7 +312,7 @@ RSpec.describe Spree::Api::V3::Store::CategoriesController, type: :controller do
it 'falls back to default locale for unsupported locale' do
request.headers['x-spree-locale'] = 'de'
- get :show, params: { id: taxon.prefixed_id }
+ get :show, params: { id: category.prefixed_id }
expect(response).to have_http_status(:ok)
expect(I18n.locale).to eq(:en)
@@ -332,7 +327,7 @@ RSpec.describe Spree::Api::V3::Store::CategoriesController, type: :controller do
it 'sets currency from header' do
request.headers['x-spree-currency'] = 'EUR'
- get :show, params: { id: taxon.permalink }
+ get :show, params: { id: category.permalink }
expect(response).to have_http_status(:ok)
expect(controller.send(:current_currency)).to eq('EUR')
@@ -340,7 +335,7 @@ RSpec.describe Spree::Api::V3::Store::CategoriesController, type: :controller do
it 'falls back to default currency for unsupported currency' do
request.headers['x-spree-currency'] = 'GBP'
- get :show, params: { id: taxon.permalink }
+ get :show, params: { id: category.permalink }
expect(response).to have_http_status(:ok)
expect(controller.send(:current_currency)).to eq('USD')
diff --git a/spree/api/spec/controllers/spree/api/v3/store/products/filters_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/store/products/filters_controller_spec.rb
index e31ab4b78a..1edf4be67d 100644
--- a/spree/api/spec/controllers/spree/api/v3/store/products/filters_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/store/products/filters_controller_spec.rb
@@ -5,10 +5,9 @@ RSpec.describe Spree::Api::V3::Store::Products::FiltersController, type: :contro
include_context 'API v3 Store'
- let(:taxonomy) { create(:taxonomy, store: store) }
- let!(:taxon) { create(:taxon, taxonomy: taxonomy) }
- let!(:child_taxon1) { create(:taxon, taxonomy: taxonomy, parent: taxon, name: 'Shirts') }
- let!(:child_taxon2) { create(:taxon, taxonomy: taxonomy, parent: taxon, name: 'Pants') }
+ let!(:category) { create(:category) }
+ let!(:child_taxon1) { create(:category, parent: category, name: 'Shirts') }
+ let!(:child_taxon2) { create(:category, parent: category, name: 'Pants') }
let(:option_type_size) { create(:option_type, name: 'size', presentation: 'Size', filterable: true, position: 1) }
let(:option_type_color) { create(:option_type, name: 'color', presentation: 'Color', filterable: true, position: 2) }
@@ -18,7 +17,7 @@ RSpec.describe Spree::Api::V3::Store::Products::FiltersController, type: :contro
let(:option_value_blue) { create(:option_value, option_type: option_type_color, name: 'blue', presentation: 'Blue', position: 2) }
let!(:product1) do
- create(:product, status: 'active', taxons: [child_taxon1]).tap do |p|
+ create(:product, status: 'active', categories: [child_taxon1]).tap do |p|
p.option_types << option_type_size
p.option_types << option_type_color
create(:variant, product: p, option_values: [option_value_small, option_value_red])
@@ -26,14 +25,14 @@ RSpec.describe Spree::Api::V3::Store::Products::FiltersController, type: :contro
end
let!(:product2) do
- create(:product, status: 'active', taxons: [child_taxon1]).tap do |p|
+ create(:product, status: 'active', categories: [child_taxon1]).tap do |p|
p.option_types << option_type_size
create(:variant, product: p, option_values: [option_value_medium])
end
end
let!(:product3) do
- create(:product, status: 'active', taxons: [child_taxon2]).tap do |p|
+ create(:product, status: 'active', categories: [child_taxon2]).tap do |p|
p.option_types << option_type_color
create(:variant, product: p, option_values: [option_value_blue])
end
@@ -128,7 +127,7 @@ RSpec.describe Spree::Api::V3::Store::Products::FiltersController, type: :contro
end
it 'returns child categories as filter options' do
- get :index, params: { category_id: taxon.prefixed_id }
+ get :index, params: { category_id: category.prefixed_id }
category_filter = json_response['filters'].find { |f| f['type'] == 'category' }
expect(category_filter).to be_present
@@ -138,9 +137,9 @@ RSpec.describe Spree::Api::V3::Store::Products::FiltersController, type: :contro
end
it 'defaults a category to manual sort (category does not drive default_sort in 6.0)' do
- taxon.update!(sort_order: 'price asc')
+ category.update!(sort_order: 'price asc')
- get :index, params: { category_id: taxon.prefixed_id }
+ get :index, params: { category_id: category.prefixed_id }
expect(json_response['default_sort']).to eq('manual')
end
diff --git a/spree/api/spec/controllers/spree/api/v3/store/products/meilisearch_integration_spec.rb b/spree/api/spec/controllers/spree/api/v3/store/products/meilisearch_integration_spec.rb
index ad5ebe1721..a782c50e6a 100644
--- a/spree/api/spec/controllers/spree/api/v3/store/products/meilisearch_integration_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/store/products/meilisearch_integration_spec.rb
@@ -19,9 +19,8 @@ RSpec.describe 'Meilisearch Integration', type: :controller, if: ENV['MEILISEARC
let(:index_name) { "#{store.code}_products" }
# Categories
- let(:taxonomy) { create(:taxonomy, store: store) }
- let(:clothing_category) { create(:taxon, name: 'Clothing', taxonomy: taxonomy) }
- let(:shoes_category) { create(:taxon, name: 'Shoes', taxonomy: taxonomy) }
+ let(:clothing_category) { create(:category, name: 'Clothing') }
+ let(:shoes_category) { create(:category, name: 'Shoes') }
# Option types
let(:color_option) { create(:option_type, name: 'color', presentation: 'Color', filterable: true) }
@@ -43,7 +42,7 @@ RSpec.describe 'Meilisearch Integration', type: :controller, if: ENV['MEILISEARC
# Products with different attributes
let!(:cheap_red_shirt) do
- p = create(:product, name: 'Red Cotton Shirt', status: 'active', store: store, taxons: [clothing_category])
+ p = create(:product, name: 'Red Cotton Shirt', status: 'active', store: store, categories: [clothing_category])
p.option_types << color_option
p.option_types << size_option
v = create(:variant, product: p, option_values: [red, small])
@@ -53,7 +52,7 @@ RSpec.describe 'Meilisearch Integration', type: :controller, if: ENV['MEILISEARC
end
let!(:expensive_blue_shirt) do
- p = create(:product, name: 'Blue Silk Shirt', status: 'active', store: store, taxons: [clothing_category])
+ p = create(:product, name: 'Blue Silk Shirt', status: 'active', store: store, categories: [clothing_category])
p.option_types << color_option
v = create(:variant, product: p, option_values: [blue, large])
v.prices.find_or_create_by!(currency: 'USD').update!(amount: 89.99)
@@ -62,7 +61,7 @@ RSpec.describe 'Meilisearch Integration', type: :controller, if: ENV['MEILISEARC
end
let!(:blue_shoes) do
- p = create(:product, name: 'Blue Running Shoes', status: 'active', store: store, taxons: [shoes_category])
+ p = create(:product, name: 'Blue Running Shoes', status: 'active', store: store, categories: [shoes_category])
p.option_types << color_option
p.option_types << size_option
v = create(:variant, product: p, option_values: [blue, small])
@@ -260,7 +259,7 @@ RSpec.describe 'Meilisearch Integration', type: :controller, if: ENV['MEILISEARC
end
# Product with neither metafield — exercises missing-attribute sort placement.
let!(:missing_metafield_product) do
- create(:product, name: 'Green Linen Shirt', status: 'active', store: store, taxons: [clothing_category])
+ create(:product, name: 'Green Linen Shirt', status: 'active', store: store, categories: [clothing_category])
end
before do
diff --git a/spree/api/spec/integration/spree/api/v3/admin/categories_spec.rb b/spree/api/spec/integration/spree/api/v3/admin/categories_spec.rb
index a951d473fa..cfd3eb4766 100644
--- a/spree/api/spec/integration/spree/api/v3/admin/categories_spec.rb
+++ b/spree/api/spec/integration/spree/api/v3/admin/categories_spec.rb
@@ -6,7 +6,7 @@ RSpec.describe 'Admin Categories API', type: :request, swagger_doc: 'api-referen
include_context 'API v3 Admin'
# A top-level category — parentless, store-owned, no taxonomy.
- let!(:category) { Spree::Category.create!(name: 'Clothing', store: store) }
+ let!(:category) { create(:category, name: 'Clothing', store: store) }
let(:Authorization) { "Bearer #{admin_jwt_token}" }
path '/api/v3/admin/categories' do
@@ -204,7 +204,7 @@ RSpec.describe 'Admin Categories API', type: :request, swagger_doc: 'api-referen
}
response '200', 'category repositioned' do
- let!(:other) { Spree::Category.create!(name: 'Footwear', store: store) }
+ let!(:other) { create(:category, name: 'Footwear', store: store) }
let(:'x-spree-api-key') { secret_api_key.plaintext_token }
let(:id) { other.prefixed_id }
let(:body) { { new_parent_id: category.prefixed_id, new_position: 0 } }
@@ -290,7 +290,7 @@ RSpec.describe 'Admin Categories API', type: :request, swagger_doc: 'api-referen
let(:category_id) { category.prefixed_id }
let(:id) { product.prefixed_id }
- before { Spree::ProductCategory.create!(taxon: category, product: product, position: 1) }
+ before { create(:product_category, category: category, product: product, position: 1) }
run_test!
end
@@ -324,7 +324,7 @@ RSpec.describe 'Admin Categories API', type: :request, swagger_doc: 'api-referen
let(:id) { product.prefixed_id }
let(:body) { { new_position: 0 } }
- before { Spree::ProductCategory.create!(taxon: category, product: product, position: 1) }
+ before { create(:product_category, category: category, product: product, position: 1) }
run_test!
end
diff --git a/spree/api/spec/integration/spree/api/v3/admin/products_spec.rb b/spree/api/spec/integration/spree/api/v3/admin/products_spec.rb
index d3ec489b6b..f03f72f11e 100644
--- a/spree/api/spec/integration/spree/api/v3/admin/products_spec.rb
+++ b/spree/api/spec/integration/spree/api/v3/admin/products_spec.rb
@@ -420,7 +420,7 @@ RSpec.describe 'Admin Products API', type: :request, swagger_doc: 'api-reference
produces 'application/json'
security [api_key: [], bearer_auth: []]
description <<~DESC
- Attaches each product in `ids` to every category (taxon) in `category_ids`.
+ Attaches each product in `ids` to every category (category) in `category_ids`.
Categories from sibling stores are silently ignored.
DESC
admin_scope :write, :products
@@ -440,8 +440,7 @@ RSpec.describe 'Admin Products API', type: :request, swagger_doc: 'api-reference
response '200', 'products added to categories' do
let(:'x-spree-api-key') { secret_api_key.plaintext_token }
- let(:taxonomy) { create(:taxonomy, store: store) }
- let(:category) { create(:taxon, taxonomy: taxonomy) }
+ let(:category) { create(:category) }
let(:body) { { ids: [product.prefixed_id], category_ids: [category.prefixed_id] } }
schema type: :object, properties: {
@@ -452,7 +451,7 @@ RSpec.describe 'Admin Products API', type: :request, swagger_doc: 'api-reference
run_test! do |response|
data = JSON.parse(response.body)
expect(data).to eq('product_count' => 1, 'category_count' => 1)
- expect(product.reload.taxons).to include(category)
+ expect(product.reload.categories).to include(category)
end
end
end
@@ -482,16 +481,15 @@ RSpec.describe 'Admin Products API', type: :request, swagger_doc: 'api-reference
response '200', 'products removed from categories' do
let(:'x-spree-api-key') { secret_api_key.plaintext_token }
- let(:taxonomy) { create(:taxonomy, store: store) }
- let(:category) { create(:taxon, taxonomy: taxonomy) }
+ let(:category) { create(:category) }
let(:body) { { ids: [product.prefixed_id], category_ids: [category.prefixed_id] } }
- before { product.taxons << category }
+ before { product.categories << category }
run_test! do |response|
data = JSON.parse(response.body)
expect(data).to eq('product_count' => 1, 'category_count' => 1)
- expect(product.reload.taxons).not_to include(category)
+ expect(product.reload.categories).not_to include(category)
end
end
end
diff --git a/spree/api/spec/integration/spree/api/v3/store/categories_spec.rb b/spree/api/spec/integration/spree/api/v3/store/categories_spec.rb
index 95d7e10301..73bdb4d6ec 100644
--- a/spree/api/spec/integration/spree/api/v3/store/categories_spec.rb
+++ b/spree/api/spec/integration/spree/api/v3/store/categories_spec.rb
@@ -4,15 +4,12 @@ require 'swagger_helper'
RSpec.describe 'Categories API', type: :request, swagger_doc: 'api-reference/store.yaml' do
include_context 'API v3 Store'
-
- let!(:taxonomy) { create(:taxonomy, store: store) }
- let!(:root_taxon) { taxonomy.root }
- let!(:category) { create(:taxon, taxonomy: taxonomy, parent: root_taxon) }
- let!(:child_category) { create(:taxon, taxonomy: taxonomy, parent: category) }
- let!(:product) { create(:product, status: 'active', taxons: [category]) }
+ let!(:root_category) { create(:category, name: 'Catalog', store: store) }
+ let!(:category) { create(:category, parent: root_category) }
+ let!(:child_category) { create(:category, parent: category) }
+ let!(:product) { create(:product, status: 'active', categories: [category]) }
let!(:other_store) { create(:store) }
- let!(:other_taxonomy) { create(:taxonomy, store: other_store) }
- let!(:other_category) { create(:taxon, taxonomy: other_taxonomy) }
+ let!(:other_category) { create(:category, store: other_store) }
path '/api/v3/store/categories' do
get 'List categories' do
diff --git a/spree/api/spec/integration/spree/api/v3/store/products_spec.rb b/spree/api/spec/integration/spree/api/v3/store/products_spec.rb
index d3f66c04fd..2658c484c0 100644
--- a/spree/api/spec/integration/spree/api/v3/store/products_spec.rb
+++ b/spree/api/spec/integration/spree/api/v3/store/products_spec.rb
@@ -5,21 +5,20 @@ require 'swagger_helper'
RSpec.describe 'Products API', type: :request, swagger_doc: 'api-reference/store.yaml' do
include_context 'API v3 Store'
- let(:taxonomy) { create(:taxonomy, store: store) }
- let(:taxon) { create(:taxon, taxonomy: taxonomy) }
- let(:child_taxon) { create(:taxon, taxonomy: taxonomy, parent: taxon, name: 'Shirts') }
+ let(:category) { create(:category) }
+ let(:child_category) { create(:category, parent: category, name: 'Shirts') }
let(:option_type) { create(:option_type, name: 'size', presentation: 'Size', filterable: true) }
let(:option_value_small) { create(:option_value, option_type: option_type, name: 'small', presentation: 'S') }
let!(:product) do
- create(:product, status: 'active', taxons: [child_taxon],
+ create(:product, status: 'active', categories: [child_category],
description: '<p>A <strong>comfortable</strong> cotton t-shirt.</p>').tap do |p|
p.option_types << option_type
create(:variant, product: p, option_values: [option_value_small])
end
end
- let!(:product2) { create(:product, status: 'active', taxons: [child_taxon]) }
+ let!(:product2) { create(:product, status: 'active', categories: [child_category]) }
let!(:draft_product) { create(:product, status: 'draft') }
let!(:other_store) { create(:store) }
let!(:other_store_product) { create(:product, store: other_store) }
@@ -291,7 +290,7 @@ RSpec.describe 'Products API', type: :request, swagger_doc: 'api-reference/store
response '200', 'filters scoped to category' do
let(:'x-spree-api-key') { api_key.token }
- let(:category_id) { taxon.prefixed_id }
+ let(:category_id) { category.prefixed_id }
schema type: :object,
properties: {
diff --git a/spree/api/spec/services/spree/api/v3/filters_aggregator_spec.rb b/spree/api/spec/services/spree/api/v3/filters_aggregator_spec.rb
index 6e99799278..7270ab4310 100644
--- a/spree/api/spec/services/spree/api/v3/filters_aggregator_spec.rb
+++ b/spree/api/spec/services/spree/api/v3/filters_aggregator_spec.rb
@@ -3,23 +3,22 @@ require 'spec_helper'
RSpec.describe Spree::Api::V3::FiltersAggregator do
let(:store) { @default_store }
let(:currency) { 'USD' }
- let(:taxonomy) { create(:taxonomy, store: store) }
- let(:taxon) { create(:taxon, taxonomy: taxonomy) }
- let(:child_taxon) { create(:taxon, taxonomy: taxonomy, parent: taxon, name: 'Child') }
+ let(:category) { create(:category) }
+ let(:child_category) { create(:category, parent: category, name: 'Child') }
let(:option_type) { create(:option_type, name: 'size', presentation: 'Size', filterable: true) }
let(:option_value_s) { create(:option_value, option_type: option_type, name: 'small', presentation: 'S') }
let(:option_value_m) { create(:option_value, option_type: option_type, name: 'medium', presentation: 'M') }
let!(:product1) do
- create(:product, status: 'active', taxons: [child_taxon]).tap do |p|
+ create(:product, status: 'active', categories: [child_category]).tap do |p|
p.option_types << option_type
create(:variant, product: p, option_values: [option_value_s])
end
end
let!(:product2) do
- create(:product, status: 'active', taxons: [child_taxon]).tap do |p|
+ create(:product, status: 'active', categories: [child_category]).tap do |p|
p.option_types << option_type
create(:variant, product: p, option_values: [option_value_m])
end
@@ -27,7 +26,7 @@ RSpec.describe Spree::Api::V3::FiltersAggregator do
let(:scope) { store.products.available(Time.current, currency) }
- subject { described_class.new(scope: scope, currency: currency, category: taxon) }
+ subject { described_class.new(scope: scope, currency: currency, category: category) }
describe '#call' do
let(:result) { subject.call }
@@ -63,8 +62,8 @@ RSpec.describe Spree::Api::V3::FiltersAggregator do
end
it "ignores the category's own sort_order (category does not drive default_sort in 6.0)" do
- taxon.update!(sort_order: 'price asc')
- aggregator = described_class.new(scope: scope, currency: currency, category: taxon)
+ category.update!(sort_order: 'price asc')
+ aggregator = described_class.new(scope: scope, currency: currency, category: category)
expect(aggregator.call[:default_sort]).to eq('manual')
end
end
@@ -228,14 +227,14 @@ RSpec.describe Spree::Api::V3::FiltersAggregator do
let(:red) { create(:option_value, option_type: color_type, name: 'red', presentation: 'Red') }
let!(:blue_product) do
- create(:product, status: 'active', taxons: [child_taxon]).tap do |p|
+ create(:product, status: 'active', categories: [child_category]).tap do |p|
p.option_types << color_type << option_type
create(:variant, product: p, option_values: [blue, option_value_s])
end
end
let!(:red_product) do
- create(:product, status: 'active', taxons: [child_taxon]).tap do |p|
+ create(:product, status: 'active', categories: [child_category]).tap do |p|
p.option_types << color_type << option_type
create(:variant, product: p, option_values: [red, option_value_m])
end
diff --git a/spree/core/app/jobs/spree/imports/create_categories_job.rb b/spree/core/app/jobs/spree/imports/create_categories_job.rb
index 0b70430774..1729ad5632 100644
--- a/spree/core/app/jobs/spree/imports/create_categories_job.rb
+++ b/spree/core/app/jobs/spree/imports/create_categories_job.rb
@@ -2,7 +2,7 @@ module Spree
module Imports
class CreateCategoriesJob < Spree::Imports::BaseJob
# Concurrent imports can race on `with_matching_name(...).first || create!(...)`
- # for the same taxonomy/taxon name and hit the unique index; a retry then finds
+ # for the same category name and hit the unique index; a retry then finds
# the peer's committed row.
retry_on ActiveRecord::RecordNotUnique, wait: :polynomially_longer, attempts: 5
@@ -11,28 +11,37 @@ module Spree
store = Spree::Store.find(store_id)
with_store_content_locale(store) do
- categories = taxon_pretty_names.filter_map { |taxon_pretty_name| find_or_create_taxon(store, taxon_pretty_name) }
+ categories = taxon_pretty_names.filter_map { |taxon_pretty_name| find_or_create_category(store, taxon_pretty_name) }
product.categories = categories
end
end
private
- def find_or_create_taxon(store, taxon_pretty_name)
- taxon_names = taxon_pretty_name.strip.split('->').map(&:strip).map(&:presence).compact
- return if taxon_names.empty?
+ # Walks a "Men -> Clothing -> Shirts" path, creating each missing level as a
+ # store-owned category. The first segment becomes a top-level category.
+ def find_or_create_category(store, taxon_pretty_name)
+ category_names = taxon_pretty_name.strip.split('->').map(&:strip).map(&:presence).compact
+ return if category_names.empty?
- taxonomy_name = taxon_names.shift
- taxonomy = store.taxonomies.with_matching_name(taxonomy_name).first || store.taxonomies.create!(name: taxonomy_name)
+ category_names.inject(nil) do |parent, category_name|
+ siblings = store.categories.where(parent: parent)
- last_taxon = taxonomy.root
-
- taxon_names.each do |taxon_name|
- last_taxon = taxonomy.taxons.with_matching_name(taxon_name).where(parent: last_taxon).first ||
- taxonomy.taxons.create!(name: taxon_name, parent: last_taxon)
+ siblings.with_matching_name(category_name).first ||
+ find_by_permalink(siblings, parent, category_name) ||
+ store.categories.create!(name: category_name, parent: parent)
end
+ end
+
+ # Uniqueness is on the permalink, and distinct names can normalize to the
+ # same slug ("Foo Bar" and "Foo-Bar" both become "foo-bar"). Matching on
+ # name alone would miss that row and then fail creating a duplicate.
+ def find_by_permalink(siblings, parent, category_name)
+ slug = category_name.to_s.to_url
+ return if slug.blank?
- last_taxon
+ permalink = parent.present? ? "#{parent.permalink}/#{slug}" : slug
+ siblings.find_by(permalink: permalink)
end
end
end
diff --git a/spree/core/app/jobs/spree/products/touch_categories_job.rb b/spree/core/app/jobs/spree/products/touch_categories_job.rb
index 5756280cbf..142755b674 100644
--- a/spree/core/app/jobs/spree/products/touch_categories_job.rb
+++ b/spree/core/app/jobs/spree/products/touch_categories_job.rb
@@ -3,9 +3,12 @@ module Spree
class TouchCategoriesJob < ::Spree::BaseJob
queue_as Spree.queues.categories
- def perform(category_ids, taxonomy_ids)
+ # @param category_ids [Array<Integer>]
+ # @param _legacy_taxonomy_ids [Array<Integer>] ignored; accepted so jobs
+ # enqueued before 6.0 stopped touching taxonomies still deserialize.
+ # Removed in 6.1.
+ def perform(category_ids, _legacy_taxonomy_ids = nil)
Spree::Category.where(id: category_ids).update_all(updated_at: Time.current)
- Spree::Taxonomy.where(id: taxonomy_ids).update_all(updated_at: Time.current)
end
end
end
diff --git a/spree/core/app/models/concerns/spree/search_indexable.rb b/spree/core/app/models/concerns/spree/search_indexable.rb
index fa3c13efab..dcda015282 100644
--- a/spree/core/app/models/concerns/spree/search_indexable.rb
+++ b/spree/core/app/models/concerns/spree/search_indexable.rb
@@ -72,7 +72,7 @@ module Spree
# we use the record's own +store_id+ (Product, Customer, Order, …). The
# multi-store extension overrides this to fan out across every store the
# record is attached to. Falling back to +Spree::Store.default+ covers
- # records like categories/taxonomies that don't have a +store_id+ column.
+ # records that don't have a +store_id+ column at all.
def store_ids_for_indexing
return [store_id] if respond_to?(:store_id) && store_id.present?
diff --git a/spree/core/app/models/spree/category.rb b/spree/core/app/models/spree/category.rb
index 058c473311..f9ab27164e 100644
--- a/spree/core/app/models/spree/category.rb
+++ b/spree/core/app/models/spree/category.rb
@@ -25,18 +25,25 @@ module Spree
#
extend FriendlyId
friendly_id :permalink, slug_column: :permalink, use: :history
- acts_as_nested_set dependent: :destroy, counter_cache: :children_count
+ # scope: :store — each store owns an independent tree, so lft/rgt are
+ # numbered per store and a move in one store never renumbers another's.
+ # Safe because every category has a store_id: pre-5.6 rows are backfilled
+ # from their taxonomy by spree:taxons:backfill_store_id (5.5 -> 5.6), and
+ # from 5.6 on #ensure_store stamps one at creation.
+ acts_as_nested_set scope: :store, dependent: :destroy, counter_cache: :children_count
#
# Associations
#
- belongs_to :taxonomy, class_name: 'Spree::Taxonomy', inverse_of: :taxons
- belongs_to :store, class_name: 'Spree::Store', optional: true
+ # @deprecated Read only by the 5.6 -> 6.0 upgrade task, which needs the
+ # column to find the rows it migrates. Removed in 6.1 with Spree::Taxonomy.
+ belongs_to :taxonomy, class_name: 'Spree::Taxonomy', inverse_of: :taxons, deprecated: true
has_many :product_categories, -> { order(:position) }, class_name: 'Spree::ProductCategory', dependent: :destroy_async, inverse_of: :category
has_many :products, through: :product_categories
# @deprecated Use #product_categories; removed in 6.1.
def classifications
+ Spree::Deprecation.warn('Spree::Category#classifications is deprecated and will be removed in Spree 6.1. Use Spree::Category#product_categories instead.')
product_categories
end
@@ -56,16 +63,12 @@ module Spree
# Validations
#
validates :name, presence: true
- validates :taxonomy, presence: true, if: :requires_taxonomy?
- # Taxonomy-backed categories are unique within their taxonomy; taxonomy-less
- # categories (store-owned) are unique within their store, so two stores can
- # each have a top-level "Shoes".
- validates :name, uniqueness: { scope: %i[parent_id taxonomy_id], case_sensitive: false }, if: :requires_taxonomy?
- validates :permalink, uniqueness: { scope: %i[parent_id taxonomy_id], case_sensitive: false }, if: :requires_taxonomy?
- validates :name, uniqueness: { scope: %i[parent_id store_id], case_sensitive: false }, unless: :requires_taxonomy?
- validates :permalink, uniqueness: { scope: %i[parent_id store_id], case_sensitive: false }, unless: :requires_taxonomy?
- validate :check_for_root, on: :create
- validate :parent_belongs_to_same_taxonomy
+ # The permalink is the category's full path ("men/clothing/shirts"), so it
+ # already encodes the hierarchy — it only has to be unique within its store.
+ # Names carry no such constraint: two categories may share a name as long as
+ # they sit in different branches, which their permalinks reflect.
+ validates :permalink, uniqueness: { scope: :store_id, case_sensitive: false }
+ validate :parent_belongs_to_same_store
with_options length: { maximum: 255 }, allow_blank: true do
validates :meta_keywords
validates :meta_description
@@ -77,11 +80,9 @@ module Spree
# Callbacks
#
before_validation :set_permalink, if: :name
- before_validation :copy_taxonomy_from_parent
before_save :set_pretty_name
- after_save :touch_ancestors_and_taxonomy
- after_update :sync_taxonomy_name
- after_touch :touch_ancestors_and_taxonomy
+ after_save :touch_ancestors
+ after_touch :touch_ancestors
after_commit :regenerate_pretty_name_and_permalink, on: :update, if: :should_regenerate_pretty_name_and_permalink?
after_move :regenerate_pretty_name_and_permalink
after_move :regenerate_translations_pretty_name_and_permalink
@@ -103,14 +104,8 @@ module Spree
#
# Scopes
#
- # 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_stores, ->(stores) { where(store_id: Array(stores).map(&:id)) }
#
# Search
#
@@ -131,7 +126,7 @@ module Spree
#
# Ransack
#
- self.whitelisted_ransackable_associations = %w[taxonomy parent]
+ self.whitelisted_ransackable_associations = %w[parent]
self.whitelisted_ransackable_attributes = %w[name permalink automatic depth is_root children_count
products_count pretty_name parent_id]
@@ -159,19 +154,6 @@ 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
-
- # Categories are owned directly via +store_id+ and never require a taxonomy.
- # (Legacy taxonomy-backed rows still carry their taxonomy until it is dropped
- # in 6.1; they simply aren't validated as requiring one.)
- def requires_taxonomy?
- false
- end
-
def active_products_with_descendants
@active_products_with_descendants ||= store.products
.joins(:product_categories)
@@ -381,19 +363,9 @@ module Spree
nil
end
- def sync_taxonomy_name
- return unless taxonomy.present?
- return unless saved_changes.key?(:name) && root?
- return if taxonomy.name.to_s == name.to_s
-
- taxonomy.update(name: name)
- end
-
- def touch_ancestors_and_taxonomy
- # Touches all ancestors at once to avoid recursive taxonomy touch, and reduce queries.
+ # Touches all ancestors at once rather than recursively, to reduce queries.
+ def touch_ancestors
ancestors.update_all(updated_at: Time.current)
- # Have taxonomy touch happen in #touch_ancestors_and_taxonomy rather than association option in order for imports to override.
- taxonomy.touch if taxonomy.present?
end
def capture_parent_before_move
@@ -422,30 +394,27 @@ module Spree
end
end
- def check_for_root
- return unless taxonomy.try(:root).present? && parent_id.nil?
-
- errors.add(:root_conflict, 'this taxonomy already has a root category')
- end
-
- def parent_belongs_to_same_taxonomy
- return unless parent.present? && parent.taxonomy_id != taxonomy_id
+ # A category added under a legacy taxonomy-backed parent inherits its
+ # taxonomy, so #ensure_store and the .for_store fallback can still resolve a
+ # store for installs that have not yet run the 6.0 upgrade task. Removed in
+ # 6.1 with Spree::Taxonomy.
+ # A tree belongs to one store.
+ def parent_belongs_to_same_store
+ return if parent.blank?
- errors.add(:parent, 'must belong to the same taxonomy')
- end
+ parent_store = parent.store
+ return if parent_store.blank? || store.blank? || parent_store.id == store.id
- def copy_taxonomy_from_parent
- self.taxonomy = parent.taxonomy if parent.present? && taxonomy.blank?
+ errors.add(:parent, :must_belong_to_same_store)
end
- # Every category 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.
+ # Every category is store-owned, so a new record always gets a store_id:
+ # from its parent, or the current store. This is what lets the rest of the
+ # model treat store_id as always present.
def ensure_store
return if store_id.present?
- self.store = taxonomy&.store || parent&.store || Spree::Store.current
+ self.store = parent&.store || Spree::Store.current
end
def regenerate_translations_pretty_name_and_permalink
diff --git a/spree/core/app/models/spree/exports/products.rb b/spree/core/app/models/spree/exports/products.rb
index a59398de6f..2269aee206 100644
--- a/spree/core/app/models/spree/exports/products.rb
+++ b/spree/core/app/models/spree/exports/products.rb
@@ -3,7 +3,7 @@ module Spree
class Products < Spree::Export
# to avoid N+1 queries
def scope_includes
- includes = [:tax_category, :option_types, { taxons: :taxonomy }, { variants: variant_includes }]
+ includes = [:tax_category, :option_types, :categories, { variants: variant_includes }]
includes << { metafields: :metafield_definition }
includes
end
diff --git a/spree/core/app/models/spree/permission_sets/default_customer.rb b/spree/core/app/models/spree/permission_sets/default_customer.rb
index 8a91479dbc..5e04bf1b72 100644
--- a/spree/core/app/models/spree/permission_sets/default_customer.rb
+++ b/spree/core/app/models/spree/permission_sets/default_customer.rb
@@ -19,7 +19,6 @@ module Spree
can :read, Spree::State
can :read, Spree::Store
can :read, Spree::Category
- can :read, Spree::Taxonomy
can :read, Spree::Variant
can :read, Spree::Zone
diff --git a/spree/core/app/models/spree/permission_sets/product_display.rb b/spree/core/app/models/spree/permission_sets/product_display.rb
index 8a9cdab301..3770814c5c 100644
--- a/spree/core/app/models/spree/permission_sets/product_display.rb
+++ b/spree/core/app/models/spree/permission_sets/product_display.rb
@@ -18,7 +18,6 @@ module Spree
can [:read, :admin], Spree::Metafield
can [:read, :admin], Spree::Collection
can [:read, :admin], Spree::Category
- can [:read, :admin], Spree::Taxonomy
can [:read, :admin], Spree::ProductCategory
can [:read, :admin], Spree::Price
can [:read, :admin], Spree::PriceList
diff --git a/spree/core/app/models/spree/permission_sets/product_management.rb b/spree/core/app/models/spree/permission_sets/product_management.rb
index 2ae417b5b5..46ed03954e 100644
--- a/spree/core/app/models/spree/permission_sets/product_management.rb
+++ b/spree/core/app/models/spree/permission_sets/product_management.rb
@@ -1,7 +1,7 @@
# Permission set for full product and catalog management.
#
# This permission set provides complete access to manage products, variants,
-# and related catalog models like taxonomies and properties.
+# and related catalog models like categories and properties.
#
# @example
# Spree.permissions.assign(:merchandiser, Spree::PermissionSets::ProductManagement)
@@ -16,7 +16,6 @@ module Spree
can :manage, Spree::OptionType
can :manage, Spree::OptionValue
can :manage, Spree::Category
- can :manage, Spree::Taxonomy
can :manage, Spree::ProductCategory
can :manage, Spree::Collection
can :manage, Spree::ProductCollection
diff --git a/spree/core/app/models/spree/product.rb b/spree/core/app/models/spree/product.rb
index 2d9ab8943d..42a78435b7 100644
--- a/spree/core/app/models/spree/product.rb
+++ b/spree/core/app/models/spree/product.rb
@@ -38,7 +38,7 @@ module Spree
publishes_lifecycle_events
- MEMOIZED_METHODS = %w[total_on_hand taxonomy_ids category_and_ancestors
+ MEMOIZED_METHODS = %w[total_on_hand category_and_ancestors
default_variant_id tax_category default_variant variant_for_images
primary_category
purchasable? in_stock? backorderable? digital?]
@@ -76,7 +76,7 @@ module Spree
has_many :option_types, through: :product_option_types
has_many :product_categories, -> { order(created_at: :asc) }, class_name: 'Spree::ProductCategory', dependent: :delete_all, inverse_of: :product
has_many :categories, through: :product_categories, class_name: 'Spree::Category', source: :category, before_remove: :remove_category
- has_many :taxonomies, through: :categories
+ has_many :taxonomies, through: :categories, deprecated: true
# @deprecated The classification_count column was renamed to categories_count in 6.0.
alias_attribute :classification_count, :categories_count
@@ -911,12 +911,12 @@ module Spree
@category_and_ancestors ||= categories.map(&:self_and_ancestors).flatten.uniq
end
- # Iterate through this product's categories and taxonomies and touch their timestamps in a batch
+ # Iterate through this product's categories and touch their timestamps in a batch
def touch_categories
if categories.any?
Spree::Products::TouchCategoriesJob.
set(wait: 5.seconds).
- perform_later(category_and_ancestors.map(&:id), taxonomy_ids.uniq)
+ perform_later(category_and_ancestors.map(&:id))
end
end
diff --git a/spree/core/app/models/spree/store.rb b/spree/core/app/models/spree/store.rb
index 75da4af8f7..7d54655e7e 100644
--- a/spree/core/app/models/spree/store.rb
+++ b/spree/core/app/models/spree/store.rb
@@ -103,7 +103,8 @@ module Spree
has_many :store_credits, class_name: 'Spree::StoreCredit'
has_many :store_credit_events, through: :store_credits, class_name: 'Spree::StoreCreditEvent'
- has_many :taxonomies, class_name: 'Spree::Taxonomy'
+ # @deprecated Taxonomy is data-only in 6.0 and dropped in 6.1; use #categories.
+ has_many :taxonomies, class_name: 'Spree::Taxonomy', deprecated: true
has_many :categories, class_name: 'Spree::Category'
has_many :collections, class_name: 'Spree::Collection', dependent: :destroy_async
diff --git a/spree/core/app/models/spree/taxonomy.rb b/spree/core/app/models/spree/taxonomy.rb
index e67654e8f2..04f51908cb 100644
--- a/spree/core/app/models/spree/taxonomy.rb
+++ b/spree/core/app/models/spree/taxonomy.rb
@@ -1,56 +1,19 @@
module Spree
- # @deprecated Taxonomy is retained in 6.0 only to back not-yet-migrated
- # categories (the taxonomy_id fallback). Categories are now store-owned via
- # store_id; Taxonomy and the spree_taxonomies table are dropped in 6.1.
+ # @deprecated Data-only in 6.0. Categories are store-owned via +store_id+ and
+ # nothing creates or reads a taxonomy at runtime any more. The model and the
+ # spree_taxonomies table are retained solely so the 5.6 -> 6.0 upgrade task
+ # (spree:migrate_categories_and_collections) can read existing rows to
+ # backfill category store_id, and so an interrupted upgrade can be rolled
+ # back. Both drop in 6.1.
class Taxonomy < Spree.base_class
has_prefix_id :txnmy # Spree-specific: taxonomy
- include Spree::TranslatableResource
- include Spree::Metafields
- include Spree::Metadata
include Spree::SingleStoreResource
- TRANSLATABLE_FIELDS = %i[name].freeze
- translates(*TRANSLATABLE_FIELDS, column_fallback: Spree.mobility_column_fallback)
-
- acts_as_list
-
- validates :name, presence: true, uniqueness: { case_sensitive: false, scope: :store_id }
- validates :store, presence: true
-
has_many :taxons, class_name: 'Spree::Category', inverse_of: :taxonomy
has_one :root, -> { where parent_id: nil }, class_name: 'Spree::Category', dependent: :destroy
belongs_to :store, class_name: 'Spree::Store'
- after_create :set_root
- after_update :set_root_taxon_name
-
default_scope { order("#{table_name}.position, #{table_name}.created_at") }
-
- scope :with_matching_name, ->(name_to_match) do
- value = name_to_match.to_s.strip.downcase
-
- if Spree.use_translations?
- i18n { name.lower.eq(value) }
- else
- where(arel_table[:name].lower.eq(value))
- end
- end
-
- self.whitelisted_ransackable_attributes = %w[name]
- self.whitelisted_ransackable_associations = %w[root]
-
- private
-
- def set_root
- self.root ||= Taxon.create!(taxonomy_id: id, name: name)
- end
-
- def set_root_taxon_name
- return unless saved_changes.key?(:name)
- return if name.to_s == root.name.to_s
-
- root.update(name: name)
- end
end
end
diff --git a/spree/core/config/locales/en.yml b/spree/core/config/locales/en.yml
index a7b75fb093..63115e6759 100644
--- a/spree/core/config/locales/en.yml
+++ b/spree/core/config/locales/en.yml
@@ -255,6 +255,10 @@ en:
values_should_be_percent: Tier values should all be percentages between 0% and 100%
preferred_tiers:
should_be_hash: should be a hash
+ spree/category:
+ attributes:
+ parent:
+ must_belong_to_same_store: must belong to the same store
spree/classification:
attributes:
taxon_id:
diff --git a/spree/core/db/migrate/20260807150001_reindex_spree_categories_by_store.rb b/spree/core/db/migrate/20260807150001_reindex_spree_categories_by_store.rb
new file mode 100644
index 0000000000..25e1b450e4
--- /dev/null
+++ b/spree/core/db/migrate/20260807150001_reindex_spree_categories_by_store.rb
@@ -0,0 +1,42 @@
+require 'spree/category_permalink_deduplicator'
+
+class ReindexSpreeCategoriesByStore < ActiveRecord::Migration[8.1]
+ PERMALINK_INDEX = 'index_spree_categories_on_permalink_and_store_id'.freeze
+
+ # Categories are store-owned in 6.0, so uniqueness is scoped to the store rather
+ # than the taxonomy. The old taxonomy-scoped indexes stop constraining anything
+ # once spree:migrate_taxons_to_categories_and_collections nulls taxonomy_id,
+ # because SQL treats NULLs as distinct in unique indexes.
+ #
+ # The permalink is the category's full path ("men/clothing/shirts"), so it
+ # already encodes the hierarchy: scoping it by parent_id was always redundant,
+ # and (permalink, store_id) is the real constraint. Name uniqueness is dropped
+ # entirely — two categories may share a name in different branches, which their
+ # permalinks already distinguish.
+ #
+ # The duplicates are resolved here rather than in the upgrade rake task. A 5.6
+ # store has already run spree:taxons:backfill_store_id (5.5 -> 5.6), so its
+ # categories carry a store_id by the time this runs, and any cross-taxonomy
+ # permalink collision would fail index creation and block the deploy.
+ def up
+ remove_index :spree_categories, name: 'index_spree_categories_on_name_and_parent_id_and_taxonomy_id', if_exists: true
+ remove_index :spree_categories, name: 'idx_on_permalink_parent_id_taxonomy_id_0aed458a0c', if_exists: true
+
+ renamed = Spree::CategoryPermalinkDeduplicator.new(connection).call
+ say "resolved #{renamed} duplicate category permalinks", true if renamed.positive?
+
+ add_index :spree_categories, %i[permalink store_id],
+ name: PERMALINK_INDEX, unique: true, if_not_exists: true
+ end
+
+ def down
+ remove_index :spree_categories, name: PERMALINK_INDEX, if_exists: true
+
+ add_index :spree_categories, %i[name parent_id taxonomy_id],
+ name: 'index_spree_categories_on_name_and_parent_id_and_taxonomy_id',
+ unique: true, if_not_exists: true
+ add_index :spree_categories, %i[permalink parent_id taxonomy_id],
+ name: 'idx_on_permalink_parent_id_taxonomy_id_0aed458a0c',
+ unique: true, if_not_exists: true
+ end
+end
diff --git a/spree/core/lib/spree/category_permalink_deduplicator.rb b/spree/core/lib/spree/category_permalink_deduplicator.rb
new file mode 100644
index 0000000000..7f0c2ae989
--- /dev/null
+++ b/spree/core/lib/spree/category_permalink_deduplicator.rb
@@ -0,0 +1,328 @@
+# frozen_string_literal: true
+
+module Spree
+ # Resolves duplicate category permalinks within a store.
+ #
+ # Before 6.0 a permalink only had to be unique within its taxonomy, so one store
+ # could legitimately hold two "catalog" roots under different taxonomies. In 6.0
+ # the permalink is the category's full path and is unique per store, so those
+ # rows have to be separated before the unique index can exist.
+ #
+ # Runs on raw SQL rather than through Spree::Category: it is called from a
+ # migration, where the model's callbacks, validations and default scopes may not
+ # match the schema on disk.
+ class CategoryPermalinkDeduplicator
+ # Spelled out per schema rather than interpolated from #effective_store_id:
+ # this query binds no values, so writing it as constants keeps it provably
+ # free of interpolated input (Brakeman flags the assembled form). The queries
+ # below interpolate the same fragments but bind their values via #sanitize.
+ DUPLICATE_GROUPS_WITH_TAXONOMY_SQL = <<~SQL.freeze
+ SELECT COALESCE(c.store_id, t.store_id) AS sid, c.permalink
+ FROM spree_categories c
+ LEFT JOIN spree_taxonomies t ON t.id = c.taxonomy_id
+ WHERE c.permalink IS NOT NULL AND COALESCE(c.store_id, t.store_id) IS NOT NULL
+ GROUP BY COALESCE(c.store_id, t.store_id), c.permalink
+ HAVING COUNT(*) > 1
+ SQL
+
+ TRANSLATION_GROUPS_WITH_TAXONOMY_SQL = <<~SQL.freeze
+ SELECT COALESCE(c.store_id, t.store_id) AS sid, tr.locale, tr.permalink
+ FROM spree_category_translations tr
+ INNER JOIN spree_categories c ON c.id = tr.spree_category_id
+ LEFT JOIN spree_taxonomies t ON t.id = c.taxonomy_id
+ WHERE tr.permalink IS NOT NULL AND COALESCE(c.store_id, t.store_id) IS NOT NULL
+ GROUP BY COALESCE(c.store_id, t.store_id), tr.locale, tr.permalink
+ HAVING COUNT(*) > 1
+ SQL
+
+ TRANSLATION_GROUPS_SQL = <<~SQL.freeze
+ SELECT c.store_id AS sid, tr.locale, tr.permalink
+ FROM spree_category_translations tr
+ INNER JOIN spree_categories c ON c.id = tr.spree_category_id
+ WHERE tr.permalink IS NOT NULL AND c.store_id IS NOT NULL
+ GROUP BY c.store_id, tr.locale, tr.permalink
+ HAVING COUNT(*) > 1
+ SQL
+
+ DUPLICATE_GROUPS_SQL = <<~SQL.freeze
+ SELECT c.store_id AS sid, c.permalink
+ FROM spree_categories c
+ WHERE c.permalink IS NOT NULL AND c.store_id IS NOT NULL
+ GROUP BY c.store_id, c.permalink
+ HAVING COUNT(*) > 1
+ SQL
+
+ # @param connection [ActiveRecord::ConnectionAdapters::AbstractAdapter]
+ def initialize(connection = ActiveRecord::Base.connection)
+ @connection = connection
+ @permalinks_by_store = {}
+ end
+
+ # Rewrites every duplicate permalink, keeping the lowest-id row in each group
+ # and re-prefixing each rewritten row's descendants so subtree paths stay
+ # consistent with their parents.
+ #
+ # @return [Integer] number of categories whose permalink changed
+ def call
+ return 0 unless connection.table_exists?('spree_categories')
+
+ renamed = 0
+
+ # Re-queried each round rather than snapshotted once: re-prefixing a
+ # subtree can move a descendant onto a path some other row already holds,
+ # and that collision has to be resolved too or the unique index cannot be
+ # created. Each round strictly reduces the duplicates, so this terminates.
+ while (groups = duplicate_groups).any?
+ # A cascade rewrites descendants directly, so the cached permalink sets
+ # are stale by the end of a round.
+ @permalinks_by_store.clear
+ moved = resolve_groups(groups)
+ break if moved.zero? # defensive: never spin if a group cannot be moved
+
+ renamed += moved
+ end
+
+ # Independent of the base pass: a store whose base permalinks are already
+ # unique can still hold colliding translated ones, and the storefront
+ # resolves categories through that table.
+ renamed + rewrite_translations
+ end
+
+ private
+
+ # @return [Integer] rows moved in this round
+ def resolve_groups(groups)
+ moved = 0
+
+ groups.each do |store_id, permalink|
+ ids = ids_for(store_id, permalink)
+ # The first row keeps the permalink; the rest have to move.
+ ids.drop(1).each do |id|
+ candidate = unique_permalink(permalink, id, store_id)
+ rewrite(id, permalink, candidate)
+ permalinks_for(store_id) << candidate
+ moved += 1
+ end
+ end
+
+ moved
+ end
+
+ private
+
+ attr_reader :connection
+
+ # Binds values as parameters rather than interpolating them, so no user- or
+ # data-derived string is ever spliced into SQL text.
+ def sanitize(sql, *binds)
+ ActiveRecord::Base.sanitize_sql_array([sql, *binds])
+ end
+
+ # Groups by the store a category belongs to *or is about to*: rows still
+ # awaiting the upgrade backfill have a NULL store_id but resolve one through
+ # their taxonomy, and they collide the moment that backfill assigns it. Both
+ # cases have to be resolved before the unique index sees them.
+ #
+ # @return [Array<Array(Integer, String)>] (store_id, permalink) pairs appearing more than once
+ # Shallowest paths first. A parent must be rewritten before its children, so
+ # the subtree re-prefix in #rewrite fixes them; otherwise a child rewritten
+ # first gets a suffix of its own and is then re-prefixed too, leaving paths
+ # like "catalog-shop-b/kids-shop-b". SQL returns groups in no defined order.
+ def duplicate_groups
+ rows = connection.select_rows(taxonomies_table? ? DUPLICATE_GROUPS_WITH_TAXONOMY_SQL : DUPLICATE_GROUPS_SQL)
+ rows.sort_by { |_store_id, permalink| permalink.to_s.count('/') }
+ end
+
+ def ids_for(store_id, permalink)
+ connection.select_values(sanitize(<<~SQL, store_id, permalink))
+ SELECT c.id FROM spree_categories c
+ #{taxonomy_join}
+ WHERE #{effective_store_id} = ? AND c.permalink = ?
+ ORDER BY c.id
+ SQL
+ end
+
+ def taxonomy_join
+ return '' unless taxonomies_table?
+
+ 'LEFT JOIN spree_taxonomies t ON t.id = c.taxonomy_id'
+ end
+
+ def effective_store_id
+ taxonomies_table? ? 'COALESCE(c.store_id, t.store_id)' : 'c.store_id'
+ end
+
+ def taxonomies_table?
+ return @taxonomies_table unless @taxonomies_table.nil?
+
+ @taxonomies_table = connection.table_exists?('spree_taxonomies')
+ end
+
+ # Appends the owning taxonomy's name, falling back to a numeric suffix when
+ # that is absent or itself taken.
+ def unique_permalink(permalink, id, store_id)
+ candidate = [permalink, taxonomy_suffix(id)].compact.join('-')
+ return candidate unless taken?(candidate, store_id)
+
+ counter = 2
+ counter += 1 while taken?("#{candidate}-#{counter}", store_id)
+ "#{candidate}-#{counter}"
+ end
+
+ def taxonomy_suffix(id)
+ return nil unless taxonomies_table?
+
+ name = connection.select_value(sanitize(<<~SQL, id))
+ SELECT t.name FROM spree_taxonomies t
+ INNER JOIN spree_categories c ON c.taxonomy_id = t.id
+ WHERE c.id = ?
+ SQL
+
+ name.presence && name.to_s.parameterize.presence
+ end
+
+ def taken?(permalink, store_id)
+ permalinks_for(store_id).include?(permalink)
+ end
+
+ # Every permalink in the store, loaded once. COALESCE on store_id defeats the
+ # index, so testing candidates in memory beats a scan per candidate — and the
+ # numeric-suffix loop tries several per collision.
+ def permalinks_for(store_id)
+ @permalinks_by_store[store_id] ||= connection.select_values(sanitize(<<~SQL, store_id)).compact.to_set
+ SELECT c.permalink FROM spree_categories c
+ #{taxonomy_join}
+ WHERE #{effective_store_id} = ? AND c.permalink IS NOT NULL
+ SQL
+ end
+
+ # Moves the row and re-prefixes its subtree, so a child of "catalog" becomes a
+ # child of "catalog-shop-b" rather than keeping the old parent's path.
+ def rewrite(id, old_permalink, candidate)
+ connection.update(sanitize('UPDATE spree_categories SET permalink = ? WHERE id = ?', candidate, id))
+
+ descendants(id).each do |descendant_id, current|
+ next if current.blank? || !current.start_with?("#{old_permalink}/")
+
+ moved = current.sub("#{old_permalink}/", "#{candidate}/")
+ connection.update(sanitize('UPDATE spree_categories SET permalink = ? WHERE id = ?', moved, descendant_id))
+ end
+ end
+
+ # The storefront resolves a category by its *translated* permalink
+ # (`scope.i18n.find_by!(permalink:)`), and that table carries no uniqueness
+ # constraint — so a collision there survives the base-column pass and makes
+ # the lookup return whichever row it happens to reach first. Each locale is a
+ # separate namespace, so each is deduplicated on its own.
+ def rewrite_translations
+ return 0 unless connection.table_exists?('spree_category_translations')
+
+ renamed = 0
+
+ while (groups = translation_duplicate_groups).any?
+ moved = resolve_translation_groups(groups)
+ break if moved.zero?
+
+ renamed += moved
+ end
+
+ renamed
+ end
+
+ # @return [Integer] translation rows moved in this round
+ def resolve_translation_groups(groups)
+ renamed = 0
+
+ groups.each do |store_id, locale, permalink|
+ rows = translation_rows_for(store_id, locale, permalink)
+ taken = translation_permalinks_for(store_id, locale)
+
+ # The first row keeps the permalink; the rest have to move.
+ rows.drop(1).each do |translation_id, category_id|
+ counter = 2
+ counter += 1 while taken.include?("#{permalink}-#{counter}")
+ candidate = "#{permalink}-#{counter}"
+ taken << candidate
+
+ move_translation(translation_id, candidate)
+ # Children's translated paths are built from the parent's, so they have
+ # to follow it exactly as they do on the base column.
+ reprefix_descendant_translations(category_id, locale, permalink, candidate)
+ renamed += 1
+ end
+ end
+
+ renamed
+ end
+
+ def move_translation(translation_id, permalink)
+ connection.update(
+ sanitize('UPDATE spree_category_translations SET permalink = ? WHERE id = ?', permalink, translation_id)
+ )
+ end
+
+ def reprefix_descendant_translations(category_id, locale, old_permalink, new_permalink)
+ descendants(category_id).each do |descendant_id, _base_permalink|
+ row = connection.select_rows(sanitize(<<~SQL, descendant_id, locale)).first
+ SELECT id, permalink FROM spree_category_translations
+ WHERE spree_category_id = ? AND locale = ?
+ SQL
+ next if row.nil?
+
+ translation_id, current = row
+ next if current.blank? || !current.start_with?("#{old_permalink}/")
+
+ move_translation(translation_id, current.sub("#{old_permalink}/", "#{new_permalink}/"))
+ end
+ end
+
+ # Every (store, locale, permalink) appearing more than once, across all stores.
+ # Shallowest first, for the same reason as #duplicate_groups.
+ def translation_duplicate_groups
+ rows = connection.select_rows(taxonomies_table? ? TRANSLATION_GROUPS_WITH_TAXONOMY_SQL : TRANSLATION_GROUPS_SQL)
+ rows.sort_by { |_store_id, _locale, permalink| permalink.to_s.count('/') }
+ end
+
+ def translation_rows_for(store_id, locale, permalink)
+ connection.select_rows(sanitize(<<~SQL, store_id, locale, permalink))
+ SELECT tr.id, tr.spree_category_id
+ FROM spree_category_translations tr
+ INNER JOIN spree_categories c ON c.id = tr.spree_category_id
+ #{taxonomy_join}
+ WHERE #{effective_store_id} = ? AND tr.locale = ? AND tr.permalink = ?
+ ORDER BY tr.id
+ SQL
+ end
+
+ def translation_permalinks_for(store_id, locale)
+ connection.select_values(sanitize(<<~SQL, store_id, locale)).compact.to_set
+ SELECT tr.permalink
+ FROM spree_category_translations tr
+ INNER JOIN spree_categories c ON c.id = tr.spree_category_id
+ #{taxonomy_join}
+ WHERE #{effective_store_id} = ? AND tr.locale = ? AND tr.permalink IS NOT NULL
+ SQL
+ end
+
+ # Walks the parent chain rather than using lft/rgt, which may be stale on rows
+ # an installation has not rebuilt. One query per depth level, not per node.
+ #
+ # @return [Array<Array(Integer, String)>] (id, permalink) of every descendant
+ def descendants(id)
+ collected = []
+ frontier = [id]
+
+ until frontier.empty?
+ rows = connection.select_rows(
+ sanitize('SELECT id, permalink FROM spree_categories WHERE parent_id IN (?)', frontier)
+ )
+ break if rows.empty?
+
+ collected.concat(rows)
+ frontier = rows.map(&:first)
+ end
+
+ collected
+ end
+ end
+end
diff --git a/spree/core/lib/spree/core/engine.rb b/spree/core/lib/spree/core/engine.rb
index 234a6695ad..5a75ff89c0 100644
--- a/spree/core/lib/spree/core/engine.rb
+++ b/spree/core/lib/spree/core/engine.rb
@@ -318,7 +318,6 @@ module Spree
Spree::ProductType,
Spree::Collection,
Spree::Category,
- Spree::Taxonomy,
Spree::Store,
Spree::Policy
]
@@ -374,7 +373,6 @@ module Spree
Spree::StoreCredit,
Spree::TaxRate,
Spree::Category,
- Spree::Taxonomy,
Spree::Variant,
Spree.customer_class
]
diff --git a/spree/core/lib/spree/permitted_attributes.rb b/spree/core/lib/spree/permitted_attributes.rb
index ce2cd9c9d6..aef3593cba 100644
--- a/spree/core/lib/spree/permitted_attributes.rb
+++ b/spree/core/lib/spree/permitted_attributes.rb
@@ -5,6 +5,7 @@ module Spree
:allowed_origin_attributes,
:api_key_attributes,
:asset_attributes,
+ :category_attributes,
:checkout_attributes,
:classification_attributes,
:cms_page_attributes,
@@ -63,8 +64,6 @@ module Spree
:store_credit_category_attributes,
:tax_rate_attributes,
:tax_category_attributes,
- :taxon_attributes,
- :taxonomy_attributes,
:theme_attributes,
:user_attributes,
:variant_attributes,
@@ -262,18 +261,14 @@ module Spree
@@store_credit_category_attributes = [:name]
- @@taxonomy_attributes = [:name, :position]
-
@@tax_category_attributes = [:name, :tax_code,:description, :is_default]
@@tax_rate_attributes = [:name, :amount, :amount_percentage, :zone_id, :tax_category_id, :included_in_price, :show_rate_in_label, :calculator_type, calculator_attributes: {}]
- @@taxon_attributes = [
+ @@category_attributes = [
:name, :parent_id, :position, :icon, :description, :permalink,
- :taxonomy_id, :meta_description, :meta_keywords, :meta_title, :child_index,
- :automatic, :rules_match_policy, :sort_order,
- :image, :square_image, :description,
- taxon_rules_attributes: [:id, :type, :value, :match_policy, :_destroy],
+ :meta_description, :meta_keywords, :meta_title, :child_index,
+ :image, :square_image
]
@@theme_attributes = [:name, :type, :default]
@@ -305,5 +300,11 @@ module Spree
@@wished_item_attributes = [:variant_id, :quantity]
@@zone_attributes = [:name, :description, :default_tax, :kind, :states_country_id, country_ids: [], state_ids: []]
+
+ # @deprecated Renamed to .category_attributes in 6.0; removed in 6.1.
+ def self.taxon_attributes
+ Spree::Deprecation.warn('Spree::PermittedAttributes.taxon_attributes is deprecated and will be removed in Spree 6.1. Use .category_attributes instead.')
+ category_attributes
+ end
end
end
diff --git a/spree/core/lib/spree/testing_support/factories/category_factory.rb b/spree/core/lib/spree/testing_support/factories/category_factory.rb
new file mode 100644
index 0000000000..489bd1b8c3
--- /dev/null
+++ b/spree/core/lib/spree/testing_support/factories/category_factory.rb
@@ -0,0 +1,21 @@
+FactoryBot.define do
+ # No store attribute: Category#ensure_store resolves one from the parent, the
+ # taxonomy, or the current store — so a child lands in its parent's store
+ # rather than being forced into the default.
+ factory :category, class: Spree::Category do
+ sequence(:name) { |n| "category_#{n}" }
+
+ parent { nil }
+
+ trait :with_description do
+ description { '<div>Test <strong>description</strong></div>' }
+ end
+
+ trait :with_header_image do
+ after(:create) do |category|
+ category.image.attach(io: File.new(Spree::Core::Engine.root.join('spec', 'fixtures', 'thinking-cat.jpg')), filename: 'thinking-cat.jpg')
+ category.save!
+ end
+ end
+ end
+end
diff --git a/spree/core/lib/spree/testing_support/factories/taxon_factory.rb b/spree/core/lib/spree/testing_support/factories/taxon_factory.rb
index 2b1e1dac04..529013c323 100644
--- a/spree/core/lib/spree/testing_support/factories/taxon_factory.rb
+++ b/spree/core/lib/spree/testing_support/factories/taxon_factory.rb
@@ -1,27 +1,10 @@
FactoryBot.define do
- factory :taxon, class: Spree::Category, aliases: [:category] do
+ # @deprecated Use :category. Kept so existing extension and application suites
+ # keep passing through the 6.0 rename; removed in 6.1 with Spree::Taxon.
+ #
+ # A category is store-owned via store_id, so this creates no taxonomy. Callers
+ # that need a legacy taxonomy-backed row pass `taxonomy:` explicitly.
+ factory :taxon, parent: :category do
sequence(:name) { |n| "taxon_#{n}" }
-
- association :taxonomy, strategy: :create
- parent_id { taxonomy.root.id }
-
- trait :with_description do
- description { '<div>Test <strong>description</strong></div>' }
- end
-
- trait :with_header_image do
- after(:create) do |taxon|
- taxon.image.attach(io: File.new(Spree::Core::Engine.root.join('spec', 'fixtures', 'thinking-cat.jpg')), filename: 'thinking-cat.jpg')
- end
- end
- end
-
- factory :automatic_taxon, parent: :taxon do
- automatic { true }
- rules_match_policy { :all }
-
- trait :any_match_policy do
- rules_match_policy { :any }
- end
end
end
diff --git a/spree/core/lib/spree/testing_support/factories/taxonomy_factory.rb b/spree/core/lib/spree/testing_support/factories/taxonomy_factory.rb
index 09e304b334..ee5e6f9ea2 100644
--- a/spree/core/lib/spree/testing_support/factories/taxonomy_factory.rb
+++ b/spree/core/lib/spree/testing_support/factories/taxonomy_factory.rb
@@ -1,6 +1,14 @@
FactoryBot.define do
+ # @deprecated Taxonomy is data-only in 6.0 and dropped in 6.1. Retained for the
+ # upgrade-path specs. The root category is created here because the model no
+ # longer manages one.
factory :taxonomy, class: Spree::Taxonomy do
sequence(:name) { |n| "taxonomy_#{n}" }
store { Spree::Store.default }
+
+ after(:create) do |taxonomy|
+ Spree::Category.create!(taxonomy: taxonomy, store: taxonomy.store, name: taxonomy.name)
+ taxonomy.reload
+ end
end
end
diff --git a/spree/core/lib/tasks/categories_and_collections.rake b/spree/core/lib/tasks/categories_and_collections.rake
index 3467acb8fc..81a7c3fa98 100644
--- a/spree/core/lib/tasks/categories_and_collections.rake
+++ b/spree/core/lib/tasks/categories_and_collections.rake
@@ -1,3 +1,5 @@
+require 'spree/category_permalink_deduplicator'
+
namespace :spree do
# Migrates rule-based (automatic) categories to Spree::Collection and rewrites the
# class-name strings that Phase 3 renamed. Idempotent (each automatic category is
@@ -133,21 +135,32 @@ namespace :spree do
# categories are store-owned; Taxonomy + the taxonomy_id column drop in 6.1.
puts 'Severing taxonomy links from surviving categories...'
- # Backfill store_id from the taxonomy first so nulling taxonomy_id never orphans a
- # category (belt-and-suspenders alongside spree:taxons:backfill_store_id).
- Spree::Taxonomy.find_each do |taxonomy|
- Spree::Category.unscoped.where(store_id: nil, taxonomy_id: taxonomy.id).
- update_all(store_id: taxonomy.store_id)
- end
-
# Drop legacy taxonomy roots (parentless, taxonomy-backed containers) left with no
# children after the automatic migration; non-empty roots stay as top-level categories.
+ # Runs BEFORE the store_id backfill so throwaway roots can't trip the store-scoped
+ # unique index on their way out.
parent_ids = Spree::Category.unscoped.where.not(parent_id: nil).distinct.pluck(:parent_id)
childless_root_ids = Spree::Category.unscoped.where.not(taxonomy_id: nil).
where(parent_id: nil).where.not(id: parent_ids).ids
Spree::Category.unscoped.where(id: childless_root_ids).find_each(&:destroy!)
puts " dropped #{childless_root_ids.size} childless taxonomy roots."
+ # Normally a no-op: the migration deduplicates over this same row set (it too
+ # groups by the store a category belongs to *or resolves through its
+ # taxonomy*). It runs again because db:migrate is not part of the upgrade
+ # manifest — the deploy pipeline owns it — so this task can be invoked against
+ # a database whose migrations have not run, where the backfill below would
+ # otherwise trip over duplicates that nothing has resolved yet.
+ renamed = Spree::CategoryPermalinkDeduplicator.new.call
+ puts " resolved #{renamed} duplicate category permalinks." if renamed.positive?
+
+ # Backfill store_id from the taxonomy so nulling taxonomy_id never orphans a
+ # category (belt-and-suspenders alongside spree:taxons:backfill_store_id).
+ Spree::Taxonomy.find_each do |taxonomy|
+ Spree::Category.unscoped.where(store_id: nil, taxonomy_id: taxonomy.id).
+ update_all(store_id: taxonomy.store_id)
+ end
+
# Null the taxonomy link on everything that remains — categories are store-owned now.
severed = Spree::Category.unscoped.where.not(taxonomy_id: nil).update_all(taxonomy_id: nil)
puts " severed taxonomy_id on #{severed} categories."
diff --git a/spree/core/spec/helpers/base_helper_spec.rb b/spree/core/spec/helpers/base_helper_spec.rb
index 3d32677fa1..2be23ed043 100644
--- a/spree/core/spec/helpers/base_helper_spec.rb
+++ b/spree/core/spec/helpers/base_helper_spec.rb
@@ -12,7 +12,7 @@ describe Spree::BaseHelper, type: :helper do
describe '#spree_storefront_resource_url' do
let!(:store) { @default_store }
- let!(:taxon) { create(:taxon) }
+ let!(:category) { create(:category) }
let!(:product) { create(:product) }
before do
@@ -59,10 +59,10 @@ describe Spree::BaseHelper, type: :helper do
end
context 'for Taxon URL' do
- it { expect(helper.spree_storefront_resource_url(taxon)).to eq("http://www.example.com/t/#{taxon.permalink}") }
+ it { expect(helper.spree_storefront_resource_url(category)).to eq("http://www.example.com/t/#{category.permalink}") }
context 'when a locale is passed' do
- it { expect(helper.spree_storefront_resource_url(taxon, locale: :de)).to eq("http://www.example.com/de/t/#{taxon.permalink}") }
+ it { expect(helper.spree_storefront_resource_url(category, locale: :de)).to eq("http://www.example.com/de/t/#{category.permalink}") }
end
context 'when locale_param is present' do
@@ -70,7 +70,7 @@ describe Spree::BaseHelper, type: :helper do
allow(helper).to receive(:locale_param).and_return(:fr)
end
- it { expect(helper.spree_storefront_resource_url(taxon)).to eq("http://www.example.com/fr/t/#{taxon.permalink}") }
+ it { expect(helper.spree_storefront_resource_url(category)).to eq("http://www.example.com/fr/t/#{category.permalink}") }
end
end
end
diff --git a/spree/core/spec/jobs/spree/imports/create_categories_job_spec.rb b/spree/core/spec/jobs/spree/imports/create_categories_job_spec.rb
index d3a91aac7c..29208666da 100644
--- a/spree/core/spec/jobs/spree/imports/create_categories_job_spec.rb
+++ b/spree/core/spec/jobs/spree/imports/create_categories_job_spec.rb
@@ -5,36 +5,43 @@ RSpec.describe Spree::Imports::CreateCategoriesJob, type: :job do
let!(:product) { create(:product) }
describe '#perform' do
- it 'creates taxonomies and taxons and assigns them to the product' do
+ it 'creates categories and assigns them to the product' do
described_class.perform_now(product.id, store.id, ['Men -> Clothing -> Shirts', 'Brands -> Nike'])
product.reload
- expect(product.taxons.map(&:pretty_name)).to contain_exactly(
+ expect(product.categories.map(&:pretty_name)).to contain_exactly(
'Men -> Clothing -> Shirts',
'Brands -> Nike'
)
- men_taxonomy = store.taxonomies.find_by(name: 'Men')
- expect(men_taxonomy).to be_present
- expect(men_taxonomy.taxons.find_by(name: 'Clothing')).to be_present
- expect(men_taxonomy.taxons.find_by(name: 'Shirts')).to be_present
+ men = store.categories.find_by(name: 'Men')
+ expect(men).to be_present
+ expect(men.parent).to be_nil
+ expect(store.categories.find_by(name: 'Clothing').parent).to eq(men)
+ expect(store.categories.find_by(name: 'Shirts').parent).to eq(store.categories.find_by(name: 'Clothing'))
- brands_taxonomy = store.taxonomies.find_by(name: 'Brands')
- expect(brands_taxonomy).to be_present
- expect(brands_taxonomy.taxons.find_by(name: 'Nike')).to be_present
+ brands = store.categories.find_by(name: 'Brands')
+ expect(brands).to be_present
+ expect(store.categories.find_by(name: 'Nike').parent).to eq(brands)
+ end
+
+ it 'does not create taxonomies' do
+ expect {
+ described_class.perform_now(product.id, store.id, ['Men -> Clothing -> Shirts'])
+ }.not_to change { Spree::Taxonomy.count }
end
it 'skips invalid category paths' do
described_class.perform_now(product.id, store.id, ['Men -> -> Shirts', ' -> ', ' '])
- expect(product.reload.taxons.map(&:pretty_name)).to contain_exactly('Men -> Shirts')
+ expect(product.reload.categories.map(&:pretty_name)).to contain_exactly('Men -> Shirts')
end
it 'is idempotent' do
described_class.perform_now(product.id, store.id, ['Men -> Clothing'])
described_class.perform_now(product.id, store.id, ['Men -> Clothing'])
- expect(product.reload.taxons.map(&:pretty_name)).to contain_exactly('Men -> Clothing')
+ expect(product.reload.categories.map(&:pretty_name)).to contain_exactly('Men -> Clothing')
end
context "when the store's default locale differs from I18n.default_locale" do
@@ -46,60 +53,71 @@ RSpec.describe Spree::Imports::CreateCategoriesJob, type: :job do
described_class.perform_now(product.id, store.id, ['Men -> Clothing'])
end
- taxon = product.reload.taxons.first
- expect(taxon).to be_present
+ category = product.reload.categories.first
+ expect(category).to be_present
# The raw NOT NULL base columns (not the translation) must be populated.
- expect(taxon.read_attribute(:name)).to eq('Clothing')
- expect(taxon.taxonomy.read_attribute(:name)).to eq('Men')
+ expect(category.read_attribute(:name)).to eq('Clothing')
+ expect(category.parent.read_attribute(:name)).to eq('Men')
end
- it "keeps Spree::Taxon's Mobility.with_locale(nil) permalink regeneration working" do
+ it "keeps Spree::Category's Mobility.with_locale(nil) permalink regeneration working" do
I18n.with_locale(:de) do
described_class.perform_now(product.id, store.id, ['Men -> Clothing'])
end
- taxon = product.reload.taxons.first
- expect(taxon.read_attribute(:permalink)).to eq('men/clothing')
- expect(taxon.read_attribute(:pretty_name)).to eq('Men -> Clothing')
+ category = product.reload.categories.first
+ expect(category.read_attribute(:permalink)).to eq('men/clothing')
+ expect(category.read_attribute(:pretty_name)).to eq('Men -> Clothing')
end
end
- context 'when taxonomies and taxons already exist' do
- let!(:men_taxonomy) { create(:taxonomy, name: 'Men', store: store) }
- let!(:clothing_taxon) { create(:taxon, name: 'Clothing', taxonomy: men_taxonomy, parent: men_taxonomy.root) }
- let!(:shirts_taxon) { create(:taxon, name: 'Shirts', taxonomy: men_taxonomy, parent: clothing_taxon) }
+ context 'when categories already exist' do
+ let!(:men_category) { create(:category, name: 'Men', store: store, parent: nil) }
+ let!(:clothing_category) { create(:category, name: 'Clothing', store: store, parent: men_category) }
+ let!(:shirts_category) { create(:category, name: 'Shirts', store: store, parent: clothing_category) }
- it 'reuses existing taxonomies and taxons' do
+ it 'reuses existing categories' do
expect {
described_class.perform_now(product.id, store.id, ['Men -> Clothing -> Shirts'])
}.not_to change { Spree::Category.count }
- expect(product.reload.taxons.map(&:pretty_name)).to contain_exactly(
+ expect(product.reload.categories.map(&:pretty_name)).to contain_exactly(
'Men -> Clothing -> Shirts'
)
end
- it 'matches taxonomies and taxons by case insensitive name' do
-
+ it 'matches categories by case insensitive name' do
described_class.perform_now(product.id, store.id, ['men -> clothing -> shirts'])
- expect(product.reload.taxons.map(&:pretty_name)).to contain_exactly(
+ expect(product.reload.categories.map(&:pretty_name)).to contain_exactly(
'Men -> Clothing -> Shirts'
)
end
context 'when given an empty list' do
before do
- product.taxons = [shirts_taxon]
+ product.categories = [shirts_category]
product.save!
end
- it 'clears existing taxons' do
+ it 'clears existing categories' do
expect {
described_class.perform_now(product.id, store.id, [])
- }.to change { product.reload.taxons.count }.from(1).to(0)
+ }.to change { product.reload.categories.count }.from(1).to(0)
end
end
end
+
+ # Uniqueness is on the permalink, so two different names that normalize to
+ # the same slug are the same category as far as the database is concerned.
+ it 'reuses a category whose name differs but whose slug matches' do
+ existing = create(:category, name: 'Foo Bar', store: store)
+
+ expect {
+ described_class.perform_now(product.id, store.id, ['Foo-Bar'])
+ }.not_to change { Spree::Category.unscoped.count }
+
+ expect(product.reload.categories).to eq([existing])
+ end
end
end
diff --git a/spree/core/spec/jobs/spree/products/touch_categories_job_spec.rb b/spree/core/spec/jobs/spree/products/touch_categories_job_spec.rb
index ee19bb3d4e..0f1c4cb8dc 100644
--- a/spree/core/spec/jobs/spree/products/touch_categories_job_spec.rb
+++ b/spree/core/spec/jobs/spree/products/touch_categories_job_spec.rb
@@ -2,26 +2,26 @@ require 'spec_helper'
describe Spree::Products::TouchCategoriesJob, type: :job do
describe '#perform' do
- subject { described_class.perform_now(category_ids, taxonomy_ids) }
+ subject { described_class.perform_now(category_ids) }
- let!(:taxonomy) { create(:taxonomy) }
- let!(:category_1) { create(:taxon, taxonomy: taxonomy) }
- let!(:category_2) { create(:taxon, taxonomy: taxonomy) }
- let!(:other_category) { create(:taxon) }
+ let!(:category_1) { create(:category) }
+ let!(:category_2) { create(:category) }
+ let!(:other_category) { create(:category) }
let(:category_ids) { [category_1.id, category_2.id] }
- let(:taxonomy_ids) { [taxonomy.id] }
it 'touches all specified categories' do
expect { subject }.to change { Spree::Category.where(id: category_ids).pluck(:updated_at) }
end
- it 'touches all specified taxonomies' do
- expect { subject }.to change { Spree::Taxonomy.where(id: taxonomy_ids).pluck(:updated_at) }
- end
-
it 'does not touch other categories' do
expect { subject }.not_to change { other_category.reload.updated_at }
end
+
+ it 'accepts the legacy taxonomy_ids argument from jobs enqueued before 6.0' do
+ expect {
+ described_class.perform_now(category_ids, [1, 2])
+ }.to change { Spree::Category.where(id: category_ids).pluck(:updated_at) }
+ end
end
end
diff --git a/spree/core/spec/lib/spree/category_permalink_deduplicator_spec.rb b/spree/core/spec/lib/spree/category_permalink_deduplicator_spec.rb
new file mode 100644
index 0000000000..b0c168754f
--- /dev/null
+++ b/spree/core/spec/lib/spree/category_permalink_deduplicator_spec.rb
@@ -0,0 +1,173 @@
+require 'spec_helper'
+require 'spree/category_permalink_deduplicator'
+
+RSpec.describe Spree::CategoryPermalinkDeduplicator do
+ let(:store) { @default_store }
+
+ # Rows are staged with store_id NULL — the genuine pre-backfill state of a store
+ # upgrading from 5.5. The unique index tolerates them (NULLs compare distinct),
+ # while the deduplicator still sees the collision because it resolves each
+ # category's store through its taxonomy. Dropping the index instead would issue
+ # DDL inside the example's transaction, which MySQL commits implicitly and so
+ # destroys the savepoint the suite rolls back to.
+ def stage(category, permalink)
+ Spree::Category.unscoped.where(id: category.id).update_all(permalink: permalink, store_id: nil)
+ end
+
+ it 'separates duplicate permalinks and cascades to descendants' do
+ taxonomy_a = create(:taxonomy, name: 'Shop A', store: store)
+ taxonomy_b = create(:taxonomy, name: 'Shop B', store: store)
+
+ child_a = create(:category, name: 'Kids A', taxonomy: taxonomy_a, parent: taxonomy_a.root)
+ child_b = create(:category, name: 'Kids B', taxonomy: taxonomy_b, parent: taxonomy_b.root)
+
+ # Both taxonomies own a "catalog" root — legal before 6.0, colliding after.
+ stage(taxonomy_a.root, 'catalog')
+ stage(taxonomy_b.root, 'catalog')
+ stage(child_a, 'catalog/kids-a')
+ stage(child_b, 'catalog/kids-b')
+
+ expect(described_class.new.call).to eq(1)
+
+ permalinks = Spree::Category.unscoped.pluck(:id, :permalink).to_h
+ expect(permalinks[taxonomy_a.root.id]).to eq('catalog')
+ expect(permalinks[taxonomy_b.root.id]).to eq('catalog-shop-b')
+
+ # The moved root's child follows it rather than keeping a path its parent
+ # no longer has.
+ expect(permalinks[child_b.id]).to eq('catalog-shop-b/kids-b')
+ expect(permalinks[child_a.id]).to eq('catalog/kids-a')
+ end
+
+ it 'falls back to a numeric suffix when the taxonomy-named variant is taken' do
+ taxonomy_a = create(:taxonomy, name: 'Shop A', store: store)
+ taxonomy_b = create(:taxonomy, name: 'Shop B', store: store)
+ # An unrelated category already occupies the name the suffix would produce.
+ squatter = create(:taxonomy, name: 'Squatter', store: store)
+
+ stage(taxonomy_a.root, 'catalog')
+ stage(taxonomy_b.root, 'catalog')
+ stage(squatter.root, 'catalog-shop-b')
+
+ described_class.new.call
+
+ permalinks = Spree::Category.unscoped.pluck(:permalink).compact
+ expect(permalinks.uniq.size).to eq(permalinks.size)
+ expect(permalinks).to include('catalog-shop-b-2')
+ end
+
+ it 'is idempotent' do
+ taxonomy_a = create(:taxonomy, name: 'A', store: store)
+ taxonomy_b = create(:taxonomy, name: 'B', store: store)
+ stage(taxonomy_a.root, 'dup')
+ stage(taxonomy_b.root, 'dup')
+
+ described_class.new.call
+ after_first_run = Spree::Category.unscoped.order(:id).pluck(:id, :permalink)
+
+ expect(described_class.new.call).to eq(0)
+ expect(Spree::Category.unscoped.order(:id).pluck(:id, :permalink)).to eq(after_first_run)
+ end
+
+ it 'leaves distinct permalinks untouched' do
+ taxonomy = create(:taxonomy, name: 'Solo', store: store)
+ stage(taxonomy.root, 'untouched')
+
+ expect(described_class.new.call).to eq(0)
+ expect(Spree::Category.unscoped.where(id: taxonomy.root.id).pick(:permalink)).to eq('untouched')
+ end
+
+ # The storefront resolves a category through the translations table, which has
+ # no uniqueness constraint — so a collision there survives the base-column pass
+ # and makes the localized lookup return whichever row it reaches first.
+ it 'separates colliding translated permalinks' do
+ taxonomy_a = create(:taxonomy, name: 'Shop A', store: store)
+ taxonomy_b = create(:taxonomy, name: 'Shop B', store: store)
+
+ Spree::Category::Translation.create!(spree_category_id: taxonomy_a.root.id, locale: 'pl', permalink: 'katalog')
+ Spree::Category::Translation.create!(spree_category_id: taxonomy_b.root.id, locale: 'pl', permalink: 'katalog')
+ stage(taxonomy_a.root, 'catalog')
+ stage(taxonomy_b.root, 'catalog')
+
+ described_class.new.call
+
+ translated = Spree::Category::Translation.where(locale: 'pl').pluck(:permalink)
+ expect(translated.uniq.size).to eq(translated.size)
+ end
+
+ it 'separates translated permalinks even when the base column is already unique' do
+ alpha = create(:category, name: 'Alpha', store: store)
+ beta = create(:category, name: 'Beta', store: store)
+ Spree::Category::Translation.create!(spree_category_id: alpha.id, locale: 'pl', permalink: 'dup')
+ Spree::Category::Translation.create!(spree_category_id: beta.id, locale: 'pl', permalink: 'dup')
+
+ described_class.new.call
+
+ translated = Spree::Category::Translation.where(locale: 'pl').pluck(:permalink)
+ expect(translated.uniq.size).to eq(translated.size)
+ end
+
+ it 'cascades child translations when a translated parent permalink moves' do
+ alpha = create(:category, name: 'Alpha', store: store)
+ beta = create(:category, name: 'Beta', store: store)
+ child = create(:category, name: 'Kids', parent: beta, store: store)
+
+ Spree::Category::Translation.create!(spree_category_id: alpha.id, locale: 'pl', permalink: 'katalog')
+ Spree::Category::Translation.create!(spree_category_id: beta.id, locale: 'pl', permalink: 'katalog')
+ Spree::Category::Translation.create!(spree_category_id: child.id, locale: 'pl', permalink: 'katalog/dzieci')
+
+ described_class.new.call
+
+ moved_root = Spree::Category::Translation.find_by(spree_category_id: beta.id, locale: 'pl').permalink
+ child_path = Spree::Category::Translation.find_by(spree_category_id: child.id, locale: 'pl').permalink
+
+ expect(child_path).to start_with("#{moved_root}/")
+ end
+
+ # SQL returns duplicate groups in no defined order. If a colliding child is
+ # rewritten before its colliding parent, it gets a suffix of its own and is
+ # then re-prefixed by the parent's cascade, yielding "catalog-b/kids-b".
+ it 'rewrites a parent before its children, whatever order the groups arrive in' do
+ taxonomy_a = create(:taxonomy, name: 'Shop A', store: store)
+ taxonomy_b = create(:taxonomy, name: 'Shop B', store: store)
+ kids_a = create(:category, name: 'Kids', taxonomy: taxonomy_a, parent: taxonomy_a.root)
+ kids_b = create(:category, name: 'Kids', taxonomy: taxonomy_b, parent: taxonomy_b.root)
+
+ # Parents collide, and so do their children.
+ stage(taxonomy_a.root, 'catalog')
+ stage(taxonomy_b.root, 'catalog')
+ stage(kids_a, 'catalog/kids')
+ stage(kids_b, 'catalog/kids')
+
+ described_class.new.call
+
+ permalinks = Spree::Category.unscoped.pluck(:id, :permalink).to_h
+ expect(permalinks[kids_b.id]).to eq("#{permalinks[taxonomy_b.root.id]}/kids")
+ expect(permalinks[kids_a.id]).to eq("#{permalinks[taxonomy_a.root.id]}/kids")
+
+ all = permalinks.values.compact
+ expect(all.uniq.size).to eq(all.size)
+ end
+
+ # Re-prefixing a subtree can move a descendant onto a path another row already
+ # holds. That collision is created *by* the dedup pass, so a single snapshot of
+ # the duplicate groups never sees it — and the unique index then cannot be
+ # created, which is the exact failure this class exists to prevent.
+ it 'resolves collisions its own cascade creates' do
+ taxonomy_a = create(:taxonomy, name: 'Shop A', store: store)
+ taxonomy_b = create(:taxonomy, name: 'Shop B', store: store)
+ kids = create(:category, name: 'Kids', taxonomy: taxonomy_b, parent: taxonomy_b.root)
+ squatter = create(:category, name: 'Squatter', taxonomy: taxonomy_a, parent: taxonomy_a.root)
+
+ stage(taxonomy_a.root, 'catalog')
+ stage(taxonomy_b.root, 'catalog')
+ stage(kids, 'catalog/kids')
+ # Already sits on the path the cascade will move `kids` onto.
+ stage(squatter, 'catalog-shop-b/kids')
+
+ described_class.new.call
+
+ permalinks = Spree::Category.unscoped.pluck(:permalink).compact
+ expect(permalinks.uniq.size).to eq(permalinks.size)
+ end
+end
diff --git a/spree/core/spec/lib/spree/core/controller_helpers/store_spec.rb b/spree/core/spec/lib/spree/core/controller_helpers/store_spec.rb
index a28bc31b6e..8f89f5b270 100644
--- a/spree/core/spec/lib/spree/core/controller_helpers/store_spec.rb
+++ b/spree/core/spec/lib/spree/core/controller_helpers/store_spec.rb
@@ -62,7 +62,7 @@ describe Spree::Core::ControllerHelpers::Store, type: :controller do
before { allow(controller).to receive(:current_store).and_return(store) }
context 'when no store is present' do
- object = Spree::Taxonomy.new
+ object = Spree::Collection.new
it 'sets the current_store' do
controller.ensure_current_store(object)
@@ -72,7 +72,7 @@ describe Spree::Core::ControllerHelpers::Store, type: :controller do
end
context 'when an object already has a store assigned' do
- object = Spree::Taxonomy.new
+ object = Spree::Collection.new
it 'raises an exception' do
object.store = store_2
@@ -83,7 +83,7 @@ describe Spree::Core::ControllerHelpers::Store, type: :controller do
end
context 'when an object already has a store assigned and the same store is re-assigned' do
- object = Spree::Taxonomy.new
+ object = Spree::Collection.new
it 'no exception is raised' do
object.store = store
diff --git a/spree/core/spec/lib/tasks/categories_and_collections_spec.rb b/spree/core/spec/lib/tasks/categories_and_collections_spec.rb
index 0e80ffe2e9..5a24bef344 100644
--- a/spree/core/spec/lib/tasks/categories_and_collections_spec.rb
+++ b/spree/core/spec/lib/tasks/categories_and_collections_spec.rb
@@ -18,12 +18,12 @@ describe 'spree:migrate_taxons_to_categories_and_collections' do
describe 'automatic categories -> collections' do
let(:taxonomy) { create(:taxonomy, store: store) }
let!(:category) do
- create(:automatic_taxon, taxonomy: taxonomy, store: store, name: 'On Sale',
- sort_order: 'price asc', rules_match_policy: 'all')
+ create(:category, taxonomy: taxonomy, store: store, name: 'On Sale', automatic: true,
+ sort_order: 'price asc', rules_match_policy: 'all')
end
let!(:rule) { create(:tag_taxon_rule, taxon: category, value: 'sale', match_policy: 'is_equal_to') }
let!(:product) { create(:product, store: store) }
- let!(:membership) { Spree::ProductCategory.create!(category: category, product: product, position: 1) }
+ let!(:membership) { create(:product_category, category: category, product: product, position: 1) }
it 'creates a Collection mirroring the category and deletes the category' do
permalink = category[:permalink]
@@ -50,7 +50,8 @@ describe 'spree:migrate_taxons_to_categories_and_collections' do
describe 'manual categories' do
let(:taxonomy) { create(:taxonomy, store: store) }
- let!(:category) { create(:taxon, taxonomy: taxonomy, store: store) }
+ # A child of the root, so the root survives the childless-root sweep.
+ let!(:category) { create(:category, taxonomy: taxonomy, store: store, parent: taxonomy.root) }
it 'keeps them as store-owned categories, severs taxonomy_id, creates no collection' do
root = taxonomy.root
@@ -97,7 +98,7 @@ describe 'spree:migrate_taxons_to_categories_and_collections' do
describe 'ActionText descriptions on surviving categories' do
let(:taxonomy) { create(:taxonomy, store: store) }
let!(:category) do
- create(:taxon, taxonomy: taxonomy, store: store).tap do |c|
+ create(:category, taxonomy: taxonomy, store: store, parent: taxonomy.root).tap do |c|
c.update!(description: '<div>Kept description</div>')
# simulate a pre-6.0 row still typed as Spree::Taxon
ActionText::RichText.where(record_id: c.id, name: 'description').update_all(record_type: 'Spree::Taxon')
@@ -113,7 +114,7 @@ describe 'spree:migrate_taxons_to_categories_and_collections' do
describe 'idempotency' do
let(:taxonomy) { create(:taxonomy, store: store) }
- let!(:category) { create(:automatic_taxon, taxonomy: taxonomy, store: store) }
+ let!(:category) { create(:category, taxonomy: taxonomy, store: store, name: 'On Sale', automatic: true) }
let!(:rule) { create(:tag_taxon_rule, taxon: category, value: 'sale', match_policy: 'is_equal_to') }
it 'is safe to run twice without duplicating collections' do
@@ -124,4 +125,70 @@ describe 'spree:migrate_taxons_to_categories_and_collections' do
expect(Spree::Collection.where(store: store).count).to eq(1)
end
end
+
+ # Pre-6.0 permalinks were unique per taxonomy, so one store could hold two
+ # identical permalinks under different taxonomies. In 6.0 the permalink is the
+ # category's full path and must be unique per store — without disambiguation the
+ # backfill trips the unique index and aborts the task, leaving a half-migrated store.
+ describe 'colliding permalinks' do
+ # Builds +count+ taxonomies whose roots share a permalink, each keeping a child
+ # so the childless-root sweep can't quietly resolve the collision for us.
+ def build_colliding_roots(count, permalink: 'catalog')
+ taxonomies = Array.new(count) { |i| create(:taxonomy, name: "Shop #{i}", store: store) }
+
+ taxonomies.each_with_index do |taxonomy, index|
+ create(:category, name: "Child #{index}", taxonomy: taxonomy, parent: taxonomy.root)
+ end
+
+ # Clear store_id first — that is the genuine pre-upgrade state, and it is what
+ # keeps these rows out of the store-scoped index while we set up the collision.
+ Spree::Category.unscoped.update_all(store_id: nil)
+
+ taxonomies.each do |taxonomy|
+ Spree::Category.unscoped.where(id: taxonomy.root.id).update_all(permalink: permalink)
+ end
+
+ taxonomies
+ end
+
+ def permalinks
+ Spree::Category.unscoped.pluck(:permalink).compact
+ end
+
+ it 'completes instead of aborting on the store-scoped unique index' do
+ build_colliding_roots(2)
+
+ expect { subject.invoke }.not_to raise_error
+ expect(permalinks).to include('catalog', 'catalog-shop-1')
+ end
+
+ it 'gives every category a distinct permalink when more than two collide' do
+ build_colliding_roots(3)
+ subject.invoke
+
+ expect(permalinks.uniq.size).to eq(permalinks.size)
+ end
+
+ it 'leaves categories with distinct permalinks alone' do
+ taxonomy = create(:taxonomy, name: 'Solo', store: store)
+ # A child keeps the root alive past the childless-root sweep.
+ create(:category, name: 'Child', taxonomy: taxonomy, parent: taxonomy.root)
+ Spree::Category.unscoped.where(id: taxonomy.root.id).update_all(permalink: 'untouched')
+
+ subject.invoke
+
+ expect(permalinks).to include('untouched')
+ end
+
+ it 'leaves the rewritten permalinks untouched on a second run' do
+ build_colliding_roots(2)
+ subject.invoke
+ snapshot = Spree::Category.unscoped.order(:id).pluck(:id, :name, :permalink, :store_id)
+
+ subject.reenable
+ subject.invoke
+
+ expect(Spree::Category.unscoped.order(:id).pluck(:id, :name, :permalink, :store_id)).to eq(snapshot)
+ end
+ end
end
diff --git a/spree/core/spec/lib/tasks/taxons_spec.rb b/spree/core/spec/lib/tasks/taxons_spec.rb
index 821bacf29c..5b4a47208d 100644
--- a/spree/core/spec/lib/tasks/taxons_spec.rb
+++ b/spree/core/spec/lib/tasks/taxons_spec.rb
@@ -16,27 +16,35 @@ describe 'spree:taxons:backfill_store_id' do
let!(:store) { Spree::Store.default || create(:store, default: true) }
# ensure_store stamps store_id on create, so null it to reproduce a row
- # created before the column existed.
- def downgrade!(taxon)
- taxon.update_columns(store_id: nil)
- taxon
+ # created before the column existed. Build the tree first and downgrade
+ # afterwards: store_id scopes the nested set, so a child cannot be created
+ # under a parent whose store has already been cleared.
+ def downgrade!(*categories)
+ Spree::Category.unscoped.where(id: categories.map(&:id)).update_all(store_id: nil)
+ categories.each(&:reload)
+ categories.one? ? categories.first : categories
end
- context 'taxonomy-backed taxon' do
+ context 'taxonomy-backed category' do
let(:taxonomy) { create(:taxonomy, store: store) }
- let!(:taxon) { downgrade!(create(:taxon, taxonomy: taxonomy)) }
+ let!(:category) { downgrade!(create(:category, taxonomy: taxonomy)) }
+
it 'backfills store_id from the taxonomy' do
- expect { subject.invoke }.to change { taxon.reload.store_id }.from(nil).to(store.id)
+ expect { subject.invoke }.to change { category.reload.store_id }.from(nil).to(store.id)
end
end
context 'taxonomy-less taxons in a parent chain' do
# Root keeps its store (resolved via the taxonomy pass or pre-existing);
# descendants must inherit it down the chain.
- let!(:root) { Spree::Category.create!(name: 'Root', store: store) }
- let!(:mid) { downgrade!(Spree::Category.create!(name: 'Mid', parent: root)) }
- let!(:leaf) { downgrade!(Spree::Category.create!(name: 'Leaf', parent: mid)) }
+ let!(:root) { create(:category, name: 'Root', store: store) }
+ let!(:mid) { create(:category, name: 'Mid', parent: root) }
+ let!(:leaf) { create(:category, name: 'Leaf', parent: mid) }
+
+ # Downgrade only once the chain exists, so each child is built under a
+ # parent that still has its store.
+ before { downgrade!(mid, leaf) }
it 'resolves every level from the nearest resolved ancestor' do
subject.invoke
@@ -53,9 +61,9 @@ describe 'spree:taxons:backfill_store_id' do
end
context 'when nothing needs backfilling' do
- # Every taxon already has a store_id — the task must still run cleanly.
+ # Every category already has a store_id — the task must still run cleanly.
# The buggy join-update raised at SQL-parse time even with zero matching rows.
- let!(:taxon) { create(:taxon, taxonomy: create(:taxonomy, store: store)) }
+ let!(:category) { create(:category, taxonomy: create(:taxonomy, store: store)) }
it 'is a safe no-op' do
expect { subject.invoke }.not_to raise_error
@@ -67,7 +75,7 @@ describe 'spree:taxons:backfill_store_id' do
# resolve it against, so the row is intentionally left untouched rather than
# defaulted (ensure_store's current-store fallback needs request context the
# task doesn't have).
- let!(:orphan) { downgrade!(Spree::Category.create!(name: 'Orphan', store: store)) }
+ let!(:orphan) { downgrade!(create(:category, name: 'Orphan', store: store)) }
it 'leaves store_id nil' do
expect { subject.invoke }.not_to change { orphan.reload.store_id }.from(nil)
diff --git a/spree/core/spec/models/spree/ability_spec.rb b/spree/core/spec/models/spree/ability_spec.rb
index 6dcd4e362a..132e1f0412 100644
--- a/spree/core/spec/models/spree/ability_spec.rb
+++ b/spree/core/spec/models/spree/ability_spec.rb
@@ -181,14 +181,6 @@ describe Spree::Ability, type: :model do
end
end
- context 'for Taxonomy' do
- let(:resource) { Spree::Taxonomy.new }
-
- context 'requested by any user' do
- it_behaves_like 'read only'
- end
- end
-
context 'for User' do
context 'requested by same user' do
let(:resource) { user }
diff --git a/spree/core/spec/models/spree/category_spec.rb b/spree/core/spec/models/spree/category_spec.rb
index 2fc14f850a..dec11edded 100644
--- a/spree/core/spec/models/spree/category_spec.rb
+++ b/spree/core/spec/models/spree/category_spec.rb
@@ -3,20 +3,79 @@ require 'spec_helper'
RSpec.describe Spree::Category, type: :model do
let(:store) { @default_store }
- describe 'uniqueness across stores' do
- it 'allows the same top-level name/permalink in different stores' do
+ # The permalink is the category's full path, so it alone identifies a category
+ # within a store. Names are unconstrained — the path disambiguates them.
+ describe 'permalink uniqueness' do
+ it 'allows the same permalink in different stores' do
other = create(:store)
described_class.create!(name: 'Shoes', store: store)
- duplicate = described_class.new(name: 'Shoes', store: other)
- expect(duplicate).to be_valid
+ expect(described_class.new(name: 'Shoes', store: other)).to be_valid
end
- it 'still rejects a duplicate name within the same store' do
+ it 'rejects a duplicate permalink within the same store' do
described_class.create!(name: 'Shoes', store: store)
- duplicate = described_class.new(name: 'Shoes', store: store)
+
+ expect(described_class.new(name: 'Shoes', store: store)).not_to be_valid
+ end
+
+ # Guards the index itself, not the validation: top-level categories have
+ # parent_id IS NULL, and the previous (permalink, parent_id, taxonomy_id)
+ # index left them unconstrained because unique indexes treat NULLs as distinct.
+ it 'enforces it at the database level, including for top-level categories' do
+ described_class.create!(name: 'Shoes', store: store)
+ duplicate = described_class.new(name: 'Boots', permalink: 'shoes', store: store)
+
+ expect { duplicate.save!(validate: false) }.to raise_error(ActiveRecord::RecordNotUnique)
+ end
+
+ it 'allows the same name under different parents, since the paths differ' do
+ men = described_class.create!(name: 'Men', store: store)
+ women = described_class.create!(name: 'Women', store: store)
+ described_class.create!(name: 'Shoes', parent: men, store: store)
+
+ sibling = described_class.new(name: 'Shoes', parent: women, store: store)
+
+ expect(sibling).to be_valid
+ expect { sibling.save! }.not_to raise_error
+ expect(sibling.reload.permalink).to eq('women/shoes')
+ end
+
+ # The model validation is case-insensitive while the database index is not,
+ # so a case variant has to be caught before it reaches the index.
+ it 'rejects a permalink that differs only by case' do
+ described_class.create!(name: 'Shoes', store: store)
+ duplicate = described_class.new(name: 'Other', permalink: 'SHOES', store: store)
expect(duplicate).not_to be_valid
+ expect(duplicate.errors[:permalink]).to be_present
+ end
+
+ it 'generates lowercase permalinks, so mixed-case names cannot collide by case alone' do
+ expect(described_class.create!(name: 'Mixed Case Name', store: store).permalink).to eq('mixed-case-name')
+ end
+
+ it 'rejects two categories sharing a parent and a name, whose paths collide' do
+ men = described_class.create!(name: 'Men', store: store)
+ described_class.create!(name: 'Shoes', parent: men, store: store)
+
+ expect(described_class.new(name: 'Shoes', parent: men, store: store)).not_to be_valid
+ end
+ end
+
+ describe 'parent store boundary' do
+ it 'rejects a parent owned by another store' do
+ foreign_parent = described_class.create!(name: 'Foreign', store: create(:store))
+ child = described_class.new(name: 'Child', store: store, parent: foreign_parent)
+
+ expect(child).not_to be_valid
+ expect(child.errors[:parent]).to include('must belong to the same store')
+ end
+
+ it 'accepts a parent in the same store' do
+ parent = described_class.create!(name: 'Local', store: store)
+
+ expect(described_class.new(name: 'Child', store: store, parent: parent)).to be_valid
end
end
@@ -24,7 +83,7 @@ RSpec.describe Spree::Category, type: :model do
it 'decrements ancestors when a subcategory is destroyed' do
parent = described_class.create!(name: 'Electronics', store: store)
child = described_class.create!(name: 'Phones', parent: parent)
- Spree::ProductCategory.create!(taxon: child, product: create(:product, store: store))
+ create(:product_category, category: child, product: create(:product, store: store))
expect(parent.reload.products_count).to eq(1)
child.destroy
@@ -36,8 +95,8 @@ RSpec.describe Spree::Category, type: :model do
parent = described_class.create!(name: 'Electronics', store: store)
phones = described_class.create!(name: 'Phones', parent: parent)
laptops = described_class.create!(name: 'Laptops', parent: parent)
- Spree::ProductCategory.create!(taxon: phones, product: create(:product, store: store))
- Spree::ProductCategory.create!(taxon: laptops, product: create(:product, store: store))
+ create(:product_category, category: phones, product: create(:product, store: store))
+ create(:product_category, category: laptops, product: create(:product, store: store))
expect(parent.reload.products_count).to eq(2)
phones.destroy
@@ -49,7 +108,7 @@ RSpec.describe Spree::Category, type: :model do
root = described_class.create!(name: 'Root', store: store)
mid = described_class.create!(name: 'Mid', parent: root)
leaf = described_class.create!(name: 'Leaf', parent: mid)
- Spree::ProductCategory.create!(taxon: leaf, product: create(:product, store: store))
+ create(:product_category, category: leaf, product: create(:product, store: store))
expect(root.reload.products_count).to eq(1)
mid.destroy # removes the mid + leaf subtree
@@ -62,8 +121,8 @@ RSpec.describe Spree::Category, type: :model do
a = described_class.create!(name: 'A', parent: root)
b = described_class.create!(name: 'B', parent: root)
product = create(:product, store: store)
- Spree::ProductCategory.create!(taxon: a, product: product)
- Spree::ProductCategory.create!(taxon: b, product: product)
+ create(:product_category, category: a, product: product)
+ create(:product_category, category: b, product: product)
expect(root.reload.products_count).to eq(1)
a.destroy
@@ -98,12 +157,6 @@ RSpec.describe Spree::Category, type: :model do
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 (#ensure_store)' do
it 'falls back to the current store when none is given' do
Spree::Current.store = store
@@ -133,7 +186,7 @@ RSpec.describe Spree::Category, type: :model do
end
describe '.for_store / .for_stores' do
- it 'finds taxonomy-less categories by store_id' do
+ it 'finds categories by store_id' do
category = described_class.create!(name: 'Kitchen', store: store)
expect(described_class.for_store(store)).to include(category)
end
@@ -204,7 +257,7 @@ RSpec.describe Spree::Category, type: :model do
it 'maintains the count on a direct Classification create/destroy' do
product = create(:product, store: store)
- classification = Spree::ProductCategory.create!(taxon: phones, product: product)
+ classification = create(:product_category, category: phones, product: product)
expect(electronics.reload.products_count).to eq(1)
classification.destroy
@@ -222,4 +275,539 @@ RSpec.describe Spree::Category, type: :model do
expect(other_root.reload.products_count).to eq(1) # gained it
end
end
+ let(:store) { @default_store }
+ let(:category) { build(:category, name: 'Ruby on Rails', parent: nil) }
+
+ it_behaves_like 'metadata'
+
+ describe '#to_param' do
+ it 'is the permalink, so category URLs use the full path' do
+ category = create(:category, name: 'Ruby on Rails')
+
+ expect(category.to_param).to eq(category.permalink)
+ expect(category.to_param).to eq('ruby-on-rails')
+ end
+ end
+
+
+ context 'Scopes' do
+ describe '.with_matching_name' do
+ let!(:category1) { create(:category, name: 'shoes') }
+ let!(:category2) { create(:category, name: 'Premium Shoes') }
+
+ it 'returns the category with the matching name', :aggregate_failures do
+ expect(described_class.with_matching_name('SHOES')).to eq([category1])
+ expect(described_class.with_matching_name('Shoes')).to eq([category1])
+ expect(described_class.with_matching_name('shoes')).to eq([category1])
+
+ expect(described_class.with_matching_name('premium SHOES')).to eq([category2])
+ expect(described_class.with_matching_name('Premium shoes')).to eq([category2])
+ expect(described_class.with_matching_name('premium shoes')).to eq([category2])
+ end
+
+ context 'with translations' do
+ before do
+ I18n.with_locale(:pl) do
+ category1.update!(name: 'Buty')
+ category2.update!(name: 'Buty Premium')
+ end
+ end
+
+ it 'returns the category with the matching name', :aggregate_failures do
+ I18n.with_locale(:pl) do
+ expect(described_class.with_matching_name('BUTY')).to eq([category1])
+ expect(described_class.with_matching_name('Buty')).to eq([category1])
+ expect(described_class.with_matching_name('buty')).to eq([category1])
+
+ expect(described_class.with_matching_name('Buty PREMIUM')).to eq([category2])
+ expect(described_class.with_matching_name('Buty premium')).to eq([category2])
+ expect(described_class.with_matching_name('buty premium')).to eq([category2])
+ end
+ end
+ end
+ end
+ end
+
+ context 'when using another locale' do
+ before do
+ root_category = create(:category, name: 'EN parent', store: store)
+ category.update!(name: 'EN name', parent: root_category)
+
+ Mobility.with_locale(:pl) do
+ root_category.update!(name: 'PL parent')
+
+ category.update!(
+ name: 'PL name',
+ description: 'PL description'
+ )
+ end
+
+ category.reload
+ end
+
+ let(:category_pl_translation) { category.translations.find_by(locale: 'pl') }
+
+ it 'translates category fields' do
+ expect(category.name).to eq('EN name')
+
+ expect(category_pl_translation).to be_present
+ expect(category_pl_translation.name).to eq('PL name')
+ expect(category_pl_translation.permalink).to eq('pl-parent/pl-name')
+
+ expect(category.description_pl.to_plain_text).to eq('PL description')
+ end
+ end
+
+ context 'set_permalink' do
+ it 'sets permalink correctly when no parent present' do
+ category.set_permalink
+ expect(category.permalink).to eql 'ruby-on-rails'
+ end
+
+ it 'supports Chinese characters' do
+ category.name = '你好'
+ category.set_permalink
+ expect(category.permalink).to eql 'ni-hao'
+ end
+
+ it 'stores old slugs in FriendlyIds history' do
+ # Stub out the unrelated methods that cannot handle a save without an id
+ allow(subject).to receive(:set_depth!)
+ expect(subject).to receive(:create_slug)
+ subject.permalink = 'custom-slug'
+ subject.run_callbacks :save
+ end
+
+ context 'with parent category' do
+ let(:parent) { FactoryBot.build(:category, permalink: 'brands') }
+
+ before { allow(category).to receive_messages parent: parent }
+
+ it 'sets permalink correctly when category has parent' do
+ category.set_permalink
+ expect(category.permalink).to eql 'brands/ruby-on-rails'
+ end
+
+ it 'sets permalink correctly with existing permalink present' do
+ category.permalink = 'b/rubyonrails'
+ category.set_permalink
+ expect(category.permalink).to eql 'brands/rubyonrails'
+ end
+
+ it 'supports Chinese characters' do
+ category.name = '我'
+ category.set_permalink
+ expect(category.permalink).to eql 'brands/wo'
+ end
+
+ # Regression test for #3390
+ context 'setting a new node sibling position via :child_index=' do
+ let(:idx) { rand(0..100) }
+
+ before { allow(parent).to receive(:move_to_child_with_index) }
+
+ context 'category is not new' do
+ before { allow(category).to receive(:new_record?).and_return(false) }
+
+ it 'passes the desired index move_to_child_with_index of :parent ' do
+ expect(category).to receive(:move_to_child_with_index).with(parent, idx)
+
+ category.child_index = idx
+ end
+ end
+ end
+ end
+ end
+
+ # Regression test for #13395
+ describe 'permalink uniqueness after normalization' do
+ let!(:parent) { create(:category, name: 'Parent', store: store) }
+
+ it 'returns a validation error instead of raising RecordNotUnique when normalized permalink conflicts' do
+ create(:category, name: 'Foo Bar', parent: parent)
+
+ sibling = create(:category, name: 'Other', parent: parent)
+ sibling.permalink = "#{parent.permalink}/Foo Bar"
+
+ expect { sibling.save }.not_to raise_error
+ expect(sibling.errors[:permalink]).to be_present
+ end
+
+ it 'normalizes permalink before validation on update' do
+ category = create(:category, name: 'Test', parent: parent)
+ category.permalink = "#{parent.permalink}/Héllo Wörld"
+ category.valid?
+
+ expect(category.permalink).to eq("#{parent.permalink}/hello-world")
+ end
+ end
+
+ # Regression test for #2620
+ context 'creating a child node using first_or_create' do
+ let!(:parent) { create(:category, name: 'Parent', store: store) }
+
+ it 'does not error out' do
+ expect { parent.children.unscoped.where(name: 'Some name', parent_id: parent.id).first_or_create }.not_to raise_error
+ end
+ end
+
+ context 'ransackable_associations' do
+ it { expect(described_class.whitelisted_ransackable_associations).to include('parent') }
+ it { expect(described_class.whitelisted_ransackable_associations).not_to include('taxonomy') }
+ end
+
+ describe '#cached_self_and_descendants_ids' do
+ it { expect(category.cached_self_and_descendants_ids).to eq(category.self_and_descendants.ids) }
+ end
+
+ describe '#localized_slugs_for_store' do
+ let(:store) { create(:store, default_locale: 'fr', supported_locales: 'en,pl,fr') }
+ let!(:root_category) { create(:category, name: 'Categories', store: store) }
+ let(:category) { create(:category, permalink: 'test_slug_en', parent: root_category, store: store) }
+ let!(:category_translation_fr) { category.translations.create(slug: 'test_slug_fr', locale: 'fr') }
+
+ before { Spree::Locales::SetFallbackLocaleForStore.new.call(store: store) }
+
+ subject { category.localized_slugs_for_store(store) }
+
+ context 'when there are slugs in locales not supported by the store' do
+ let!(:category_translation_pl) { category.translations.create(slug: 'test_slug_pl', locale: 'pl') }
+ let!(:category_translation_de) { category.translations.create(slug: 'test_slug_de', locale: 'de') }
+
+ let(:expected_slugs) do
+ {
+ 'en' => 'categories/test-slug-en',
+ 'fr' => 'categories/test-slug-fr',
+ 'pl' => 'categories/test-slug-pl'
+ }
+ end
+
+ it 'returns only slugs in locales supported by the store' do
+ expect(subject).to match(expected_slugs)
+ end
+ end
+
+ context 'when one of the supported locales does not have a translation' do
+ let(:expected_slugs) do
+ {
+ 'en' => 'categories/test-slug-en',
+ 'fr' => 'categories/test-slug-fr',
+ 'pl' => 'categories/test-slug-fr'
+ }
+ end
+
+ it "falls back to store's default locale" do
+ expect(subject).to match(expected_slugs)
+ end
+ end
+
+ context 'when setting the slug translations for the parent' do
+ let!(:root_category_translation_pl) { root_category.translations.create(slug: 'slug with space', locale: 'pl') }
+ let!(:root_category_translation_fr) { root_category.translations.create(slug: 'categories-fr', locale: 'fr') }
+
+ let(:expected_slugs) do
+ {
+ 'en' => 'categories',
+ 'fr' => 'categories-fr',
+ 'pl' => 'slug-with-space'
+ }
+ end
+
+ it "sets the slugs in slug format" do
+ expect(root_category.reload.localized_slugs_for_store(store)).to match(expected_slugs)
+ end
+ end
+
+ context 'when setting the slugs in category under taxomony with different parent slug' do
+ let!(:root_category_translation_pl) { root_category.translations.create(slug: 'slug with space', locale: 'pl') }
+ let!(:category_translation_pl) { category.translations.create(locale: 'pl') }
+
+ let(:expected_slugs) do
+ {
+ 'en' => 'categories/test-slug-en',
+ 'fr' => 'categories/test-slug-fr',
+ 'pl' => "slug-with-space/#{category.name.to_url}"
+ }
+ end
+
+ it "sets the slug in valid format" do
+ expect(category.localized_slugs_for_store(store)).to match(expected_slugs)
+ end
+ end
+ end
+
+ describe '#regenerate_pretty_name_and_permalink' do
+ let!(:parent) { create(:category, name: 'Parent', store: store) }
+ let!(:category) { create(:category, name: 'Category#1', parent: parent, store: store) }
+
+ it 'regenerates pretty name and permalink' do
+ expect(category.pretty_name).to eq("#{category.parent.pretty_name} -> #{category.name}")
+ expect(category.permalink).to eq("#{category.parent.permalink}/#{category.name.to_url}")
+ end
+
+ context "when parent's permalink is changed" do
+ before do
+ category.parent.update!(permalink: 'new-permalink')
+ end
+
+ it 'updates the pretty name and permalink' do
+ expect(category.reload.pretty_name).to eq("#{category.parent.pretty_name} -> #{category.name}")
+ expect(category.permalink).to eq("new-permalink/#{category.name.to_url}")
+ end
+ end
+
+ context 'when parent name is changed' do
+ before do
+ category.parent.update!(name: 'New Parent')
+ end
+
+ it 'updates the pretty name and permalink' do
+ expect(category.reload.pretty_name).to eq("New Parent -> #{category.name}")
+ expect(category.permalink).to eq("#{category.parent.permalink}/#{category.name.to_url}")
+ end
+ end
+
+ context 'with translations' do
+ before do
+ Mobility.with_locale(:pl) do
+ category.update!(name: 'Kategoria#1')
+ category.reload
+
+ category.parent.update!(name: 'Kategoria')
+ end
+ end
+
+ it 'updates the pretty name and permalink for translations as well' do
+ Mobility.with_locale(:pl) do
+ expect(category.reload.pretty_name).to eq('Kategoria -> Kategoria#1')
+ expect(category.permalink).to eq('kategoria/kategoria-number-1')
+ end
+ end
+ end
+
+ context 'when category is moved' do
+ let(:parent2) { create(:category, name: 'Parent2', permalink: 'parent2', parent: parent) }
+ let(:category2) { create(:category, name: 'Child', parent: parent2, permalink: 'child') }
+
+ before do
+ category.parent.update!(name: 'Grandparent', permalink: 'grandparent')
+ category.update!(name: 'Parent', permalink: 'parent')
+
+ parent2
+ category2
+
+ Mobility.with_locale(:pl) do
+ category.parent.update!(name: 'Dziadek', permalink: 'dziadek')
+ category.update!(name: 'Rodzic')
+
+ parent2.update!(name: 'Rodzic2', permalink: 'rodzic2')
+ category2.update!(name: 'Dziecko')
+ end
+
+ expect(category.permalink).to eq('grandparent/parent')
+ expect(category.pretty_name).to eq('Grandparent -> Parent')
+
+ expect(category2.permalink).to eq('grandparent/parent2/child')
+ expect(category2.pretty_name).to eq('Grandparent -> Parent2 -> Child')
+
+ Mobility.with_locale(:pl) do
+ expect(category.reload.pretty_name).to eq('Dziadek -> Rodzic')
+ expect(category.permalink).to eq('dziadek/rodzic')
+
+ expect(category2.pretty_name).to eq('Dziadek -> Rodzic2 -> Dziecko')
+ expect(category2.permalink).to eq('dziadek/rodzic2/dziecko')
+ end
+ end
+
+ it 'updates the pretty name and permalink' do
+ category2.move_to_child_with_index(category, 0)
+
+ expect(category2.reload.pretty_name).to eq('Grandparent -> Parent -> Child')
+ expect(category2.permalink).to eq('grandparent/parent/child')
+
+ Mobility.with_locale(:pl) do
+ expect(category2.reload.pretty_name).to eq('Dziadek -> Rodzic -> Dziecko')
+ expect(category2.permalink).to eq('dziadek/rodzic/dziecko')
+ end
+ end
+
+ it 'updates the pretty name and permalink when move is done inside different locales' do
+ Mobility.with_locale(:pl) do
+ category2.move_to_child_with_index(category, 0)
+ end
+
+ expect(category2.permalink).to eq('grandparent/parent/child')
+ expect(category2.reload.pretty_name).to eq('Grandparent -> Parent -> Child')
+
+ Mobility.with_locale(:pl) do
+ expect(category2.reload.pretty_name).to eq('Dziadek -> Rodzic -> Dziecko')
+ expect(category2.permalink).to eq('dziadek/rodzic/dziecko')
+ end
+ end
+ end
+ end
+
+ describe '#pretty_name' do
+ let!(:category) { create(:category, name: 'Category#1') }
+
+ context 'top level' do
+ it 'is just the category name' do
+ expect(category.pretty_name).to eq(category.name)
+ end
+ end
+
+ context '2+ lvl deep' do
+ let(:category_parent) { create(:category, name: 'Parent') }
+
+ before do
+ category.parent = category_parent
+ category.save!
+ end
+
+ it 'returns parent name and category name' do
+ expect(category.reload.pretty_name).to eq('Parent -> Category#1')
+ end
+
+ context 'when name is updated' do
+ before do
+ category.name = 'New Name'
+ category.save!
+ end
+
+ it 'returns the updated pretty name' do
+ expect(category.reload.pretty_name).to eq('Parent -> New Name')
+ end
+ end
+
+ context 'when parent name is updated' do
+ before do
+ category_parent.name = 'New Parent'
+ category_parent.save!
+ end
+
+ it 'returns the updated pretty name' do
+ expect(category.reload.pretty_name).to eq('New Parent -> Category#1')
+ end
+ end
+ end
+
+ context 'when `always_use_translations` is disabled' do
+ before do
+ allow(Spree::Config).to receive(:always_use_translations).and_return(false)
+ end
+
+ it 'sets the pretty name' do
+ expect(category.reload.pretty_name).to eq(category.name)
+ end
+ end
+
+ context 'when `always_use_translations` is enabled' do
+ before do
+ allow(Spree::Config).to receive(:always_use_translations).and_return(true)
+ end
+
+ it 'sets the pretty name' do
+ expect(category.reload.pretty_name).to eq(category.name)
+ end
+ end
+ end
+
+ describe '#active_products_with_descendants' do
+ let(:root_category) { create(:category) }
+
+ context 'when category has products' do
+ let!(:product) { create(:product, categories: [root_category]) }
+
+ it 'returns true' do
+ expect(root_category.reload.active_products_with_descendants.exists?).to be true
+ end
+
+ it 'returns true when products aren\'t active' do
+ product.update(status: 'draft')
+
+ expect(root_category.reload.products.exists?).to be true
+ end
+ end
+
+ context 'when only children categories have products' do
+ let(:parent_category) { create(:category, parent: root_category) }
+ let(:child_category) { create(:category, parent: parent_category) }
+ let!(:product) { create(:product, categories: [child_category]) }
+
+ it 'returns true' do
+ expect(root_category.reload.active_products_with_descendants.exists?).to be true
+ end
+
+ it 'returns false when products aren\'t active' do
+ product.update(status: 'draft')
+
+ expect(root_category.reload.active_products_with_descendants.exists?).to be false
+ end
+ end
+
+ context 'when category has no products' do
+ it 'returns false' do
+ expect(root_category.reload.active_products_with_descendants.exists?).to be false
+ end
+ end
+
+ context 'when category has products but children categories have no products' do
+ let(:parent_category) { create(:category, parent: root_category) }
+ let!(:child_category) { create(:category, parent: parent_category) }
+ let!(:product) { create(:product, categories: [root_category]) }
+
+ it 'returns true' do
+ expect(root_category.reload.active_products_with_descendants.exists?).to be true
+ end
+
+ it 'returns false when products aren\'t active' do
+ product.update(status: 'draft')
+
+ expect(root_category.reload.active_products_with_descendants.exists?).to be false
+ end
+ end
+
+ context 'when children categories also have no products' do
+ let(:parent_category) { create(:category, parent: root_category) }
+ let!(:child_category) { create(:category, parent: parent_category) }
+
+ it 'returns false' do
+ expect(root_category.reload.active_products_with_descendants.exists?).to be false
+ end
+ end
+ end
+
+ # acts_as_nested_set is scoped by store, so each store owns an independent
+ # tree rather than sharing one global lft/rgt sequence.
+ describe 'per-store nested set' do
+ let(:other_store) { create(:store) }
+
+ it 'numbers each store from the start instead of continuing the previous one' do
+ create(:category, name: 'A', store: store)
+ other_root = create(:category, name: 'B', store: other_store)
+
+ expect(other_root.reload.lft).to eq(1)
+ end
+
+ it 'leaves another store untouched when a tree is reordered' do
+ first = create(:category, name: 'A1', store: store)
+ create(:category, name: 'A2', store: store)
+ other_root = create(:category, name: 'B1', store: other_store)
+ create(:category, name: 'B2', parent: other_root, store: other_store)
+
+ before_bounds = described_class.unscoped.where(store: other_store).order(:id).pluck(:lft, :rgt)
+ first.reload.move_to_root
+ after_bounds = described_class.unscoped.where(store: other_store).order(:id).pluck(:lft, :rgt)
+
+ expect(after_bounds).to eq(before_bounds)
+ end
+
+ it 'keeps subtree reads inside the owning store' do
+ root = create(:category, name: 'Root', store: store)
+ child = create(:category, name: 'Child', parent: root, store: store)
+ create(:category, name: 'Foreign', store: other_store)
+
+ expect(root.reload.self_and_descendants).to contain_exactly(root, child)
+ end
+ end
end
diff --git a/spree/core/spec/models/spree/classification_spec.rb b/spree/core/spec/models/spree/classification_spec.rb
index 6730da696f..9fd368f230 100644
--- a/spree/core/spec/models/spree/classification_spec.rb
+++ b/spree/core/spec/models/spree/classification_spec.rb
@@ -11,19 +11,19 @@ module Spree
products << create(:base_product)
end
- create(:taxon, products: products)
+ create(:category, products: products)
end
- it 'cannot link the same taxon to the same product more than once' do
+ it 'cannot link the same category to the same product more than once' do
product = create(:product)
- taxon = create(:taxon)
- expect { product.taxons << taxon }.not_to raise_error
- expect { product.taxons << taxon }.to raise_error(ActiveRecord::RecordInvalid)
+ category = create(:category)
+ expect { product.categories << category }.not_to raise_error
+ expect { product.categories << category }.to raise_error(ActiveRecord::RecordInvalid)
end
- def positions_to_be_valid(taxon)
- positions = taxon.reload.classifications.map(&:position)
- expect(positions).to eq((1..taxon.classifications.count).to_a)
+ def positions_to_be_valid(category)
+ positions = category.reload.classifications.map(&:position)
+ expect(positions).to eq((1..category.classifications.count).to_a)
end
it 'has a valid fixtures' do
@@ -31,7 +31,7 @@ module Spree
expect(Spree::ProductCategory.count).to eq 5
end
- context 'removing product from taxon' do
+ context 'removing product from category' do
before do
p = taxon_with_5_products.products[1]
expect(p.classifications.first.position).to eq(2)
@@ -43,7 +43,7 @@ module Spree
end
end
- context "replacing taxon's products" do
+ context "replacing category's products" do
before do
products = taxon_with_5_products.products.to_a
products.pop(1)
@@ -56,7 +56,7 @@ module Spree
end
end
- context 'removing taxon from product' do
+ context 'removing category from product' do
before do
p = taxon_with_5_products.products[1]
p.taxons.destroy(taxon_with_5_products)
@@ -92,74 +92,74 @@ module Spree
end
describe 'counter cache' do
- let(:taxon) { create(:taxon) }
+ let(:category) { create(:category) }
let(:product) { create(:product) }
- # The direct taxon-side classification_count was dropped in 6.0; the taxon
+ # The direct category-side classification_count was dropped in 6.0; the category
# now tracks membership via the descendant-inclusive products_count, kept in
- # sync by the Classification create/destroy callbacks (a leaf taxon's
+ # sync by the Classification create/destroy callbacks (a leaf category's
# products_count equals its direct count).
- describe 'products_count on taxon' do
+ describe 'products_count on category' do
it 'increments when a classification is created' do
expect {
- create(:classification, taxon: taxon, product: product)
- }.to change { taxon.reload.products_count }.from(0).to(1)
+ create(:product_category, category: category, product: product)
+ }.to change { category.reload.products_count }.from(0).to(1)
end
it 'decrements when a classification is destroyed' do
- classification = create(:classification, taxon: taxon, product: product)
+ classification = create(:product_category, category: category, product: product)
expect {
classification.destroy
- }.to change { taxon.reload.products_count }.from(1).to(0)
+ }.to change { category.reload.products_count }.from(1).to(0)
end
it 'correctly counts multiple classifications' do
products = create_list(:product, 3)
- products.each { |p| create(:classification, taxon: taxon, product: p) }
- expect(taxon.reload.products_count).to eq(3)
+ products.each { |p| create(:product_category, category: category, product: p) }
+ expect(category.reload.products_count).to eq(3)
end
end
describe 'categories_count on product' do
it 'increments when a classification is created' do
expect {
- create(:classification, taxon: taxon, product: product)
+ create(:product_category, category: category, product: product)
}.to change { product.reload.categories_count }.from(0).to(1)
end
it 'decrements when a classification is destroyed' do
- classification = create(:classification, taxon: taxon, product: product)
+ classification = create(:product_category, category: category, product: product)
expect {
classification.destroy
}.to change { product.reload.categories_count }.from(1).to(0)
end
it 'correctly counts multiple classifications' do
- taxons = create_list(:taxon, 3)
- taxons.each { |t| create(:classification, taxon: t, product: product) }
+ taxons = create_list(:category, 3)
+ taxons.each { |t| create(:product_category, category: t, product: product) }
expect(product.reload.categories_count).to eq(3)
end
end
end
describe '.grouped_category_ids_for_products' do
- let(:taxon1) { create(:taxon) }
- let(:taxon2) { create(:taxon) }
- let(:taxon3) { create(:taxon) }
+ let(:category1) { create(:category) }
+ let(:category2) { create(:category) }
+ let(:category3) { create(:category) }
let(:product1) { create(:product) }
let(:product2) { create(:product) }
- let!(:classification1) { create(:classification, taxon: taxon1, product: product1) }
- let!(:classification2) { create(:classification, taxon: taxon2, product: product1) }
- let!(:classification3) { create(:classification, taxon: taxon3, product: product2) }
- let(:expected_result) { [[product1.id, "#{taxon1.id},#{taxon2.id}"], [product2.id, taxon3.id.to_s]] }
+ let!(:classification1) { create(:product_category, category: category1, product: product1) }
+ let!(:classification2) { create(:product_category, category: category2, product: product1) }
+ let!(:classification3) { create(:product_category, category: category3, product: product2) }
+ let(:expected_result) { [[product1.id, "#{category1.id},#{category2.id}"], [product2.id, category3.id.to_s]] }
let(:product_ids) { [product1.id, product2.id] }
- let(:taxon_groups) { [taxon1.id, taxon2.id, taxon3.id] }
+ let(:taxon_groups) { [category1.id, category2.id, category3.id] }
- it 'returns the correct taxon ids' do
+ it 'returns the correct category ids' do
expect(described_class.grouped_category_ids_for_products(product_ids, taxon_groups)).to eq(expected_result)
end
- context 'when empty taxon groups' do
+ context 'when empty category groups' do
let(:taxon_groups) { [] }
it 'returns an empty array' do
diff --git a/spree/core/spec/models/spree/concerns/prefixed_id_spec.rb b/spree/core/spec/models/spree/concerns/prefixed_id_spec.rb
index f51e1991d1..69aea71a60 100644
--- a/spree/core/spec/models/spree/concerns/prefixed_id_spec.rb
+++ b/spree/core/spec/models/spree/concerns/prefixed_id_spec.rb
@@ -69,13 +69,13 @@ RSpec.describe Spree::PrefixedId do
end
context 'with has_many _ids setters' do
- let(:taxon1) { create(:taxon) }
- let(:taxon2) { create(:taxon, taxonomy: taxon1.taxonomy) }
+ let(:category1) { create(:category) }
+ let(:category2) { create(:category) }
it 'resolves prefixed IDs for has_many associations' do
product = create(:product)
- product.assign_attributes(taxon_ids: [taxon1.prefixed_id, taxon2.prefixed_id])
- expect(product.taxon_ids).to contain_exactly(taxon1.id, taxon2.id)
+ product.assign_attributes(category_ids: [category1.prefixed_id, category2.prefixed_id])
+ expect(product.category_ids).to contain_exactly(category1.id, category2.id)
end
it 'passes through values for non-existent associations' do
diff --git a/spree/core/spec/models/spree/permission_sets/product_management_spec.rb b/spree/core/spec/models/spree/permission_sets/product_management_spec.rb
index 6b64724c36..c385041b06 100644
--- a/spree/core/spec/models/spree/permission_sets/product_management_spec.rb
+++ b/spree/core/spec/models/spree/permission_sets/product_management_spec.rb
@@ -30,10 +30,6 @@ RSpec.describe Spree::PermissionSets::ProductManagement do
expect(ability.can?(:manage, Spree::Category)).to be true
end
- it 'grants manage access to Taxonomy' do
- expect(ability.can?(:manage, Spree::Taxonomy)).to be true
- end
-
it 'grants manage access to Classification' do
expect(ability.can?(:manage, Spree::ProductCategory)).to be true
end
diff --git a/spree/core/spec/models/spree/product/scopes_spec.rb b/spree/core/spec/models/spree/product/scopes_spec.rb
index aa9c2655e7..fc6aaf8ef8 100644
--- a/spree/core/spec/models/spree/product/scopes_spec.rb
+++ b/spree/core/spec/models/spree/product/scopes_spec.rb
@@ -40,35 +40,32 @@ describe 'Product scopes', type: :model do
context 'A product assigned to parent and child taxons' do
before do
- @taxonomy = create(:taxonomy)
- @root_taxon = @taxonomy.root
+ @parent_category = create(:category, name: 'Parent')
+ @child_category = create(:category, name: 'Child 1', parent: @parent_category)
+ @parent_category.reload # Need to reload for descendents to show up
- @parent_taxon = create(:taxon, name: 'Parent', taxonomy_id: @taxonomy.id, parent: @root_taxon)
- @child_taxon = create(:taxon, name: 'Child 1', taxonomy_id: @taxonomy.id, parent: @parent_taxon)
- @parent_taxon.reload # Need to reload for descendents to show up
-
- product.taxons << @parent_taxon
- product.taxons << @child_taxon
+ product.categories << @parent_category
+ product.categories << @child_category
end
it 'calling Product.in_taxon returns products in child taxons' do
- product.taxons -= [@child_taxon]
- expect(product.taxons.count).to eq(1)
+ product.categories -= [@child_category]
+ expect(product.categories.count).to eq(1)
- expect(Spree::Product.in_taxon(@parent_taxon)).to include(product)
+ expect(Spree::Product.in_taxon(@parent_category)).to include(product)
end
it 'calling Product.in_taxon should not return duplicate records' do
- expect(Spree::Product.in_taxon(@parent_taxon).to_a.size).to eq(1)
+ expect(Spree::Product.in_taxon(@parent_category).to_a.size).to eq(1)
end
- context 'returns correct products for taxon' do
- let(:other_taxon) { create(:taxon, products: [product]) }
- let!(:product_2) { create(:product, taxons: [@child_taxon, other_taxon]) }
+ context 'returns correct products for category' do
+ let(:other_category) { create(:category, products: [product]) }
+ let!(:product_2) { create(:product, categories: [@child_category, other_category]) }
- it 'includes all products in the taxon' do
- expect(Spree::Product.in_taxon(@child_taxon)).to include(product, product_2)
- expect(Spree::Product.in_taxon(other_taxon)).to include(product, product_2)
+ it 'includes all products in the category' do
+ expect(Spree::Product.in_taxon(@child_category)).to include(product, product_2)
+ expect(Spree::Product.in_taxon(other_category)).to include(product, product_2)
end
end
end
@@ -136,40 +133,40 @@ describe 'Product scopes', type: :model do
context '#ascend_by_taxons_min_position' do
subject(:ordered_products) { Spree::Product.ascend_by_taxons_min_position(taxons) }
- let(:taxons) { [parent_taxon, child_taxon_1, child_taxon_2, child_taxon_1_1, child_taxon_2_1] }
+ let(:taxons) { [parent_category, child_taxon_1, child_taxon_2, child_taxon_1_1, child_taxon_2_1] }
- let(:parent_taxon) { create(:taxon) }
+ let(:parent_category) { create(:category) }
- let(:child_taxon_1) { create(:taxon, parent: parent_taxon, taxonomy: parent_taxon.taxonomy) }
- let(:child_taxon_1_1) { create(:taxon, parent: child_taxon_1, taxonomy: child_taxon_1.taxonomy) }
+ let(:child_taxon_1) { create(:category, parent: parent_category) }
+ let(:child_taxon_1_1) { create(:category, parent: child_taxon_1) }
- let(:child_taxon_2) { create(:taxon, parent: parent_taxon, taxonomy: parent_taxon.taxonomy) }
- let(:child_taxon_2_1) { create(:taxon, parent: child_taxon_2,taxonomy: child_taxon_2.taxonomy) }
+ let(:child_taxon_2) { create(:category, parent: parent_category) }
+ let(:child_taxon_2_1) { create(:category, parent: child_taxon_2) }
let!(:product_1) { create(:product) }
- let!(:classification_1_1) { create(:classification, position: 5, product: product_1, taxon: parent_taxon) }
- let!(:classification_1_2) { create(:classification, position: 4, product: product_1, taxon: child_taxon_1_1) }
+ let!(:classification_1_1) { create(:product_category, position: 5, product: product_1, category: parent_category) }
+ let!(:classification_1_2) { create(:product_category, position: 4, product: product_1, category: child_taxon_1_1) }
let!(:product_2) { create(:product) }
- let!(:classification_2_1) { create(:classification, position: 1, product: product_2, taxon: parent_taxon) }
- let!(:classification_2_2) { create(:classification, position: 2, product: product_2, taxon: child_taxon_2_1) }
+ let!(:classification_2_1) { create(:product_category, position: 1, product: product_2, category: parent_category) }
+ let!(:classification_2_2) { create(:product_category, position: 2, product: product_2, category: child_taxon_2_1) }
let!(:product_3) { create(:product) }
- let!(:classification_3_1) { create(:classification, position: 3, product: product_3, taxon: child_taxon_1) }
- let!(:classification_3_2) { create(:classification, position: 4, product: product_3, taxon: child_taxon_2_1) }
+ let!(:classification_3_1) { create(:product_category, position: 3, product: product_3, category: child_taxon_1) }
+ let!(:classification_3_2) { create(:product_category, position: 4, product: product_3, category: child_taxon_2_1) }
let!(:product_4) { create(:product) }
- let!(:classification_4_1) { create(:classification, position: 2, product: product_4, taxon: child_taxon_2) }
+ let!(:classification_4_1) { create(:product_category, position: 2, product: product_4, category: child_taxon_2) }
let!(:product_5) { create(:product) }
- let!(:classification_5_1) { create(:classification, position: 1, product: product_5, taxon: child_taxon_1_1) }
+ let!(:classification_5_1) { create(:product_category, position: 1, product: product_5, category: child_taxon_1_1) }
let!(:product_6) { create(:product) }
- let!(:classification_6_1) { create(:classification, position: 6, product: product_6, taxon: child_taxon_2) }
- let!(:classification_6_2) { create(:classification, position: 3, product: product_6, taxon: child_taxon_1) }
+ let!(:classification_6_1) { create(:product_category, position: 6, product: product_6, category: child_taxon_2) }
+ let!(:classification_6_2) { create(:product_category, position: 3, product: product_6, category: child_taxon_1) }
before do
- create_list(:product, 3, taxons: [create(:taxon)])
+ create_list(:product, 3, categories: [create(:category)])
end
it 'orders products by ascending taxons minimum position' do
diff --git a/spree/core/spec/models/spree/product_spec.rb b/spree/core/spec/models/spree/product_spec.rb
index 1109846ea4..9ef3324796 100644
--- a/spree/core/spec/models/spree/product_spec.rb
+++ b/spree/core/spec/models/spree/product_spec.rb
@@ -1127,21 +1127,20 @@ describe Spree::Product, type: :model do
describe '#category_ids=' do
let(:product) { create(:product, store: store) }
- let(:taxonomy) { create(:taxonomy, store: store) }
- let(:category) { create(:taxon, taxonomy: taxonomy) }
+ let(:category) { create(:category, store: store) }
it 'assigns categories belonging to the product store' do
product.update!(category_ids: [category.prefixed_id])
- expect(product.reload.taxons).to include(category)
+ expect(product.reload.categories).to include(category)
end
- it "ignores categories from another store's taxonomy" do
- foreign_taxon = create(:taxon, taxonomy: create(:taxonomy, store: create(:store)))
+ it 'ignores categories belonging to another store' do
+ foreign_category = create(:category, store: create(:store))
- product.update!(category_ids: [category.prefixed_id, foreign_taxon.prefixed_id])
+ product.update!(category_ids: [category.prefixed_id, foreign_category.prefixed_id])
- expect(product.reload.taxons).to include(category)
- expect(product.reload.taxons).not_to include(foreign_taxon)
+ expect(product.reload.categories).to include(category)
+ expect(product.reload.categories).not_to include(foreign_category)
end
end
@@ -1197,8 +1196,7 @@ describe Spree::Product, type: :model do
describe '#primary_category' do
let(:product) { create(:product, store: store) }
- let(:taxonomy) { create(:taxonomy, store: store) }
- let(:category) { create(:taxon, taxonomy: taxonomy) }
+ let(:category) { create(:category) }
it 'returns the first assigned category' do
product.update!(category_ids: [category.prefixed_id])
@@ -1319,10 +1317,10 @@ describe Spree::Product, type: :model do
describe '#to_csv' do
let(:store) { Spree::Store.default }
let(:product) { create(:product) }
- let(:taxon) { create(:taxon, name: 'My Taxon') }
+ let(:category) { create(:category, name: 'My Taxon') }
before do
- product.taxons << taxon
+ product.categories << category
end
context 'when product has no variants' do
@@ -1883,28 +1881,25 @@ describe Spree::Product, type: :model do
describe 'after_touch :touch_categories' do
subject { product.touch }
- let!(:product) { create(:product, taxons: taxons) }
+ let!(:product) { create(:product, categories: categories) }
- context 'without taxons' do
- let(:taxons) { [] }
+ context 'without categories' do
+ let(:categories) { [] }
- it 'skips enqueuing a job for touching the taxons' do
+ it 'skips enqueuing a job for touching the categories' do
expect { subject }.not_to have_enqueued_job(Spree::Products::TouchCategoriesJob)
end
end
- context 'with taxons' do
- let(:taxons) { [taxon_1, taxon_2] }
-
- let!(:taxon_1) { create(:taxon, taxonomy: taxonomy, parent: taxonomy.root) }
- let!(:taxon_2) { create(:taxon, taxonomy: taxonomy, parent: taxonomy.root) }
+ context 'with categories' do
+ let(:categories) { [child_category] }
- let(:taxonomy) { create(:taxonomy) }
+ let!(:parent_category) { create(:category) }
+ let!(:child_category) { create(:category, parent: parent_category) }
- it 'enqueues a job for touching the taxons' do
+ it 'enqueues a job for touching the categories and their ancestors' do
expect { subject }.to have_enqueued_job(Spree::Products::TouchCategoriesJob).with(
- [taxonomy.root.id, taxon_1.id, taxon_2.id],
- [taxonomy.id]
+ [parent_category.id, child_category.id]
)
end
end
diff --git a/spree/core/spec/models/spree/promotion/rules/taxon_spec.rb b/spree/core/spec/models/spree/promotion/rules/category_spec.rb
similarity index 65%
rename from spree/core/spec/models/spree/promotion/rules/taxon_spec.rb
rename to spree/core/spec/models/spree/promotion/rules/category_spec.rb
index db016c6132..cde03a8ab1 100644
--- a/spree/core/spec/models/spree/promotion/rules/taxon_spec.rb
+++ b/spree/core/spec/models/spree/promotion/rules/category_spec.rb
@@ -6,9 +6,8 @@ describe Spree::Promotion::Rules::Category, type: :model do
let(:rule) { create(:promotion_rule_taxon, promotion: promotion) }
describe '#eligible?(order)' do
- let(:taxonomy) { create(:taxonomy, store: store) }
- let(:taxon) { create :taxon, name: 'first', taxonomy: taxonomy }
- let(:taxon2) { create :taxon, name: 'second', taxonomy: taxonomy }
+ let(:category) { create :category, name: 'first' }
+ let(:category2) { create :category, name: 'second' }
let(:order) { create :order_with_line_items, store: store }
context 'with any match policy' do
@@ -16,30 +15,30 @@ describe Spree::Promotion::Rules::Category, type: :model do
rule.preferred_match_policy = 'any'
end
- it 'is eligible if order does has any preferred taxon' do
- order.products.first.taxons << taxon
- rule.taxons << taxon
+ it 'is eligible if order does has any preferred category' do
+ order.products.first.categories << category
+ rule.categories << category
expect(rule).to be_eligible(order)
end
context 'when order contains items from different taxons' do
before do
- order.products.first.taxons << taxon
- rule.taxons << taxon
+ order.products.first.categories << category
+ rule.categories << category
end
- it 'acts on a product within the eligible taxon' do
+ it 'acts on a product within the eligible category' do
expect(rule).to be_actionable(order.line_items.last)
end
- it 'does not act on a product in another taxon' do
- order.line_items << create(:line_item, product: create(:product, taxons: [taxon2]))
+ it 'does not act on a product in another category' do
+ order.line_items << create(:line_item, product: create(:product, categories: [category2]))
expect(rule).not_to be_actionable(order.line_items.last)
end
end
- context 'when order does not have any preferred taxon' do
- before { rule.taxons << taxon2 }
+ context 'when order does not have any preferred category' do
+ before { rule.categories << category2 }
it { expect(rule).not_to be_eligible(order) }
it 'sets an error message' do
@@ -49,11 +48,11 @@ describe Spree::Promotion::Rules::Category, type: :model do
end
end
- context 'when a product has a taxon child of a taxon rule' do
+ context 'when a product has a category child of a category rule' do
before do
- taxon.children << taxon2
- order.products.first.taxons << taxon2
- rule.taxons << taxon2
+ category.children << category2
+ order.products.first.categories << category2
+ rule.categories << category2
end
it { expect(rule).to be_eligible(order) }
@@ -66,16 +65,16 @@ describe Spree::Promotion::Rules::Category, type: :model do
end
it 'is eligible order has all preferred taxons' do
- order.products.first.taxons << taxon2
- order.products.last.taxons << taxon
+ order.products.first.categories << category2
+ order.products.last.categories << category
- rule.taxons = [taxon, taxon2]
+ rule.taxons = [category, category2]
expect(rule).to be_eligible(order)
end
context 'when order does not have all preferred taxons' do
- before { rule.taxons << taxon }
+ before { rule.categories << category }
it { expect(rule).not_to be_eligible(order) }
it 'sets an error message' do
@@ -85,15 +84,15 @@ describe Spree::Promotion::Rules::Category, type: :model do
end
end
- context 'when a product has a taxon child of a taxon rule' do
- let(:taxon3) { create :taxon, taxonomy: taxonomy }
+ context 'when a product has a category child of a category rule' do
+ let(:category3) { create :category }
before do
- taxon.children << taxon2
- order.products.first.taxons << taxon2
- order.products.last.taxons << taxon3
- rule.taxons << taxon2
- rule.taxons << taxon3
+ category.children << category2
+ order.products.first.categories << category2
+ order.products.last.categories << category3
+ rule.categories << category2
+ rule.categories << category3
end
it { expect(rule).to be_eligible(order) }
@@ -104,7 +103,7 @@ describe Spree::Promotion::Rules::Category, type: :model do
describe '#add_taxons' do
let(:promotion) { create(:promotion) }
let(:rule) { create(:promotion_rule_taxon, promotion: promotion) }
- let(:taxons) { create_list(:taxon, 3) }
+ let(:taxons) { create_list(:category, 3) }
it 'adds the taxons to the rule' do
rule.taxon_ids_to_add = taxons.map(&:id)
diff --git a/spree/core/spec/models/spree/promotion_spec.rb b/spree/core/spec/models/spree/promotion_spec.rb
index 76cff06590..c8f17392c6 100644
--- a/spree/core/spec/models/spree/promotion_spec.rb
+++ b/spree/core/spec/models/spree/promotion_spec.rb
@@ -950,7 +950,7 @@ describe Spree::Promotion, type: :model do
let(:promotion) { create(:promotion) }
let(:product) { create(:product) }
let(:other_product) { create(:product) }
- let(:taxon) { create(:taxon) }
+ let(:category) { create(:category) }
it 'builds a rule from a {type, preferences} hash' do
promotion.rules = [{ type: 'item_total', preferences: { amount_min: 50 } }]
@@ -1034,13 +1034,13 @@ describe Spree::Promotion, type: :model do
expect(promotion.rules.first.products).to contain_exactly(other_product)
end
- it 'builds a Taxon rule with taxon_ids' do
- promotion.rules = [{ type: 'category', taxon_ids: [taxon.id] }]
+ it 'builds a Taxon rule with category_ids' do
+ promotion.rules = [{ type: 'category', category_ids: [category.id] }]
promotion.reload
rule = promotion.rules.first
expect(rule).to be_a(Spree::Promotion::Rules::Category)
- expect(rule.taxons).to contain_exactly(taxon)
+ expect(rule.taxons).to contain_exactly(category)
end
# Regression: `category_ids=` on Rules::Taxon is a custom setter (not a
@@ -1049,12 +1049,12 @@ describe Spree::Promotion, type: :model do
# an association name.
it 'builds a Taxon rule on a new promotion using category_ids (custom setter)' do
fresh = build(:promotion)
- fresh.rules = [{ type: 'category', category_ids: [taxon.id] }]
+ fresh.rules = [{ type: 'category', category_ids: [category.id] }]
fresh.save!
rule = fresh.reload.rules.first
expect(rule).to be_a(Spree::Promotion::Rules::Category)
- expect(rule.taxons).to contain_exactly(taxon)
+ expect(rule.taxons).to contain_exactly(category)
end
it 'defers application until after_save on a new promotion record' do
diff --git a/spree/core/spec/models/spree/report_line_items/products_performance_spec.rb b/spree/core/spec/models/spree/report_line_items/products_performance_spec.rb
index 6e7d652ebf..b963e9ffaa 100644
--- a/spree/core/spec/models/spree/report_line_items/products_performance_spec.rb
+++ b/spree/core/spec/models/spree/report_line_items/products_performance_spec.rb
@@ -19,18 +19,18 @@ RSpec.describe Spree::ReportLineItems::ProductsPerformance do
end
describe '#category_levels' do
- let(:taxonomy) { store.taxonomies.first || create(:taxonomy, name: 'Categories', store: store) }
- let(:taxon) { create(:taxon, name: 'Shoes', taxonomy: taxonomy) }
+ let(:category) { create(:category, name: 'Shoes') }
context 'when product has taxons' do
before do
- product.taxons << taxon
+ product.categories << category
product.save!
end
it 'returns mapped category levels' do
- expect(subject.category_lvl0).to eq('Categories')
- expect(subject.category_lvl1).to eq('Shoes')
+ # A top-level category is level 0 — there is no taxonomy root above it.
+ expect(subject.category_lvl0).to eq('Shoes')
+ expect(subject.category_lvl1).to be_nil
expect(subject.category_lvl2).to be_nil
end
end
diff --git a/spree/core/spec/models/spree/search_provider/database_spec.rb b/spree/core/spec/models/spree/search_provider/database_spec.rb
index d6235613e3..4d49766589 100644
--- a/spree/core/spec/models/spree/search_provider/database_spec.rb
+++ b/spree/core/spec/models/spree/search_provider/database_spec.rb
@@ -118,23 +118,22 @@ module Spree
end
context 'with in_category filter' do
- let(:taxonomy) { create(:taxonomy, store: store) }
- let(:parent_taxon) { create(:taxon, taxonomy: taxonomy, name: 'Clothing') }
- let(:child_taxon) { create(:taxon, taxonomy: taxonomy, parent: parent_taxon, name: 'Shirts') }
+ let(:parent_category) { create(:category, name: 'Clothing') }
+ let(:child_category) { create(:category, parent: parent_category, name: 'Shirts') }
before do
- product_1.taxons << child_taxon
- product_2.taxons << parent_taxon
+ product_1.categories << child_category
+ product_2.categories << parent_category
end
it 'returns products directly in the category' do
- result = provider.search_and_filter(scope: scope, filters: { 'in_category' => child_taxon.prefixed_id })
+ result = provider.search_and_filter(scope: scope, filters: { 'in_category' => child_category.prefixed_id })
expect(result.products).to include(product_1)
expect(result.products).not_to include(product_2, product_3)
end
it 'returns products in descendant categories when filtering by parent' do
- result = provider.search_and_filter(scope: scope, filters: { 'in_category' => parent_taxon.prefixed_id })
+ result = provider.search_and_filter(scope: scope, filters: { 'in_category' => parent_category.prefixed_id })
expect(result.products).to include(product_1, product_2)
expect(result.products).not_to include(product_3)
end
@@ -147,13 +146,12 @@ module Spree
end
context 'with in_categories filter (multiple, OR logic)' do
- let(:taxonomy) { create(:taxonomy, store: store) }
- let(:shirts_taxon) { create(:taxon, taxonomy: taxonomy, name: 'Shirts') }
- let(:pants_taxon) { create(:taxon, taxonomy: taxonomy, name: 'Pants') }
+ let(:shirts_taxon) { create(:category, name: 'Shirts') }
+ let(:pants_taxon) { create(:category, name: 'Pants') }
before do
- product_1.taxons << shirts_taxon
- product_2.taxons << pants_taxon
+ product_1.categories << shirts_taxon
+ product_2.categories << pants_taxon
end
it 'returns products in any of the given categories' do
@@ -196,14 +194,17 @@ module Spree
end
context "with 'manual' sort by category" do
- let(:taxonomy) { create(:taxonomy, store: store) }
- let(:parent_category) { create(:taxon, taxonomy: taxonomy, name: 'Clothing') }
- let(:child_category) { create(:taxon, taxonomy: taxonomy, parent: parent_category, name: 'Shirts') }
+ let(:parent_category) { create(:category, name: 'Clothing') }
+ let(:child_category) { create(:category, parent: parent_category, name: 'Shirts') }
before do
# Positions deliberately differ from id/creation order so the assertion
# proves position ordering (not incidental default order). Positions span
# the parent AND its descendant — manual sort collapses the subtree by MIN.
+ #
+ # Built directly rather than through :product_category: the factory sets
+ # a position, which makes acts_as_list renumber siblings on each insert
+ # and overwrite the explicit values below.
Spree::ProductCategory.create!(category: parent_category, product: product_2).update_column(:position, 1)
Spree::ProductCategory.create!(category: child_category, product: product_1).update_column(:position, 2)
Spree::ProductCategory.create!(category: parent_category, product: product_3).update_column(:position, 3)
@@ -231,8 +232,7 @@ module Spree
end
context 'with no sort param on a category page' do
- let(:taxonomy) { create(:taxonomy, store: store) }
- let(:category) { create(:taxon, taxonomy: taxonomy, name: 'Clothing') }
+ let(:category) { create(:category, name: 'Clothing') }
before do
Spree::ProductCategory.create!(category: category, product: product_2).update_column(:position, 1)
diff --git a/spree/core/spec/models/spree/taxon_category_aliases_spec.rb b/spree/core/spec/models/spree/taxon_category_aliases_spec.rb
index af48dfea13..e861d0d1c9 100644
--- a/spree/core/spec/models/spree/taxon_category_aliases_spec.rb
+++ b/spree/core/spec/models/spree/taxon_category_aliases_spec.rb
@@ -15,7 +15,7 @@ RSpec.describe 'Taxon -> Category deprecation aliases' do
describe Spree::Taxons::AddProducts do
it 'warns and delegates to Spree::Categories::AddProducts (adds the products)' do
- category = create(:taxon)
+ category = create(:category)
product = create(:product)
expect(Spree::Deprecation).to receive(:warn).with(/Spree::Categories::AddProducts/)
@@ -27,7 +27,7 @@ RSpec.describe 'Taxon -> Category deprecation aliases' do
describe Spree::Taxons::RemoveProducts do
it 'warns and delegates to Spree::Categories::RemoveProducts (removes the products)' do
- category = create(:taxon)
+ category = create(:category)
product = create(:product)
create(:product_category, category: category, product: product)
@@ -73,7 +73,7 @@ RSpec.describe 'Taxon -> Category deprecation aliases' do
it 'is a subclass of the renamed job and warns on perform' do
expect(described_class.superclass).to eq(Spree::Products::TouchCategoriesJob)
- category = create(:taxon)
+ category = create(:category)
expect(Spree::Deprecation).to receive(:warn).with(/TouchCategoriesJob/)
expect { described_class.new.perform([category.id], []) }.not_to raise_error
@@ -101,7 +101,7 @@ RSpec.describe 'Taxon -> Category deprecation aliases' do
end
it 'Spree::ProductCategory#taxon warns and returns #category' do
- product_category = create(:classification)
+ product_category = create(:product_category)
expect(Spree::Deprecation).to receive(:warn).with(/#category/)
expect(product_category.taxon).to eq(product_category.category)
end
diff --git a/spree/core/spec/models/spree/taxon_spec.rb b/spree/core/spec/models/spree/taxon_spec.rb
deleted file mode 100644
index f63c7da9ee..0000000000
--- a/spree/core/spec/models/spree/taxon_spec.rb
+++ /dev/null
@@ -1,644 +0,0 @@
-require 'spec_helper'
-
-describe Spree::Category, type: :model do
- let(:store) { @default_store }
- let(:taxonomy) { store.taxonomies.first || create(:taxonomy, store: store) }
- let(:taxon) { build(:taxon, name: 'Ruby on Rails', parent: nil) }
-
- it_behaves_like 'metadata'
-
- describe '#to_param' do
- subject { super().to_param }
-
- it { is_expected.to eql taxon.permalink }
- end
-
- context 'Validations' do
- describe '#check_for_root' do
- let(:valid_taxon) { build(:taxon, name: 'Vaild Rails', parent_id: taxonomy.root.id, taxonomy: taxonomy) }
-
- it 'does not validate the taxon' do
- expect(taxon.valid?).to eq false
- end
-
- it 'validates the taxon' do
- expect(valid_taxon.valid?).to eq true
- end
- end
-
- describe '#parent_belongs_to_same_taxonomy' do
- let(:valid_parent) { create(:taxon, name: 'Valid Parent', taxonomy: taxonomy) }
- let(:invalid_parent) { create(:taxon, name: 'Invalid Parent', taxonomy: create(:taxonomy, store: store)) }
-
- it 'does not validate the taxon' do
- expect(build(:taxon, taxonomy: taxonomy, parent: invalid_parent).valid?).to eq false
- end
-
- it 'validates the taxon' do
- expect(build(:taxon, taxonomy: taxonomy, parent: valid_parent).valid?).to eq true
- end
- end
- end
-
- context 'Store ownership' do
- describe '#requires_taxonomy?' do
- it 'is false — a Category is owned via store_id, not a taxonomy' do
- expect(described_class.new.requires_taxonomy?).to be(false)
- end
- end
-
- describe 'taxonomy presence' do
- it 'is valid without a taxonomy when store-owned' do
- category = described_class.new(name: 'Standalone', store: store)
- expect(category).to be_valid
- end
- end
-
- describe '#ensure_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 '.with_matching_name' do
- let!(:taxon1) { create(:taxon, name: 'shoes', taxonomy: taxonomy) }
- let!(:taxon2) { create(:taxon, name: 'Premium Shoes', taxonomy: taxonomy) }
-
- it 'returns the taxon with the matching name', :aggregate_failures do
- expect(described_class.with_matching_name('SHOES')).to eq([taxon1])
- expect(described_class.with_matching_name('Shoes')).to eq([taxon1])
- expect(described_class.with_matching_name('shoes')).to eq([taxon1])
-
- expect(described_class.with_matching_name('premium SHOES')).to eq([taxon2])
- expect(described_class.with_matching_name('Premium shoes')).to eq([taxon2])
- expect(described_class.with_matching_name('premium shoes')).to eq([taxon2])
- end
-
- context 'with translations' do
- before do
- I18n.with_locale(:pl) do
- taxon1.update!(name: 'Buty')
- taxon2.update!(name: 'Buty Premium')
- end
- end
-
- it 'returns the taxon with the matching name', :aggregate_failures do
- I18n.with_locale(:pl) do
- expect(described_class.with_matching_name('BUTY')).to eq([taxon1])
- expect(described_class.with_matching_name('Buty')).to eq([taxon1])
- expect(described_class.with_matching_name('buty')).to eq([taxon1])
-
- expect(described_class.with_matching_name('Buty PREMIUM')).to eq([taxon2])
- expect(described_class.with_matching_name('Buty premium')).to eq([taxon2])
- expect(described_class.with_matching_name('buty premium')).to eq([taxon2])
- end
- end
- end
- end
- end
-
- context 'when using another locale' do
- before do
- root_taxon = taxon.taxonomy.root
- taxon.update!(name: 'EN name', parent: taxon.taxonomy.root)
-
- Mobility.with_locale(:pl) do
- root_taxon.update!(name: 'PL taxonomy')
-
- taxon.update!(
- name: 'PL name',
- description: 'PL description'
- )
- end
-
- taxon.reload
- end
-
- let(:taxon_pl_translation) { taxon.translations.find_by(locale: 'pl') }
-
- it 'translates taxon fields' do
- expect(taxon.name).to eq('EN name')
-
- expect(taxon_pl_translation).to be_present
- expect(taxon_pl_translation.name).to eq('PL name')
- expect(taxon_pl_translation.permalink).to eq('pl-taxonomy/pl-name')
-
- expect(taxon.description_pl.to_plain_text).to eq('PL description')
- end
- end
-
- context 'set_permalink' do
- it 'sets permalink correctly when no parent present' do
- taxon.set_permalink
- expect(taxon.permalink).to eql 'ruby-on-rails'
- end
-
- it 'supports Chinese characters' do
- taxon.name = '你好'
- taxon.set_permalink
- expect(taxon.permalink).to eql 'ni-hao'
- end
-
- it 'stores old slugs in FriendlyIds history' do
- # Stub out the unrelated methods that cannot handle a save without an id
- allow(subject).to receive(:set_depth!)
- expect(subject).to receive(:create_slug)
- subject.permalink = 'custom-slug'
- subject.run_callbacks :save
- end
-
- context 'with parent taxon' do
- let(:parent) { FactoryBot.build(:taxon, permalink: 'brands') }
-
- before { allow(taxon).to receive_messages parent: parent }
-
- it 'sets permalink correctly when taxon has parent' do
- taxon.set_permalink
- expect(taxon.permalink).to eql 'brands/ruby-on-rails'
- end
-
- it 'sets permalink correctly with existing permalink present' do
- taxon.permalink = 'b/rubyonrails'
- taxon.set_permalink
- expect(taxon.permalink).to eql 'brands/rubyonrails'
- end
-
- it 'supports Chinese characters' do
- taxon.name = '我'
- taxon.set_permalink
- expect(taxon.permalink).to eql 'brands/wo'
- end
-
- # Regression test for #3390
- context 'setting a new node sibling position via :child_index=' do
- let(:idx) { rand(0..100) }
-
- before { allow(parent).to receive(:move_to_child_with_index) }
-
- context 'taxon is not new' do
- before { allow(taxon).to receive(:new_record?).and_return(false) }
-
- it 'passes the desired index move_to_child_with_index of :parent ' do
- expect(taxon).to receive(:move_to_child_with_index).with(parent, idx)
-
- taxon.child_index = idx
- end
- end
- end
- end
- end
-
- # Regression test for #13395
- describe 'permalink uniqueness after normalization' do
- let!(:taxonomy) { create(:taxonomy, store: store) }
- let(:parent) { taxonomy.root }
-
- it 'returns a validation error instead of raising RecordNotUnique when normalized permalink conflicts' do
- create(:taxon, name: 'Foo Bar', taxonomy: taxonomy, parent: parent)
-
- sibling = create(:taxon, name: 'Other', taxonomy: taxonomy, parent: parent)
- sibling.permalink = "#{parent.permalink}/Foo Bar"
-
- expect { sibling.save }.not_to raise_error
- expect(sibling.errors[:permalink]).to be_present
- end
-
- it 'normalizes permalink before validation on update' do
- taxon = create(:taxon, name: 'Test', taxonomy: taxonomy, parent: parent)
- taxon.permalink = "#{parent.permalink}/Héllo Wörld"
- taxon.valid?
-
- expect(taxon.permalink).to eq("#{parent.permalink}/hello-world")
- end
- end
-
- # Regression test for #2620
- context 'creating a child node using first_or_create' do
- let!(:taxonomy) { create(:taxonomy, store: store) }
-
- it 'does not error out' do
- expect { taxonomy.root.children.unscoped.where(name: 'Some name', parent_id: taxonomy.taxons.first.id).first_or_create }.not_to raise_error
- end
- end
-
- context 'ransackable_associations' do
- it { expect(described_class.whitelisted_ransackable_associations).to include('taxonomy') }
- end
-
- describe '#cached_self_and_descendants_ids' do
- it { expect(taxon.cached_self_and_descendants_ids).to eq(taxon.self_and_descendants.ids) }
- end
-
- describe '#copy_taxonomy_from_parent' do
- let!(:parent) { create(:taxon, taxonomy: taxonomy) }
- let(:taxon) { build(:taxon, parent: parent, taxonomy: nil) }
-
- it { expect(taxon.valid?).to eq(true) }
- it { expect { taxon.save! }.to change(taxon, :taxonomy).to(taxonomy) }
- end
-
- describe '#sync_taxonomy_name' do
- let!(:taxonomy) { create(:taxonomy, name: 'Soft Goods', store: store) }
- let!(:taxon) { create(:taxon, taxonomy: taxonomy, name: 'Socks' ) }
-
- context 'when none root taxon name is updated' do
- it 'does not update the taxonomy name' do
- taxon.update!(name: 'Shoes')
- taxonomy.reload
-
- expect(taxonomy.name).not_to eql taxon.name
- expect(taxonomy.name).to eql 'Soft Goods'
- end
- end
-
- context 'when root taxon name is updated' do
- it 'updates the taxonomy name' do
- root_taxon = described_class.find_by(name: 'Soft Goods')
-
- root_taxon.update!(name: 'Hard Goods')
- taxonomy.reload
-
- expect(taxonomy.name).not_to eql 'Soft Goods'
- expect(taxonomy.name).to eql root_taxon.name
- end
- end
-
- context 'when root taxon name is updated with special characters' do
- it 'updates the taxonomy name' do
- root_taxon = described_class.find_by(name: 'Soft Goods')
-
- root_taxon.update!(name: 'spÉcial Numérique ƒ ˙ ¨ πø∆©')
- taxonomy.reload
-
- expect(taxonomy.name).not_to eql 'Soft Goods'
- expect(taxonomy.name).to eql root_taxon.name
- end
- end
-
- context 'when root taxon attribute other than name is updated' do
- it 'does not update the taxonomy name' do
- root_taxon = described_class.find_by(name: 'Soft Goods')
-
- expect {
- root_taxon.update!(permalink: 'something-else')
- taxonomy.reload
- }.not_to change { taxonomy.name }
-
- expect(root_taxon.permalink).to eql 'something-else'
- end
- end
- end
-
- describe '#localized_slugs_for_store' do
- let(:store) { create(:store, default_locale: 'fr', supported_locales: 'en,pl,fr') }
- let(:taxonomy) { store.taxonomies.find_by(name: Spree.t(:taxonomy_categories_name)) || create(:taxonomy, name: Spree.t(:taxonomy_categories_name), store: store) }
- let(:taxon) { create(:taxon, taxonomy: taxonomy, permalink: 'test_slug_en') }
- let!(:taxon_translation_fr) { taxon.translations.create(slug: 'test_slug_fr', locale: 'fr') }
- let!(:root_taxon) { taxonomy.taxons.find_by(parent_id: nil) }
-
- before { Spree::Locales::SetFallbackLocaleForStore.new.call(store: store) }
-
- subject { taxon.localized_slugs_for_store(store) }
-
- context 'when there are slugs in locales not supported by the store' do
- let!(:taxon_translation_pl) { taxon.translations.create(slug: 'test_slug_pl', locale: 'pl') }
- let!(:taxon_translation_de) { taxon.translations.create(slug: 'test_slug_de', locale: 'de') }
-
- let(:expected_slugs) do
- {
- 'en' => 'categories/test-slug-en',
- 'fr' => 'categories/test-slug-fr',
- 'pl' => 'categories/test-slug-pl'
- }
- end
-
- it 'returns only slugs in locales supported by the store' do
- expect(subject).to match(expected_slugs)
- end
- end
-
- context 'when one of the supported locales does not have a translation' do
- let(:expected_slugs) do
- {
- 'en' => 'categories/test-slug-en',
- 'fr' => 'categories/test-slug-fr',
- 'pl' => 'categories/test-slug-fr'
- }
- end
-
- it "falls back to store's default locale" do
- expect(subject).to match(expected_slugs)
- end
- end
-
- context 'when setting the slug translations for taxonomy' do
- let!(:root_taxon_translation_pl) { root_taxon.translations.create(slug: 'slug with space', locale: 'pl') }
- let!(:root_taxon_translation_fr) { root_taxon.translations.create(slug: 'categories-fr', locale: 'fr') }
-
- let(:expected_slugs) do
- {
- 'en' => 'categories',
- 'fr' => 'categories-fr',
- 'pl' => 'slug-with-space'
- }
- end
-
- it "sets the slugs in slug format" do
- expect(root_taxon.reload.localized_slugs_for_store(store)).to match(expected_slugs)
- end
- end
-
- context 'when setting the slugs in taxon under taxomony with different parent slug' do
- let!(:root_taxon_translation_pl) { root_taxon.translations.create(slug: 'slug with space', locale: 'pl') }
- let!(:taxon_translation_pl) { taxon.translations.create(locale: 'pl') }
-
- let(:expected_slugs) do
- {
- 'en' => 'categories/test-slug-en',
- 'fr' => 'categories/test-slug-fr',
- 'pl' => "slug-with-space/#{taxon.name.to_url}"
- }
- end
-
- it "sets the slug in valid format" do
- expect(taxon.localized_slugs_for_store(store)).to match(expected_slugs)
- end
- end
- end
-
- describe '#regenerate_pretty_name_and_permalink' do
- let!(:taxon) { create(:taxon, name: 'Category#1', taxonomy: taxonomy) }
-
- it 'regenerates pretty name and permalink' do
- expect(taxon.pretty_name).to eq("#{taxon.parent.pretty_name} -> #{taxon.name}")
- expect(taxon.permalink).to eq("#{taxon.parent.permalink}/#{taxon.name.to_url}")
- end
-
- context "when parent's permalink is changed" do
- before do
- taxon.parent.update!(permalink: 'new-permalink')
- end
-
- it 'updates the pretty name and permalink' do
- expect(taxon.reload.pretty_name).to eq("#{taxon.parent.pretty_name} -> #{taxon.name}")
- expect(taxon.permalink).to eq("new-permalink/#{taxon.name.to_url}")
- end
- end
-
- context 'when parent name is changed' do
- before do
- taxon.parent.update!(name: 'New Parent')
- end
-
- it 'updates the pretty name and permalink' do
- expect(taxon.reload.pretty_name).to eq("New Parent -> #{taxon.name}")
- expect(taxon.permalink).to eq("#{taxon.parent.permalink}/#{taxon.name.to_url}")
- end
- end
-
- context 'with translations' do
- before do
- Mobility.with_locale(:pl) do
- taxon.update!(name: 'Kategoria#1')
- taxon.reload
-
- taxon.parent.update!(name: 'Kategoria')
- end
- end
-
- it 'updates the pretty name and permalink for translations as well' do
- Mobility.with_locale(:pl) do
- expect(taxon.reload.pretty_name).to eq('Kategoria -> Kategoria#1')
- expect(taxon.permalink).to eq('kategoria/kategoria-number-1')
- end
- end
- end
-
- context 'when taxon is moved' do
- let(:parent2) { create(:taxon, name: 'Parent2', permalink: 'parent2', taxonomy: taxonomy) }
- let(:taxon2) { create(:taxon, name: 'Child', parent: parent2, permalink: 'child', taxonomy: taxonomy) }
-
- before do
- taxon.parent.update!(name: 'Grandparent', permalink: 'grandparent')
- taxon.update!(name: 'Parent', permalink: 'parent')
-
- parent2
- taxon2
-
- Mobility.with_locale(:pl) do
- taxon.parent.update!(name: 'Dziadek', permalink: 'dziadek')
- taxon.update!(name: 'Rodzic')
-
- parent2.update!(name: 'Rodzic2', permalink: 'rodzic2')
- taxon2.update!(name: 'Dziecko')
- end
-
- expect(taxon.permalink).to eq('grandparent/parent')
- expect(taxon.pretty_name).to eq('Grandparent -> Parent')
-
- expect(taxon2.permalink).to eq('grandparent/parent2/child')
- expect(taxon2.pretty_name).to eq('Grandparent -> Parent2 -> Child')
-
- Mobility.with_locale(:pl) do
- expect(taxon.reload.pretty_name).to eq('Dziadek -> Rodzic')
- expect(taxon.permalink).to eq('dziadek/rodzic')
-
- expect(taxon2.pretty_name).to eq('Dziadek -> Rodzic2 -> Dziecko')
- expect(taxon2.permalink).to eq('dziadek/rodzic2/dziecko')
- end
- end
-
- it 'updates the pretty name and permalink' do
- taxon2.move_to_child_with_index(taxon, 0)
-
- expect(taxon2.reload.pretty_name).to eq('Grandparent -> Parent -> Child')
- expect(taxon2.permalink).to eq('grandparent/parent/child')
-
- Mobility.with_locale(:pl) do
- expect(taxon2.reload.pretty_name).to eq('Dziadek -> Rodzic -> Dziecko')
- expect(taxon2.permalink).to eq('dziadek/rodzic/dziecko')
- end
- end
-
- it 'updates the pretty name and permalink when move is done inside different locales' do
- Mobility.with_locale(:pl) do
- taxon2.move_to_child_with_index(taxon, 0)
- end
-
- expect(taxon2.permalink).to eq('grandparent/parent/child')
- expect(taxon2.reload.pretty_name).to eq('Grandparent -> Parent -> Child')
-
- Mobility.with_locale(:pl) do
- expect(taxon2.reload.pretty_name).to eq('Dziadek -> Rodzic -> Dziecko')
- expect(taxon2.permalink).to eq('dziadek/rodzic/dziecko')
- end
- end
- end
- end
-
- describe '#pretty_name' do
- let!(:taxon) { create(:taxon, name: 'Category#1', taxonomy: taxonomy) }
-
- context '1 lvl deep' do
- it 'returns taxonomy name and taxon name' do
- expect(taxon.pretty_name).to eq("#{taxonomy.root.pretty_name} -> #{taxon.name}")
- end
- end
-
- context '2+ lvl deep' do
- let(:taxon_parent) { create(:taxon, name: 'Parent', taxonomy: taxonomy) }
-
- before do
- taxon.parent = taxon_parent
- taxon.save!
- end
-
- it 'returns parent name and taxon name' do
- expect(taxon.reload.pretty_name).to eq("#{taxonomy.root.pretty_name} -> Parent -> Category#1")
- end
-
- context 'when name is updated' do
- before do
- taxon.name = 'New Name'
- taxon.save!
- end
-
- it 'returns the updated pretty name' do
- expect(taxon.reload.pretty_name).to eq("#{taxonomy.root.pretty_name} -> Parent -> New Name")
- end
- end
-
- context 'when parent name is updated' do
- before do
- taxon_parent.name = 'New Parent'
- taxon_parent.save!
- end
-
- it 'returns the updated pretty name' do
- expect(taxon.reload.pretty_name).to eq("#{taxonomy.root.pretty_name} -> New Parent -> Category#1")
- end
- end
- end
-
- context 'when `always_use_translations` is disabled' do
- before do
- allow(Spree::Config).to receive(:always_use_translations).and_return(false)
- end
-
- it 'sets the pretty name' do
- expect(taxon.reload.pretty_name).to eq("#{taxonomy.name} -> #{taxon.name}")
- end
- end
-
- context 'when `always_use_translations` is enabled' do
- before do
- allow(Spree::Config).to receive(:always_use_translations).and_return(true)
- end
-
- it 'sets the pretty name' do
- expect(taxon.reload.pretty_name).to eq("#{taxonomy.name} -> #{taxon.name}")
- end
- end
- end
-
- describe '#store' do
- let(:taxonomy) { create(:taxonomy, store: store) }
- let(:taxon) { build(:taxon, taxonomy: taxonomy) }
-
- it 'returns the store from the taxonomy' do
- expect(taxon.store).to eq(store)
- end
- end
-
- describe '#active_products_with_descendants' do
- let(:root_category) { create(:taxon, taxonomy: taxonomy) }
-
- context 'when category has products' do
- let!(:product) { create(:product, taxons: [root_category]) }
-
- it 'returns true' do
- expect(root_category.reload.active_products_with_descendants.exists?).to be true
- end
-
- it 'returns true when products aren\'t active' do
- product.update(status: 'draft')
-
- expect(root_category.reload.products.exists?).to be true
- end
- end
-
- context 'when only children categories have products' do
- let(:parent_category) { create(:taxon, taxonomy: taxonomy, parent: root_category) }
- let(:child_category) { create(:taxon, taxonomy: taxonomy, parent: parent_category) }
- let!(:product) { create(:product, taxons: [child_category]) }
-
- it 'returns true' do
- expect(root_category.reload.active_products_with_descendants.exists?).to be true
- end
-
- it 'returns false when products aren\'t active' do
- product.update(status: 'draft')
-
- expect(root_category.reload.active_products_with_descendants.exists?).to be false
- end
- end
-
- context 'when category has no products' do
- it 'returns false' do
- expect(root_category.reload.active_products_with_descendants.exists?).to be false
- end
- end
-
- context 'when category has products but children categories have no products' do
- let(:parent_category) { create(:taxon, taxonomy: taxonomy, parent: root_category) }
- let!(:child_category) { create(:taxon, taxonomy: taxonomy, parent: parent_category) }
- let!(:product) { create(:product, taxons: [root_category]) }
-
- it 'returns true' do
- expect(root_category.reload.active_products_with_descendants.exists?).to be true
- end
-
- it 'returns false when products aren\'t active' do
- product.update(status: 'draft')
-
- expect(root_category.reload.active_products_with_descendants.exists?).to be false
- end
- end
-
- context 'when children categories also have no products' do
- let(:parent_category) { create(:taxon, taxonomy: taxonomy, parent: root_category) }
- let!(:child_category) { create(:taxon, taxonomy: taxonomy, parent: parent_category) }
-
- it 'returns false' do
- expect(root_category.reload.active_products_with_descendants.exists?).to be false
- end
- end
- end
-
-end
diff --git a/spree/core/spec/models/spree/taxonomy_spec.rb b/spree/core/spec/models/spree/taxonomy_spec.rb
deleted file mode 100644
index 024af159d7..0000000000
--- a/spree/core/spec/models/spree/taxonomy_spec.rb
+++ /dev/null
@@ -1,89 +0,0 @@
-require 'spec_helper'
-
-describe Spree::Taxonomy, type: :model do
- it_behaves_like 'metadata'
-
- describe 'scopes' do
- describe '.with_matching_name' do
- let!(:taxonomy1) { create(:taxonomy, name: 'Winter 2024') }
- let!(:taxonomy2) { create(:taxonomy, name: 'Winter 2025') }
-
- it 'returns the taxonomy with the matching name', :aggregate_failures do
- expect(described_class.with_matching_name('WINTER 2024')).to eq([taxonomy1])
- expect(described_class.with_matching_name('Winter 2024')).to eq([taxonomy1])
- expect(described_class.with_matching_name('winter 2024')).to eq([taxonomy1])
-
- expect(described_class.with_matching_name('WINTER 2025')).to eq([taxonomy2])
- expect(described_class.with_matching_name('Winter 2025')).to eq([taxonomy2])
- expect(described_class.with_matching_name('winter 2025')).to eq([taxonomy2])
- end
-
- context 'with translations' do
- before do
- I18n.with_locale(:pl) do
- taxonomy1.update!(name: 'Zima 2024')
- taxonomy2.update!(name: 'Zima 2025')
- end
- end
-
- it 'returns the taxonomy with the matching name', :aggregate_failures do
- I18n.with_locale(:pl) do
- expect(described_class.with_matching_name('ZIMA 2024')).to eq([taxonomy1])
- expect(described_class.with_matching_name('Zima 2024')).to eq([taxonomy1])
- expect(described_class.with_matching_name('zima 2024')).to eq([taxonomy1])
-
- expect(described_class.with_matching_name('ZIMA 2025')).to eq([taxonomy2])
- expect(described_class.with_matching_name('Zima 2025')).to eq([taxonomy2])
- expect(described_class.with_matching_name('zima 2025')).to eq([taxonomy2])
- end
- end
- end
- end
- end
-
- context '#destroy' do
- before do
- @taxonomy = create(:taxonomy)
- @root_taxon = @taxonomy.root
- @child_taxon = create(:taxon, taxonomy_id: @taxonomy.id, parent: @root_taxon)
- end
-
- it 'destroys all associated taxons' do
- @taxonomy.destroy
- expect { Spree::Category.find(@root_taxon.id) }.to raise_error(ActiveRecord::RecordNotFound)
- expect { Spree::Category.find(@child_taxon.id) }.to raise_error(ActiveRecord::RecordNotFound)
- end
- end
-
- describe '#set_root_taxon_name' do
- before do
- @taxonomy = create(:taxonomy, name: 'Clothing')
- @taxonomy.reload
- end
-
- context 'when Taxonomy is created' do
- it 'sets the root Taxonomy name to match' do
- expect(@taxonomy.root.name).to eq('Clothing')
- end
- end
-
- context 'when Taxonomy name is updated' do
- it 'changes the root Taxon name to match' do
- @taxonomy.update!(name: 'Soft Goods')
- @taxonomy.reload
-
- expect(@taxonomy.root.reload.name).to eq('Soft Goods')
- end
- end
-
- context 'when Taxonomy position is updated' do
- it 'does not change the root Taxon name' do
- @taxonomy.update!(position: 2)
- @taxonomy.reload
-
- expect(@taxonomy.root.name).to eq('Clothing')
- expect(@taxonomy.position).to eq(2)
- end
- end
- end
-end
diff --git a/spree/core/spec/presenters/spree/csv/order_line_items_presenter_spec.rb b/spree/core/spec/presenters/spree/csv/order_line_items_presenter_spec.rb
index 4dda0293c3..f8b7a96668 100644
--- a/spree/core/spec/presenters/spree/csv/order_line_items_presenter_spec.rb
+++ b/spree/core/spec/presenters/spree/csv/order_line_items_presenter_spec.rb
@@ -54,13 +54,13 @@ RSpec.describe Spree::CSV::OrderLineItemPresenter do
end
describe '#taxon_dict' do
- let(:taxon) { build(:taxon, pretty_name: 'Category -> Subcategory -> Product') }
+ let(:category) { build(:category, pretty_name: 'Category -> Subcategory -> Product') }
- it 'splits taxon pretty name into array' do
- expect(presenter.send(:taxon_dict, taxon)).to eq(['Category', 'Subcategory', 'Product'])
+ it 'splits category pretty name into array' do
+ expect(presenter.send(:taxon_dict, category)).to eq(['Category', 'Subcategory', 'Product'])
end
- it 'returns empty array for nil taxon' do
+ it 'returns empty array for nil category' do
expect(presenter.send(:taxon_dict, nil)).to eq([])
end
end
diff --git a/spree/core/spec/presenters/spree/search_provider/product_presenter_spec.rb b/spree/core/spec/presenters/spree/search_provider/product_presenter_spec.rb
index 55ada68c3f..2368e82858 100644
--- a/spree/core/spec/presenters/spree/search_provider/product_presenter_spec.rb
+++ b/spree/core/spec/presenters/spree/search_provider/product_presenter_spec.rb
@@ -56,29 +56,29 @@ module Spree
end
context 'with categories' do
- let(:taxonomy) { create(:taxonomy, store: store) }
- let(:taxon) { create(:taxon, taxonomy: taxonomy) }
- let(:product) { create(:product, name: 'Test Shirt', taxons: [taxon]) }
+ let(:root_category) { create(:category, store: store, name: 'Apparel') }
+ let(:category) { create(:category, store: store, parent: root_category) }
+ let(:product) { create(:product, name: 'Test Shirt', categories: [category]) }
it 'indexes category_ids as prefixed IDs including ancestors' do
doc = documents.first
- expect(doc[:category_ids]).to include(taxon.prefixed_id)
- expect(doc[:category_ids]).to include(taxonomy.root.prefixed_id)
+ expect(doc[:category_ids]).to include(category.prefixed_id)
+ expect(doc[:category_ids]).to include(root_category.prefixed_id)
doc[:category_ids].each { |id| expect(id).to start_with('ctg_') }
end
end
context 'with nested categories' do
- let(:taxonomy) { create(:taxonomy, store: store) }
- let(:parent) { create(:taxon, taxonomy: taxonomy, name: 'Men') }
- let(:child) { create(:taxon, taxonomy: taxonomy, parent: parent, name: 'Jackets') }
- let(:product) { create(:product, name: 'Test Jacket', channels: [store.default_channel], taxons: [child]) }
+ let(:root_category) { create(:category, store: store, name: 'Apparel') }
+ let(:parent) { create(:category, store: store, parent: root_category, name: 'Men') }
+ let(:child) { create(:category, store: store, parent: parent, name: 'Jackets') }
+ let(:product) { create(:product, name: 'Test Jacket', channels: [store.default_channel], categories: [child]) }
it 'indexes all ancestor category IDs so parent-level filtering works' do
doc = documents.first
expect(doc[:category_ids]).to include(child.prefixed_id)
expect(doc[:category_ids]).to include(parent.prefixed_id)
- expect(doc[:category_ids]).to include(taxonomy.root.prefixed_id)
+ expect(doc[:category_ids]).to include(root_category.prefixed_id)
end
end
@@ -118,11 +118,10 @@ module Spree
end
context 'category membership positions (subtree-MIN)' do
- let(:taxonomy) { create(:taxonomy, store: store) }
- let(:parent) { create(:taxon, taxonomy: taxonomy, name: 'Men') }
- let(:jackets) { create(:taxon, taxonomy: taxonomy, parent: parent, name: 'Jackets') }
- let(:coats) { create(:taxon, taxonomy: taxonomy, parent: parent, name: 'Coats') }
- let(:product) { create(:product, name: 'Test Jacket', channels: [store.default_channel], taxons: [jackets, coats]) }
+ let(:parent) { create(:category, store: store, name: 'Men') }
+ let(:jackets) { create(:category, store: store, parent: parent, name: 'Jackets') }
+ let(:coats) { create(:category, store: store, parent: parent, name: 'Coats') }
+ let(:product) { create(:product, name: 'Test Jacket', channels: [store.default_channel], categories: [jackets, coats]) }
before do
Spree::ProductCategory.where(product: product, category: jackets).update_all(position: 5)
diff --git a/spree/core/spec/services/spree/imports/row_processors/product_variant_spec.rb b/spree/core/spec/services/spree/imports/row_processors/product_variant_spec.rb
index 8db3f5bd1e..95516442bf 100644
--- a/spree/core/spec/services/spree/imports/row_processors/product_variant_spec.rb
+++ b/spree/core/spec/services/spree/imports/row_processors/product_variant_spec.rb
@@ -296,12 +296,11 @@ RSpec.describe Spree::Imports::RowProcessors::ProductVariant, type: :service do
end
it 'preserves product taxons' do
- men_taxonomy = store.taxonomies.find_by(name: 'Men') || create(:taxonomy, name: 'Men', store: store)
- clothing_taxon = create(:taxon, name: 'Clothing', taxonomy: men_taxonomy, parent: men_taxonomy.root)
- product.update!(taxons: [clothing_taxon])
+ clothing_category = create(:category, name: 'Clothing')
+ product.update!(categories: [clothing_category])
subject.process!
- expect(product.reload.taxons).to eq [clothing_taxon]
+ expect(product.reload.categories).to eq [clothing_category]
end
it 'preserves product metafields' do