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

Dynamic state machine proposal #1743

Closed
wants to merge 12 commits into
from

Conversation

Projects
None yet
4 participants
@radar
Member

radar commented Jul 10, 2012

This issue should address #1418.

See the code in this gist to see what it looks like. As I explain there:

The code works by calling the checkout_flow method which clears all current transitions. The go_to_state method then defines transitions for each step of the process. The first call will transition from cart, and subsequent calls will transition from the state before it.

If a state has a condition on it, then a record of all previous states are kept until there is a state that has no condition. Once this happens, the code will define state transitions for all state paths for the states in between. For instance, the follow transitions will be defined between delivery and complete:

  • Delivery to payment
  • Delivery to confirm
  • Delivery to complete
  • Payment to confirm
  • Payment to complete
  • Confirm to complete

The remove method will remove a transition that has previously been either automatically or manually defined. In the case of this state machine, the delivery to confirm transition has been automatically defined, but we don't want it. So we get rid of it. As in the above example, the "delivery to confirm" transition will be removed.

What this will allow for is for dynamic state machine definitions for Order objects by re-defining the checkout_flow block on them and using the new state definition API.

This issue is just opening that implementation up for consideration. Does it look sensible? Does it fit the use cases? That kind of thing.

Thoughts?

@radar radar closed this Jul 10, 2012

@radar radar reopened this Jul 10, 2012

@@ -450,6 +413,14 @@ def payment_method
end
end
def process_payments!

This comment has been minimized.

@radar

radar Jul 10, 2012

Member

This just moves the begin/rescue logic out of the state machine and into this method, which I think is neater.

@radar

radar Jul 10, 2012

Member

This just moves the begin/rescue logic out of the state machine and into this method, which I think is neater.

@radar

View changes

Show outdated Hide outdated core/app/models/spree/order.rb
@@ -576,13 +547,13 @@ def require_email
end
def has_available_shipment
return unless :address == state_name.to_sym
return unless address?

This comment has been minimized.

@radar

radar Jul 10, 2012

Member

mmm, query methods

@radar

radar Jul 10, 2012

Member

mmm, query methods

end
end
def self.define_state_machine!

This comment has been minimized.

@radar

radar Jul 10, 2012

Member

This method is the magic. This is called when Core::Engine does a to_prepare. This means that you can re-define the next state transitions as many times as you wish and every time to_prepare is called, the state machine will re-define the transitions.

@radar

radar Jul 10, 2012

Member

This method is the magic. This is called when Core::Engine does a to_prepare. This means that you can re-define the next state transitions as many times as you wish and every time to_prepare is called, the state machine will re-define the transitions.

@radar

View changes

Show outdated Hide outdated core/app/models/spree/order/checkout.rb
klass.next_event_transitions.each { |t| transition(t.merge(:on => :next)) }
# Persist the state on the order
after_transition do |order|

This comment has been minimized.

@radar

radar Jul 10, 2012

Member

I am not sure if this is actually required any more.

@radar

radar Jul 10, 2012

Member

I am not sure if this is actually required any more.

@radar

View changes

Show outdated Hide outdated core/app/models/spree/order/checkout.rb
def self.go_to_state(name, options={})
if options[:if]
previous_states.each do |state|

This comment has been minimized.

@radar

radar Jul 10, 2012

Member

I thought about moving this out into its own method but decided against it because I am lazy.

@radar

radar Jul 10, 2012

Member

I thought about moving this out into its own method but decided against it because I am lazy.

end
end
def self.remove_transition(options={})

This comment has been minimized.

@radar

radar Jul 10, 2012

Member

This was previously called remove, but I've renamed it to remove_transition so that there isn't a "mysterious" remove method on the Order class, and so that it fits more into the same theme as find_transition and add_transition.

@radar

radar Jul 10, 2012

Member

This was previously called remove, but I've renamed it to remove_transition so that there isn't a "mysterious" remove method on the Order class, and so that it fits more into the same theme as find_transition and add_transition.

end
end
def self.next_event_transitions

This comment has been minimized.

@radar

radar Jul 10, 2012

Member

This used to be called transitions. Same reasons for renaming the remove method.

@radar

radar Jul 10, 2012

Member

This used to be called transitions. Same reasons for renaming the remove method.

@radar

This comment has been minimized.

Show comment
Hide comment
@radar

radar Jul 10, 2012

Member

Just a note: this doesn't actually change the checkout breadcrumbs yet. I've not yet begun work on that, but will probably knock it off tomorrow.

Member

radar commented Jul 10, 2012

Just a note: this doesn't actually change the checkout breadcrumbs yet. I've not yet begun work on that, but will probably knock it off tomorrow.

radar added some commits Jul 10, 2012

Implemented checkout_steps method
This replaces the code duplication in checkout_helper to determine what steps to show for an order during the checkout process
@radar

This comment has been minimized.

Show comment
Hide comment
@radar

radar Jul 10, 2012

Member

Checkout breadcrumbs are in @256d28f. The build is passing on Travis.

Member

radar commented Jul 10, 2012

Checkout breadcrumbs are in @256d28f. The build is passing on Travis.

@radar

This comment has been minimized.

Show comment
Hide comment
@radar

radar Jul 13, 2012

Member

Kicking the tyres some more on this today.

Member

radar commented Jul 13, 2012

Kicking the tyres some more on this today.

@radar

This comment has been minimized.

Show comment
Hide comment
@radar

radar Jul 26, 2012

Member

I can't think of anything more to do on this. Can @BDQ, @cmar, @LBRapid, @joneslee85 and/or @schof look this over and see if they can break it?

Member

radar commented Jul 26, 2012

I can't think of anything more to do on this. Can @BDQ, @cmar, @LBRapid, @joneslee85 and/or @schof look this over and see if they can break it?

@joneslee85

This comment has been minimized.

Show comment
Hide comment
@joneslee85

joneslee85 Jul 26, 2012

Member

@radar Will spend sometime on this this weekend, see if I could break it

Member

joneslee85 commented Jul 26, 2012

@radar Will spend sometime on this this weekend, see if I could break it

@schof

This comment has been minimized.

Show comment
Hide comment
@schof

schof Aug 6, 2012

Member

I'm ok to merge this as long as we update the documentation and release notes adequately (plus resolve conflicts of course.)

Member

schof commented Aug 6, 2012

I'm ok to merge this as long as we update the documentation and release notes adequately (plus resolve conflicts of course.)

radar added a commit that referenced this pull request Aug 8, 2012

@radar

This comment has been minimized.

Show comment
Hide comment
@radar

radar Aug 8, 2012

Member

Merged to master with @fa1d66c. Tests are running on master now. When they're good, I'll update the Checkout Customization guide appropriately.

Member

radar commented Aug 8, 2012

Merged to master with @fa1d66c. Tests are running on master now. When they're good, I'll update the Checkout Customization guide appropriately.

@radar radar closed this Aug 8, 2012

@gsnarawat

This comment has been minimized.

Show comment
Hide comment
@gsnarawat

gsnarawat Feb 15, 2013

I want to remove the Shipping page validations.. I used this code but still there is validations exist. plz help.

checkout_flow do
go_to_state :address, :if => lambda { |order| order.payment_required? }
go_to_state :payment, :if => lambda { |order| order.payment_required? }
go_to_state :confirm, :if => lambda { |order| order.confirmation_required? }
go_to_state :complete
remove_transition :from => :delivery, :to => :confirm
end

I want to remove the Shipping page validations.. I used this code but still there is validations exist. plz help.

checkout_flow do
go_to_state :address, :if => lambda { |order| order.payment_required? }
go_to_state :payment, :if => lambda { |order| order.payment_required? }
go_to_state :confirm, :if => lambda { |order| order.confirmation_required? }
go_to_state :complete
remove_transition :from => :delivery, :to => :confirm
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment