Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POC: extract state machines to a concern #26

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions core/app/models/spree/return_authorization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions core/lib/spree/app_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions core/lib/spree/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
16 changes: 16 additions & 0 deletions core/lib/spree/core/state_machines.rb
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions core/lib/spree/core/state_machines/base_return_authorization.rb
Original file line number Diff line number Diff line change
@@ -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