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
Cover nested product data in the workflow specs
Damian Legawiec0e301530ff28
diff --git a/docs/plans/6.0-extendable-validations.md b/docs/plans/6.0-extendable-validations.mdindex 1f9f9d95ed..f539704a6c 100644--- a/docs/plans/6.0-extendable-validations.md+++ b/docs/plans/6.0-extendable-validations.md@@ -53,10 +53,14 @@ value validation is a deferred, separate topic.bypasses ActiveRecord validations, normalizations, and line-item pricecapture on the money path.- **`Products::Create`, `Products::Update`, and `Products::Destroy` become- workflows** with `validate` + lifecycle hooks. Product writes are real- orchestration (nested variants, delivery-profile stamping, the two-step- custom-fields write, publications), not the plain CRUD the services doctrine- rejects wrapping. Every server-side write path routes through them: Admin API+ workflows** with `validate` + lifecycle hooks. They are **deliberately+ thin** — assigning nested data (variants, media, prices, custom fields,+ categories) stays on the model's own setters and `after_create`/`after_save`+ callbacks, where every caller gets it whether or not a workflow is involved.+ The workflow exists for the veto point, and its placement is the payoff:+ those setters stash their input and replay it once the product exists, so+ `validate` runs before the insert and a rejection leaves no half-built+ product behind. Every server-side write path routes through them: Admin APIv3, the CSV importer, seeds and sample data. Hooks always fire — no bulkbypass flag; an installation with no handlers pays nothing. Variants ride theproduct graph (99% of variant writes are nested under the product); there is@@ -273,6 +277,13 @@ No schema changes anywhere in this plan.### Notes from implementation+- **The product workflows are thin by design, not by omission.** An earlier+ draft of this plan justified them as "creation is already orchestration";+ that was wrong — `Spree::Product` already implements `variants=`, `media=`,+ `prices=`, `custom_fields=` and friends as deferring setters replayed in+ callbacks, and that is where they belong. The workflow's contribution is the+ hook, sited before the insert.+- The order-side twin **fails the whole batch** rather than warning(`partial_success?` returns false): a merchant's struck-out or repriced rowsilently not applying is worse than a failed request. Storefront carts keepdiff --git a/spree/core/app/workflows/spree/products/create.rb b/spree/core/app/workflows/spree/products/create.rbindex 9462a103e8..0e85e2773c 100644--- a/spree/core/app/workflows/spree/products/create.rb+++ b/spree/core/app/workflows/spree/products/create.rb@@ -5,10 +5,14 @@ module Spree# handler sees every product a store gains rather than only the ones# typed into the dashboard.#- # Product writes earn a workflow (rather than the plain CRUD the services- # doctrine leaves alone) because creation is already orchestration:- # nested variants, a delivery profile stamped from the product type, and- # custom-field values that can only be written after the product exists.+ # Deliberately thin. Assigning a product's nested data — variants, media,+ # prices, custom fields, categories — belongs to the model's setters and+ # its after_create/after_save callbacks, and stays there: every caller+ # gets that behavior, workflow or not. What the workflow adds is the veto+ # point, and where it sits matters. Those setters stash their input and+ # replay it once the product exists, so :validate runs before the insert+ # and therefore before any variant, image or custom field is written —+ # a rejection leaves nothing behind to clean up.class Create < Spree::Workflowhooks :validate, :after_creatediff --git a/spree/core/app/workflows/spree/products/update.rb b/spree/core/app/workflows/spree/products/update.rbindex 14fa679bef..c18b56969a 100644--- a/spree/core/app/workflows/spree/products/update.rb+++ b/spree/core/app/workflows/spree/products/update.rb@@ -1,9 +1,11 @@module Spreemodule Products- # Updates a product. Handlers reading `product.changes` in :validate see- # the pending edit before it is written, which is what makes rules like- # "price may not drop below cost" or "an active product needs an image"- # expressible without a model validation.+ # Updates a product. Nested data is assigned by the model's own setters+ # (see Spree::Products::Create); this exists for the veto point.+ #+ # Handlers reading `product.changes` in :validate see the pending edit+ # before it is written, which is what makes rules like "price may not+ # drop below cost" expressible without a model validation.class Update < Spree::Workflowhooks :validate, :after_updatediff --git a/spree/core/spec/workflows/spree/products/lifecycle_spec.rb b/spree/core/spec/workflows/spree/products/lifecycle_spec.rbindex 62090d82d5..8375c8fa0a 100644--- a/spree/core/spec/workflows/spree/products/lifecycle_spec.rb+++ b/spree/core/spec/workflows/spree/products/lifecycle_spec.rb@@ -45,6 +45,34 @@ RSpec.describe 'product workflows' doexpect(result.error.to_s).to include("Name can't be blank")end+ # Nested data is written by the model's after_create callbacks, so a+ # rejection before the insert leaves no half-built product behind.+ it 'writes no nested data when a handler rejects' do+ Spree.hooks.register('products.create.validate') { |flow| flow.reject!('not allowed') }++ result = nil+ expect do+ result = described_class.call(+ store: store,+ attributes: { name: 'Rejected', variants: [{ sku: 'REJ-1' }] }+ )+ end.not_to change(Spree::Variant, :count)++ expect(result).to be_failure+ expect(Spree::Product.find_by(name: 'Rejected')).to be_nil+ end++ # The setters live on the model, so a workflow create gets them too.+ it 'applies nested variants through the model setters' do+ result = described_class.call(+ store: store,+ attributes: { name: 'With variants', variants: [{ sku: 'WV-1' }] }+ )++ expect(result).to be_success+ expect(result.value.variants.map(&:sku)).to include('WV-1')+ end+# The CSV importer assigns attributes across several steps and hands over# the record rather than a hash.it 'accepts an already-built product' do