-
-
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::Payment #2664
Conversation
005982d
to
0b85aba
Compare
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.
Nice work.
The same change requests from #2656 apply here as well, though.
0b85aba
to
924e439
Compare
COMPLETED = 'completed' | ||
INVALID = 'invalid' | ||
VOID = 'void' | ||
FAILED = 'failed' |
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.
We should freeze these strings since they shouldn't be mutatable.
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.
Will do!
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.
Oh, just realised the frozen_string_literal
at the top of the file takes care of this already.
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.
Ah, right. We should delete all of the other freeze calls then :p.
core/app/models/spree/payment.rb
Outdated
@@ -43,6 +53,7 @@ class Payment < Spree::Base | |||
validates :amount, numericality: true | |||
validates :source, presence: true, if: :source_required? | |||
validates :payment_method, presence: true | |||
validates :state, inclusion: { in: [PENDING, PROCESSING, CHECKOUT, COMPLETED, INVALID, VOID, FAILED] } |
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.
I feel like a method should be used here. So that way people can easily extend payment states if they need to. Perhaps add a config to Solidus for this?
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.
Very good point Ben. @jgayfer would you mind to address this?
7140c8b
to
924e439
Compare
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.
Same two nits mentioned in #2656 apply here as well, but still not mandatory
|
||
def change_state!(new_state) | ||
previous_state = state | ||
return if new_state == previous_state |
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.
Can we try to make use of ActiveModel::Dirty
here?
|
||
class AddDefaultStateToPayment < ActiveRecord::Migration[5.1] | ||
def change | ||
change_column_default(:spree_payments, :state, Spree::Payment::CHECKOUT) |
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.
We should use the value of the constant here so we do not rely on the code may change in the future.
924e439
to
49fc12d
Compare
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.
@BenMorganIO made a good point. @jgayfer could you please extract this state
validation into a method? Thanks
core/app/models/spree/payment.rb
Outdated
@@ -43,6 +53,7 @@ class Payment < Spree::Base | |||
validates :amount, numericality: true | |||
validates :source, presence: true, if: :source_required? | |||
validates :payment_method, presence: true | |||
validates :state, inclusion: { in: [PENDING, PROCESSING, CHECKOUT, COMPLETED, INVALID, VOID, FAILED] } |
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.
Very good point Ben. @jgayfer would you mind to address this?
49fc12d
to
0f2a44c
Compare
While this spec will pass, any additional attempts to save the payment in question would fail as the Payment was not able to save on create due to it being invalid (missing a source).
0f2a44c
to
d59ee42
Compare
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.
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.
d59ee42
to
7be4165
Compare
@tvdeyen Changes have been made 👍 |
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.
Thanks 👌
Are we able to place state machine specific methods in their own class? I feel like that would make managing the model so much easier. |
@BenMorganIO a |
reopened in #3039 |
This PR removes the state machine gem from
Spree::Payment
while keeping the external API intact. The logic that was previously hidden within the state machine is now contained within theSpree::Payment
model.Please see #2656 for the rationale behind these changes.