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.

Retire the state machine on Product

Damian Legawiec470deb0038b3
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 12519b62ac..8270f41762 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
@@ -1474,7 +1474,7 @@ RSpec.describe Spree::Api::V3::Admin::ProductsController, type: :controller do
end
end
- Spree::Product.state_machine.states.map(&:name).each do |from_status|
+ Spree::Product.statuses.each do |from_status|
context "when product is in #{from_status} status" do
it_behaves_like 'updates status to active', from_status
end
diff --git a/spree/core/app/models/spree/product.rb b/spree/core/app/models/spree/product.rb
index 09b5ba0c94..1e220028e4 100644
--- a/spree/core/app/models/spree/product.rb
+++ b/spree/core/app/models/spree/product.rb
@@ -43,12 +43,6 @@ module Spree
STATUSES = %w[draft active archived].freeze
- STATUS_TO_WEBHOOK_EVENT = {
- 'active' => 'activated',
- 'draft' => 'drafted',
- 'archived' => 'archived'
- }.freeze
-
TRANSLATABLE_FIELDS = %i[name description slug meta_description meta_title].freeze
RICH_TEXT_TRANSLATABLE_FIELDS = %i[description].freeze
translates(*TRANSLATABLE_FIELDS, column_fallback: Spree.mobility_column_fallback)
@@ -224,8 +218,6 @@ module Spree
validate :discontinue_on_must_be_later_than_make_active_at, if: -> { make_active_at && discontinue_on }
scope :for_store, ->(store) { where(store_id: store.id) }
- scope :draft, -> { where(status: 'draft') }
- scope :archived, -> { where(status: 'archived') }
scope :not_archived, -> { where.not(status: 'archived') }
scope :on_sale, lambda { |currency = nil|
currency ||= Spree::Store.default.default_currency
@@ -263,7 +255,6 @@ module Spree
scope :archivable, -> { where(status: %w[active draft]) }
scope :by_source, ->(source) { send(source) }
- scope :paused, -> { where(status: 'paused') }
scope :published, -> { where(status: 'active') }
accepts_nested_attributes_for(
@@ -392,21 +383,30 @@ module Spree
# written per currency through the default variant's +set_price+.
delegate :compare_at_price, :track_inventory?, :images, to: :default_variant, allow_nil: true
- state_machine :status, initial: :draft do
- event :activate do
- transition to: :active
- end
- after_transition to: :active, do: [:after_activate, :publish_product_activated_event]
+ # No state machine — status moves through the Spree::Products workflows
+ # (docs/plans/6.0-service-workflows.md). has_status generates the `draft`
+ # and `archived` scopes and every predicate, but never redefines what the
+ # model already owns: `Product.active` stays the currency-aware scope from
+ # Spree::ProductScopes rather than a plain status lookup.
+ include Spree::HasStatus
+ has_status(*STATUSES, default: :draft)
- event :archive do
- transition to: :archived
- end
- after_transition to: :archived, do: [:after_archive, :publish_product_archived_event]
+ # @deprecated Call Spree.product_activate_workflow — removed in 6.1.
+ def activate!
+ Spree::Deprecation.warn('Spree::Product#activate! is deprecated and will be removed in Spree 6.1. Call Spree.product_activate_workflow instead.')
+ run_status_workflow(Spree.product_activate_workflow)
+ end
- event :draft do
- transition to: :draft
- end
- after_transition to: :draft, do: :after_draft
+ # @deprecated Call Spree.product_archive_workflow — removed in 6.1.
+ def archive!
+ Spree::Deprecation.warn('Spree::Product#archive! is deprecated and will be removed in Spree 6.1. Call Spree.product_archive_workflow instead.')
+ run_status_workflow(Spree.product_archive_workflow)
+ end
+
+ # @deprecated Call Spree.product_draft_workflow — removed in 6.1.
+ def draft!
+ Spree::Deprecation.warn('Spree::Product#draft! is deprecated and will be removed in Spree 6.1. Call Spree.product_draft_workflow instead.')
+ run_status_workflow(Spree.product_draft_workflow)
end
def self.bulk_auto_match_collections(store, product_ids)
@@ -1080,28 +1080,20 @@ module Spree
end
end
- def eligible_for_collection_matching?
- previously_new_record? || tag_list_previously_changed? || available_on_previously_changed?
- end
-
- def after_activate
- # Implement your logic here
- end
-
- def after_archive
- # Implement your logic here
- end
+ # Mirrors the machine's bang events: an illegal move raised.
+ def run_status_workflow(workflow)
+ result = workflow.call(product: self)
- def after_draft
- # Implement your logic here
- end
+ if result.failure?
+ errors.add(:base, result.error.value.to_s)
+ raise ActiveRecord::RecordInvalid, self
+ end
- def publish_product_activated_event
- publish_event('product.activated')
+ true
end
- def publish_product_archived_event
- publish_event('product.archived')
+ def eligible_for_collection_matching?
+ previously_new_record? || tag_list_previously_changed? || available_on_previously_changed?
end
end
end
diff --git a/spree/core/spec/models/spree/product_spec.rb b/spree/core/spec/models/spree/product_spec.rb
index e4c6771856..7f48dc7d6f 100644
--- a/spree/core/spec/models/spree/product_spec.rb
+++ b/spree/core/spec/models/spree/product_spec.rb
@@ -1903,6 +1903,33 @@ describe Spree::Product, type: :model do
end
end
+ describe 'status' do
+ it 'has no state machine' do
+ expect(described_class).not_to respond_to(:state_machines)
+ end
+
+ it 'defaults to draft' do
+ expect(described_class.new.status).to eq('draft')
+ end
+
+ it 'rejects an unknown status' do
+ expect(build(:product, status: 'nonsense')).not_to be_valid
+ end
+
+ # has_status must not shadow this one: Product.active takes a currency and
+ # joins prices, where a plain status lookup would silently return a
+ # different set.
+ it 'keeps the currency-aware active scope' do
+ expect(described_class.active(nil).to_sql).to include(Spree::Price.table_name)
+ end
+
+ it 'generates the plain status scopes and predicates' do
+ expect(described_class.draft.to_sql).to include("'draft'")
+ expect(described_class.archived.to_sql).to include("'archived'")
+ expect(build(:product, status: 'draft')).to be_draft
+ end
+ end
+
describe 'custom events', events: true do
describe 'product.activated' do
let(:product) { create(:product, status: 'draft') }
@@ -1911,7 +1938,7 @@ describe Spree::Product, type: :model do
expect(product).to receive(:publish_event).with('product.activated')
allow(product).to receive(:publish_event).with(anything)
- product.activate!
+ Spree::Products::Activate.call(product: product)
end
end
@@ -1922,7 +1949,7 @@ describe Spree::Product, type: :model do
expect(product).to receive(:publish_event).with('product.archived')
allow(product).to receive(:publish_event).with(anything)
- product.archive!
+ Spree::Products::Archive.call(product: product)
end
end
end
diff --git a/spree/core/spec/models/spree/stock/availability_validator_spec.rb b/spree/core/spec/models/spree/stock/availability_validator_spec.rb
index fc7fd8d5df..6f64b222a2 100644
--- a/spree/core/spec/models/spree/stock/availability_validator_spec.rb
+++ b/spree/core/spec/models/spree/stock/availability_validator_spec.rb
@@ -43,7 +43,7 @@ module Spree
context 'when supply is sufficient but product is not active' do
before do
- line_item.product.draft!
+ line_item.product.update!(status: 'draft')
end
it 'shows a message about product status and not quantity' do
diff --git a/spree/core/spec/workflows/spree/products/status_transitions_spec.rb b/spree/core/spec/workflows/spree/products/status_transitions_spec.rb
new file mode 100644
index 0000000000..d5190b00bc
--- /dev/null
+++ b/spree/core/spec/workflows/spree/products/status_transitions_spec.rb
@@ -0,0 +1,66 @@
+require 'spec_helper'
+
+RSpec.describe 'Spree::Products status workflows' do
+ let(:store) { Spree::Store.default }
+
+ describe Spree::Products::Activate do
+ let(:product) { create(:product, status: 'draft', store: store) }
+
+ it 'puts the product on sale' do
+ expect { described_class.call(product: product) }.to change { product.reload.status }.from('draft').to('active')
+ end
+
+ it 'publishes product.activated', events: true do
+ allow(product).to receive(:publish_event).with(anything)
+ expect(product).to receive(:publish_event).with('product.activated')
+
+ described_class.call(product: product)
+ end
+
+ context 'hooks' do
+ before { Spree.hooks.clear! }
+ after { Spree.hooks.clear! }
+
+ it 'runs the after_activate hook' do
+ seen = []
+ Spree.hooks.register('products.activate.after_activate') { |workflow| seen << workflow.product }
+
+ described_class.call(product: product)
+
+ expect(seen).to eq([product])
+ end
+
+ it 'can be vetoed' do
+ Spree.hooks.register('products.activate.validate') { |workflow| workflow.reject!('nope') }
+
+ result = described_class.call(product: product)
+
+ expect(result).not_to be_success
+ expect(product.reload.status).to eq('draft')
+ end
+ end
+ end
+
+ describe Spree::Products::Archive do
+ let(:product) { create(:product, status: 'active', store: store) }
+
+ it 'takes the product off sale' do
+ expect { described_class.call(product: product) }.to change { product.reload.status }.from('active').to('archived')
+ end
+
+ it 'publishes product.archived', events: true do
+ allow(product).to receive(:publish_event).with(anything)
+ expect(product).to receive(:publish_event).with('product.archived')
+
+ described_class.call(product: product)
+ end
+ end
+
+ describe Spree::Products::Draft do
+ let(:product) { create(:product, status: 'active', store: store) }
+
+ it 'returns the product to draft' do
+ expect { described_class.call(product: product) }.to change { product.reload.status }.from('active').to('draft')
+ end
+ end
+end