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
Retire the state machine on PriceList
Damian Legawiec8333e380d4a4
diff --git a/spree/api/app/controllers/spree/api/v3/admin/price_lists_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/price_lists_controller.rbindex 604d72ab28..5142d6e5c2 100644--- a/spree/api/app/controllers/spree/api/v3/admin/price_lists_controller.rb+++ b/spree/api/app/controllers/spree/api/v3/admin/price_lists_controller.rb@@ -43,17 +43,17 @@ module Spree# PATCH /api/v3/admin/price_lists/:id/activate#- # State transition: draft|inactive → active (or → scheduled when- # `starts_at` is in the future). Mirrors the old Rails admin's- # "Activate" button which automatically scheduled future lists.+ # draft|inactive → active, or → scheduled when `starts_at` is in the+ # future. The workflow decides which, so a caller never has to.def activateauthorize! :update, @resource- event = @resource.starts_at.present? && @resource.starts_at.future? ? :schedule : :activate- if @resource.send(event)+ result = Spree.price_list_activate_workflow.call(price_list: @resource)++ if result.success?render json: serialize_resource(@resource)else- render_validation_error(@resource.errors)+ render_service_error(result.error)endend@@ -61,10 +61,12 @@ module Spreedef deactivateauthorize! :update, @resource- if @resource.deactivate+ result = Spree.price_list_deactivate_workflow.call(price_list: @resource)++ if result.success?render json: serialize_resource(@resource)else- render_validation_error(@resource.errors)+ render_service_error(result.error)endenddiff --git a/spree/core/app/models/spree/price_list.rb b/spree/core/app/models/spree/price_list.rbindex 3b3a4baeae..7010035008 100644--- a/spree/core/app/models/spree/price_list.rb+++ b/spree/core/app/models/spree/price_list.rb@@ -92,18 +92,31 @@ module Spree.where('ends_at IS NULL OR ends_at >= ?', current_time)}- state_machine :status, initial: :draft do- event :activate do- transition to: :active- end+ # No state machine — the transitions live in the Spree::PriceLists+ # workflows (docs/plans/6.0-service-workflows.md).+ include Spree::HasStatus+ has_status :draft, :active, :inactive, :scheduled, default: :draft++ # @deprecated Call Spree.price_list_activate_workflow — removed in 6.1.+ # Kept literal: the old event always went straight to active, and the+ # workflow schedules a future-dated list instead. A caller relying on+ # this shell gets what it always got.+ def activate+ Spree::Deprecation.warn('Spree::PriceList#activate is deprecated and will be removed in Spree 6.1. Call Spree.price_list_activate_workflow instead.')+ update(status: 'active')+ end- event :deactivate do- transition to: :inactive- end+ # @deprecated Call Spree.price_list_deactivate_workflow — removed in 6.1.+ def deactivate+ Spree::Deprecation.warn('Spree::PriceList#deactivate is deprecated and will be removed in Spree 6.1. Call Spree.price_list_deactivate_workflow instead.')+ Spree.price_list_deactivate_workflow.call(price_list: self).success?+ end- event :schedule do- transition to: :scheduled- end+ # @deprecated Call Spree.price_list_activate_workflow — removed in 6.1.+ # It picks scheduled or active from the dates itself.+ def schedule+ Spree::Deprecation.warn('Spree::PriceList#schedule is deprecated and will be removed in Spree 6.1. Call Spree.price_list_activate_workflow instead.')+ update(status: 'scheduled')end# Returns price lists applicable for a given pricing contextdiff --git a/spree/core/db/sample_data/wholesale.rb b/spree/core/db/sample_data/wholesale.rbindex b45e0a2969..a02736ea63 100644--- a/spree/core/db/sample_data/wholesale.rb+++ b/spree/core/db/sample_data/wholesale.rb@@ -68,4 +68,4 @@ if blank_prices.any?price_list.bulk_update_prices(rows) if rows.any?end-price_list.activate if price_list.can_activate?+Spree.price_list_activate_workflow.call(price_list: price_list) unless price_list.active?diff --git a/spree/core/spec/models/spree/price_list_spec.rb b/spree/core/spec/models/spree/price_list_spec.rbindex a352af81ea..b9a410e2f5 100644--- a/spree/core/spec/models/spree/price_list_spec.rb+++ b/spree/core/spec/models/spree/price_list_spec.rb@@ -12,32 +12,19 @@ describe Spree::PriceList, type: :model doendend- describe 'state_machine' do+ describe 'status' dolet(:price_list) { create(:price_list) }- it 'has initial status of draft' do- expect(price_list.status).to eq('draft')- end-- describe '#activate' do- it 'transitions to active' do- price_list.activate- expect(price_list.status).to eq('active')- end+ it 'has no state machine' do+ expect(described_class).not_to respond_to(:state_machines)end- describe '#deactivate' do- it 'transitions to inactive' do- price_list.deactivate- expect(price_list.status).to eq('inactive')- end+ it 'has initial status of draft' do+ expect(price_list.status).to eq('draft')end- describe '#schedule' do- it 'transitions to scheduled' do- price_list.schedule- expect(price_list.status).to eq('scheduled')- end+ it 'rejects an unknown status' do+ expect(build(:price_list, status: 'nonsense')).not_to be_validendend@@ -113,7 +100,7 @@ describe Spree::PriceList, type: :model dolet(:context) { Spree::Pricing::Context.new(variant: variant, currency: 'USD', store: store) }context 'when price list is inactive' do- before { price_list.deactivate }+ before { Spree.price_list_deactivate_workflow.call(price_list: price_list) }it 'returns false' doexpect(price_list.applicable?(context)).to be falsediff --git a/spree/core/spec/workflows/spree/price_lists/status_transitions_spec.rb b/spree/core/spec/workflows/spree/price_lists/status_transitions_spec.rbnew file mode 100644index 0000000000..092b99dba5--- /dev/null+++ b/spree/core/spec/workflows/spree/price_lists/status_transitions_spec.rb@@ -0,0 +1,27 @@+require 'spec_helper'++RSpec.describe 'Spree::PriceLists status workflows' do+ describe Spree::PriceLists::Activate do+ it 'activates a list that is already due' do+ price_list = create(:price_list)++ expect { described_class.call(price_list: price_list) }.to change { price_list.reload.status }.from('draft').to('active')+ end++ it 'schedules a list whose start date has not arrived' do+ price_list = create(:price_list, starts_at: 1.week.from_now)++ described_class.call(price_list: price_list)++ expect(price_list.reload.status).to eq('scheduled')+ end+ end++ describe Spree::PriceLists::Deactivate do+ it 'takes the list out of effect' do+ price_list = create(:price_list, :active)++ expect { described_class.call(price_list: price_list) }.to change { price_list.reload.status }.from('active').to('inactive')+ end+ end+end