The Concepts : State and continuation

There is a mechanism available in ActionFlow which enables users to store data inside what is called a flow state. It is imlemented as a Hash object which offers a historical snapshot of himself according to a user’S position inside a flow. This allows users to hit the back button and restore exactly the flow state they were in at that precise point in the flow.

In your ActionFlow controller, to access the current state data, simply use the state attribute. The state attribute can be accessed the same way as the session. It’s nothing more than a Hash object after all.
class MyController < ActionFlow::Base
 
  def initialize
    # Some mapping here
  end
 
 
  def step_1
 
    # Store data relevant to the current state
    state[:my_variable] = :some_value
 
    render state[:my_variable]
 
  end
 
  
  def step_2
 
    # print the state variable value
    render state[:my_variable]
 
  end
 
 
  def step_3
 
    # Modify the state data
    state[:my_variable] = :some_other_value
 
    render state[:my_variable]
 
  end
 
end

In the previous example, going through this flow would print, in order, the following.

Step 1

some_value

Next Step -> Step 2

some_value

Next Step -> Step 3

some_other_value

Hitting Back Button -> Step 2

some_value

How it's done

The mechanism underneath this is really quite simple. The view which submits events to the controller has to send back a flow execution key which tells the ActionFlow Controller two things :

  • The flow id
  • The state id

Every time that the flow receives a new request originating from an already existing flow, meaning that a flow execution key was submitted, the current content of the state attribute is cloned to a new placeholder which corresponds to the state id part within the key. Therefore, we are able to restore the contents of the state hash if the user gets back in the flow.

As with a standard Hash, whether you use a Symbol or a String to store your data is relevant. Storing data inside the state using a Symbol and trying to retrieve it with a String will return nil.

See the Session Handler page for more details about the state persistence mechanism.

~~DISCUSSION~~

 
state.txt · Last modified: 2007/06/06 16:50 by lucboudreau
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki

Creative Commons License
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 License.