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

Conversation

jgayfer
Copy link

@jgayfer jgayfer commented Apr 3, 2018

This PR removes the state machine gem from Spree::InventoryUnit while keeping the external API intact. The logic that was previously hidden within the state machine is now contained within the Spree::InventoryUnit model.

Please see #2656 for the rationale behind these changes.

Copy link
Member

@tvdeyen tvdeyen left a comment

Choose a reason for hiding this comment

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

Some notes about configurable states and the same nits from #2656

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


PRE_SHIPMENT_STATES = [BACKORDERED, ON_HAND]
POST_SHIPMENT_STATES = [RETURNED]
CANCELABLE_STATES = [ON_HAND, BACKORDERED, SHIPPED]
Copy link
Member

Choose a reason for hiding this comment

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

I know this is just keeping things like before, but we should consider to make this configurable in the future. Either via a method or configuration.

@jgayfer jgayfer force-pushed the remove_inventory_unit_state_machine branch from 3af5fb6 to 30765e4 Compare April 5, 2018 18:54
@jgayfer
Copy link
Author

jgayfer commented Apr 6, 2018

@tvdeyen Made some of the requested changes! See my comments on #2656.

Copy link
Member

@tvdeyen tvdeyen left a comment

Choose a reason for hiding this comment

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

Same nits mentioned in #2656 apply here as well, but still not mandatory

@@ -155,5 +221,11 @@ 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


class AddDefaultStateToInventoryUnit < ActiveRecord::Migration[5.1]
def change
change_column_default(:spree_inventory_units, :state, Spree::InventoryUnit::ON_HAND)
Copy link
Member

Choose a reason for hiding this comment

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

We should use the value of the constant here so we do not rely on the code may change in the future.

@jgayfer jgayfer force-pushed the remove_inventory_unit_state_machine branch from 30765e4 to 4e56678 Compare May 22, 2018 16:22
@jgayfer
Copy link
Author

jgayfer commented May 22, 2018

@tvdeyen I've changed the database migration to use the actual value. Same question applies as in #2656 about ActiveModel::Dirty though.

Copy link
Member

@tvdeyen tvdeyen left a comment

Choose a reason for hiding this comment

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

Would you please extract this state validation into a method? Thanks

@@ -26,18 +34,19 @@ def order=(_)
end

validates_presence_of :shipment, :line_item, :variant
validates :state, inclusion: { in: [ON_HAND, BACKORDERED, RETURNED, SHIPPED, CANCELED] }
Copy link
Member

Choose a reason for hiding this comment

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

The states may change for stores. We should allow them to overwrite a method instead.

@jgayfer jgayfer force-pushed the remove_inventory_unit_state_machine branch from 4e56678 to 27aed26 Compare June 7, 2018 18:34
@jgayfer
Copy link
Author

jgayfer commented Jun 7, 2018

@tvdeyen Made the requested changes. Check out my comment on #2656.

Copy link
Member

@tvdeyen tvdeyen left a comment

Choose a reason for hiding this comment

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

Same change requests as in #2656

The state machine is a problem for a few reasons: transitions are hard
to understand and debug, transition order is tough to control, and
database transactions during a transition aren't well understood.
Removing it brings increased code clarity, as well as the ability to
more easily extend the functionality that was previously hidden by the
state machine.
@jgayfer jgayfer force-pushed the remove_inventory_unit_state_machine branch from 27aed26 to 57c67fd Compare June 8, 2018 16:28
@jgayfer
Copy link
Author

jgayfer commented Jun 8, 2018

@tvdeyen Changes have been made 👍

Copy link
Member

@tvdeyen tvdeyen left a comment

Choose a reason for hiding this comment

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

Thanks 👌

@kennyadsl
Copy link
Member

reopened in #3041

@kennyadsl kennyadsl closed this Jan 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants