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

Remove state machine gem from Spree::InventoryUnit #2668

Closed
Closed
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
127 changes: 103 additions & 24 deletions core/app/models/spree/inventory_unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,18 @@ module Spree
# Tracks the state of line items' fulfillment.
#
class InventoryUnit < Spree::Base
PRE_SHIPMENT_STATES = %w(backordered on_hand)
POST_SHIPMENT_STATES = %w(returned)
CANCELABLE_STATES = ['on_hand', 'backordered', 'shipped']
class InvalidStateChange < StandardError; end

ON_HAND = 'on_hand'
BACKORDERED = 'backordered'
RETURNED = 'returned'
SHIPPED = 'shipped'
CANCELED = 'canceled'
DEFAULT_STATES = [ON_HAND, BACKORDERED, RETURNED, SHIPPED, CANCELED]

PRE_SHIPMENT_STATES = [BACKORDERED, ON_HAND]
POST_SHIPMENT_STATES = [RETURNED]
CANCELABLE_STATES = [ON_HAND, BACKORDERED, SHIPPED]

belongs_to :variant, -> { with_deleted }, class_name: "Spree::Variant", inverse_of: :inventory_units
belongs_to :shipment, class_name: "Spree::Shipment", touch: true, inverse_of: :inventory_units
Expand All @@ -26,18 +35,19 @@ def order=(_)
end

validates_presence_of :shipment, :line_item, :variant
validate :is_valid_state?

before_destroy :ensure_can_destroy

scope :pending, -> { where pending: true }
scope :backordered, -> { where state: 'backordered' }
scope :on_hand, -> { where state: 'on_hand' }
scope :backordered, -> { where state: BACKORDERED }
scope :on_hand, -> { where state: ON_HAND }
scope :pre_shipment, -> { where(state: PRE_SHIPMENT_STATES) }
scope :shipped, -> { where state: 'shipped' }
scope :shipped, -> { where state: SHIPPED }
scope :post_shipment, -> { where(state: POST_SHIPMENT_STATES) }
scope :returned, -> { where state: 'returned' }
scope :canceled, -> { where(state: 'canceled') }
scope :not_canceled, -> { where.not(state: 'canceled') }
scope :returned, -> { where state: RETURNED }
scope :canceled, -> { where(state: CANCELED) }
scope :not_canceled, -> { where.not(state: CANCELED) }
scope :cancelable, -> { where(state: Spree::InventoryUnit::CANCELABLE_STATES, pending: false) }
scope :backordered_per_variant, ->(stock_item) do
includes(:shipment, :order)
Expand All @@ -59,24 +69,81 @@ def order=(_)

scope :shippable, -> { on_hand }

# state machine (see http://github.com/pluginaweek/state_machine/tree/master for details)
state_machine initial: :on_hand do
event :fill_backorder do
transition to: :on_hand, from: :backordered
end
after_transition on: :fill_backorder, do: :fulfill_order
def fill_backorder!
fill_backorder || raise(InvalidStateChange)
end

event :ship do
transition to: :shipped, if: :allow_ship?
end
def fill_backorder
return false unless can_fill_backorder?
change_state!(ON_HAND)
fulfill_order
true
end

event :return do
transition to: :returned, from: :shipped
end
def can_fill_backorder?
backordered?
end

event :cancel do
transition to: :canceled, from: CANCELABLE_STATES.map(&:to_sym)
end
def on_hand?
state == ON_HAND
end

def backordered?
state == BACKORDERED
end

def ship!
ship || raise(InvalidStateChange)
end

def ship
return false unless can_ship?
change_state!(SHIPPED)
true
end

def can_ship?
allow_ship?
end

def shipped?
state == SHIPPED
end

def return!
self.return || raise(InvalidStateChange)
end

def return
return false unless can_return?
change_state!(RETURNED)
true
end

def can_return?
shipped?
end

def returned?
state == RETURNED
end

def cancel!
cancel || raise(InvalidStateChange)
end

def cancel
return false unless can_cancel?
change_state!(CANCELED)
true
end

def can_cancel?
CANCELABLE_STATES.include?(state)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Later we should change CANCELABLE_STATES to be a method as stores might want to change that.

Ideally this would even be configurable, so stores won't have to class_eval or Module#prepend this class

end

def canceled?
state == CANCELED
end

# Updates the given inventory units to not be pending.
Expand Down Expand Up @@ -155,5 +222,17 @@ def ensure_can_destroy
throw :abort
end
end

def change_state!(new_state)
previous_state = state
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we try to make use of ActiveModel::Dirty here?

http://api.rubyonrails.org/classes/ActiveModel/Dirty.html

return if new_state == previous_state
update!(state: new_state)
end

def is_valid_state?
unless DEFAULT_STATES.include?(state)
errors.add(:state, "Invalid state")
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class AddDefaultStateToInventoryUnit < ActiveRecord::Migration[5.1]
def change
change_column_default(:spree_inventory_units, :state, 'on_hand')
end
end
Loading