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

Storing transitions to memory #219

Open
jdliaw opened this issue Jul 15, 2016 · 1 comment
Open

Storing transitions to memory #219

jdliaw opened this issue Jul 15, 2016 · 1 comment

Comments

@jdliaw
Copy link

jdliaw commented Jul 15, 2016

We wanted to use this state machine as only in memory for models whose state history we were not as concerned with. Without specifying a transition_class, transitions are stored to memory, but the current_state, transition_to methods etc. look into the state_machine.history to determine the current_state or whether a transition can be made. This history doesn't exist without the transition_class so the state machine was not reading from memory where the transitions were being stored.

As a fix, our state_machine method looked like this:

def state_machine
  @_state_machine ||= begin
     @state_machine = OrderStateMachine.new(self)
     if state
       @state_machine.history << Statesman::Adapters::MemoryTransition.new(state, 0)
     end
    @state_machine
  end
end

This was used in multiple places for us, so it could be useful for others as well, who want to save their transitions in memory.

@nicolas-besnard
Copy link

nicolas-besnard commented Oct 19, 2016

You can also put this directly on your state machine:

def after_initialize
  history << @transition_class.new(object.state || self.class.initial_state, 0)
end

Maybe we should try to work on a PR to add this to the documentation.

(all credits to @RobertAudi)

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

No branches or pull requests

2 participants