diff --git a/core/app/models/spree/return_authorization.rb b/core/app/models/spree/return_authorization.rb index 8aba85b7ee2..ea01f5dd420 100644 --- a/core/app/models/spree/return_authorization.rb +++ b/core/app/models/spree/return_authorization.rb @@ -22,13 +22,7 @@ class ReturnAuthorization < Spree::Base validate :must_have_shipped_units, on: :create validate :no_previously_exchanged_inventory_units, on: :create - state_machine initial: :authorized do - before_transition to: :canceled, do: :cancel_return_items - - event :cancel do - transition to: :canceled, from: :authorized, if: lambda { |return_authorization| return_authorization.can_cancel_return_items? } - end - end + include ::Spree::Config.state_machines.return_authorization extend DisplayMoney money_methods :pre_tax_total, :amount, :total_excluding_vat diff --git a/core/lib/spree/app_configuration.rb b/core/lib/spree/app_configuration.rb index 02d1249390e..31ce0cad7ef 100644 --- a/core/lib/spree/app_configuration.rb +++ b/core/lib/spree/app_configuration.rb @@ -434,6 +434,10 @@ def order_number_generator @order_number_generator ||= Spree::Order::NumberGenerator.new end + def state_machines + @state_machines ||= Spree::Core::StateMachines.new + end + def static_model_preferences @static_model_preferences ||= Spree::Preferences::StaticModelPreferences.new end diff --git a/core/lib/spree/core.rb b/core/lib/spree/core.rb index bed9d74ff73..c4a0753025f 100644 --- a/core/lib/spree/core.rb +++ b/core/lib/spree/core.rb @@ -84,6 +84,7 @@ class GatewayError < RuntimeError; end require 'spree/core/controller_helpers/store' require 'spree/core/controller_helpers/strong_parameters' require 'spree/core/role_configuration' +require 'spree/core/state_machines' require 'spree/core/stock_configuration' require 'spree/core/validators/email' require 'spree/permission_sets' diff --git a/core/lib/spree/core/state_machines.rb b/core/lib/spree/core/state_machines.rb new file mode 100644 index 00000000000..d6105bb506c --- /dev/null +++ b/core/lib/spree/core/state_machines.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +module Spree + module Core + class StateMachines + attr_writer :return_authorization + + def return_authorization + require 'spree/core/state_machines/base_return_authorization' + + @return_authorization ||= 'Spree::Core::StateMachines::BaseReturnAuthorization' + @return_authorization.constantize + end + end + end +end diff --git a/core/lib/spree/core/state_machines/base_return_authorization.rb b/core/lib/spree/core/state_machines/base_return_authorization.rb new file mode 100644 index 00000000000..90ccb1e6f46 --- /dev/null +++ b/core/lib/spree/core/state_machines/base_return_authorization.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module Spree + module Core + class StateMachines + module BaseReturnAuthorization + extend ActiveSupport::Concern + + included do + state_machine initial: :authorized do + before_transition to: :canceled, do: :cancel_return_items + + event :cancel do + transition to: :canceled, from: :authorized, if: lambda { |return_authorization| return_authorization.can_cancel_return_items? } + end + end + end + end + end + end +end