Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
docs(plans): trim collection scopes to the flat single-collection surface
Łukasz Adamczyk6ab3cd77d721
diff --git a/docs/plans/6.0-replace-taxons-with-categories.md b/docs/plans/6.0-replace-taxons-with-categories.mdindex a98f5706f2..c9adccb7bb 100644--- a/docs/plans/6.0-replace-taxons-with-categories.md+++ b/docs/plans/6.0-replace-taxons-with-categories.md@@ -421,7 +421,7 @@ Products in a collection are served by a **dedicated endpoint**, not a filter on- `GET /api/v3/store/collections/:id/products` — a thin controller that resolves the collection and **delegates to the same search-provider flow** as `/products` (query, Ransack filters, pagination, serializer). `def scope` stays the **base visibility scope** (`available` + `accessible_by`) — it is **not** pre-scoped to the collection. Instead the controller injects `in_collection: collection.id` into the filters, and each provider applies it: the DB provider via the `in_collection` Ransack scope, Meilisearch via the `collection_ids` filter. Collection scoping has to be a provider filter (not an AR pre-scope) because Meilisearch does its own filtering/counting/pagination and treats the AR scope only as a post-search visibility guard. No logic duplication.- **Sort precedence.** Effective sort = the request's `sort` param if present, otherwise the collection's `sort_order`. Position ordering (`ProductCollection.position`) applies **only when the effective sort is `manual`**; any dynamic sort — whether from the collection's own default or a shopper override — dismisses it. Because the listing runs through the provider (for the `in_collection` filter, pagination, and faceting), manual sort **cannot** be handled by bypassing the provider with a direct AR query — it must be implemented *in* the provider. That's the same provider-level manual-sort fix categories need, which is **unresolved** (Open Question #1, approach TBD); until it lands, a `sort_order: 'manual'` collection falls back to default order. The dedicated endpoint does **not** sidestep the v3 manual-sort gap.- **Facets/sort metadata** reuse the existing `GET /api/v3/store/products/filters` with a new **`collection_id`** param (parallel to `category_id`): it resolves the collection, scopes `in_collection`, and passes **only the collection's `sort_order`** to `FiltersAggregator` — via a `sort_order:` string param, **not** the `Collection` object (the aggregator stays decoupled from the model). No child facet (collections are flat).-- **Model:** `Spree::Product.in_collection` / `in_collections` scopes (flat join to `spree_product_collections`, no descendants), whitelisted as ransackable scopes.+- **Model:** `Spree::Product.in_collection` scope (flat join to `spree_product_collections`, no descendants), whitelisted as a ransackable scope. (No `in_collections` plural — the collection surface is single-collection; add it only if a real multi-collection filter appears.)- **Meilisearch:** index `collection_ids` (flat, no ancestors) on the product document, add it to `filterable_attributes` / `facet_attributes`, and handle `in_collection` in `build_filter_condition`. Kept fresh by `RegenerateProducts` (touches products + `enqueue_search_index`).Membership is materialized in `ProductCollection` for both manual and automatic collections, so read-time is always a plain join filter — rules are never evaluated per request.@@ -505,7 +505,7 @@ end**1b. Models + code** (built on 1a, fully spec'd before any rename):- `Spree::Collection`, `Spree::ProductCollection`, `Spree::CollectionRule` + STI `CollectionRules::{Tag,AvailableOn,Sale}`, all registered in `config.spree.collection_rules` (**mirrors the existing `config.spree.taxon_rules`**, `engine.rb`). `AvailableOn` ships with the interim legacy-column `apply`; channel-aware reimplementation is Phase 5- `Spree::Collections::RegenerateProducts`; `AutoMatchCollections` (+ job) for event-driven regeneration on product save; `RegenerateTimeBasedJob` + `time_based_collection_rules = [AvailableOn]` registry (**net-new — no `taxon_rules` equivalent; taxons ship no scheduled refresh**) for time-relative rules — all on the `collections` queue (see Design Details → Automatic collection regeneration)-- Product: `in_collection`/`in_collections` scopes (+ ransack whitelist), `collections`/`collections_count`, `primary_category`+- Product: `in_collection` scope (+ ransack whitelist), `collections`/`collections_count`, `primary_category`- Store + Admin Collection API: `collections` controllers + serializers; dedicated `collections/:id/products`; admin nested curation + `reposition`; `/products/filters` `collection_id` param + `FiltersAggregator` `sort_order:` change (see API → Collection product listing)- Meilisearch: index `collection_ids` + filterable/facet attrs + `build_filter_condition` + reindex@@ -625,7 +625,7 @@ Gated on the channels plan finishing the `available_on` → `ProductPublication#> - *Collection nesting* — collections stay **flat**; hierarchy lives in Category (matches Shopify and keeps the Category/Collection split clean).1. **"Manual" sort in the v3 Store API (categories *and* collections).** Manual/position ordering isn't implemented in the v3 search-provider path (see Current State → Known gap) — only in the admin controller and the legacy `Products::Find` finder. Both category pages and the collection endpoint list through the search provider, so neither can bypass it; the fix is provider-level and shared. Collections can ship without it (dynamic sorts all work; a `sort_order: 'manual'` collection just falls back to default order until this lands). The open part is the exact approach:- - **DB provider:** add a `'manual'` branch to `SearchProvider::Database#apply_sort` that orders by the grouping's min join-position (`ascend_by_taxons_min_position` for a category; an `ascend_by_collections_min_position` twin for a collection), and thread the category/collection context into `search_and_filter` / `apply_sort` (they don't receive it today).+ - **DB provider:** add a `'manual'` branch to `SearchProvider::Database#apply_sort` and thread the category/collection context into `search_and_filter` / `apply_sort` (they don't receive it today). For a **category** this orders by the grouping's min join-position (`ascend_by_taxons_min_position` — the `MIN`/`GROUP BY` collapses a product that sits under a category *and its descendants*). A **collection** needs no such scope: it's flat and a PLP targets one collection with a unique `(collection_id, product_id)`, so it's a plain `joins(:product_collections).where(collection_id:).order('spree_product_collections.position ASC')`.- **Meilisearch:** fall back to the DB for the manual case (resolve ordered ids via the min-position scope, bypassing MS sort); native per-grouping position sort isn't feasible (position is a many-valued join property; MS sorts only on fixed scalar attributes).## References