-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
jgayfer
wants to merge
1
commit into
solidusio:master
from
jgayfer:remove_inventory_unit_state_machine
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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) | ||
|
@@ -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) | ||
end | ||
|
||
def canceled? | ||
state == CANCELED | ||
end | ||
|
||
# Updates the given inventory units to not be pending. | ||
|
@@ -155,5 +222,17 @@ def ensure_can_destroy | |
throw :abort | ||
end | ||
end | ||
|
||
def change_state!(new_state) | ||
previous_state = state | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we try to make use of |
||
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 |
7 changes: 7 additions & 0 deletions
7
core/db/migrate/20180405181459_add_default_state_to_inventory_unit.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
orModule#prepend
this class