Divya
Joined: 14 Sep 2009 Posts: 57
|
Posted: Thu Dec 03, 2009 7:01 am Post subject: acts_as_state_machine plugin |
|
|
------------------------------acts_as_state_machine-------------------------------------------
This act gives an Active Record model the ability to act as a finite state machine (FSM).
A finite state machine (FSM) is a model of behavior composed of a finite number of states, transitions between those states, and actions. It is similar to a "flow graph" where we can inspect the way in which the logic runs when certain conditions are met.
Why would we use a " Finite State Machine? "
If, your model has a finite number of various states, and you want an easy way for callbacks to be done. Callbacks can be used to notify, validate, increment, anything, when your model changes state.
This plugin, for a Rails application can be installed with one simple command, provides an engine for dealing with different "states" of a database record.
Basically what it means is that frequently you will have database records that will need to be assigned a certain state, or status. For example, let's say that you have user records in a database, and that you want to be able to assign certain"states" to users, such as active and suspended.
We have always tackled with boolean fields in a database, or even an integer field where various numbers represent various states. A "1" might represent active, whereas a zero would represent inactive, and so on.
So, enter this glorious plugin that let's you very simply define states for your records. In addition it, let's you define transitions from state to state, so that you can control how records move from state to state, and even trigger methods when a state change occurs.
In an effort to illustrate a simple example, take a look at this,
So first step you need to do was ,
Step 1:
ruby script/plugin install http://elitists.textdriven.com/svn/plugins/acts_as_state_machine/trunk/
Step 2:
In your model
class Post < ActiveRecord::Base
acts_as_state_machine :initial => :pending, :column => 'state'
state :pending
state :approve
state :edittoapprove
event :pending do
transitions :from => :pending, :to => :approve
end
event :approve do
transitions :from => :approve, :to => :edittoapprove
end
validates_presence_of :title
validates_presence_of :description
end
Note: "The plug-in makes an assumption that the state of your model is saved in field called state. This can be replaced by adding the additional option :column => 'field'. "
Here i am using a user posts to be displayed , So i have taken a table "posts" and get with fields :title, :description, :user_id,:state,
So that i am assigning three states state :pending, :approve, :edittoapprove
And the transitions are done from " pending to approve " and "approve to edittoapprove "
Step 3:
So in my controller i have been using the methods as
def post_appr
@post = Post.find(params[:id])
@post.pending!
redirect_to :action => 'display_posts' and return
end
def edit_appr
@post=Post.find(params[:id])
@post.approve!
redirect_to :action => 'display_posts' and return
end
See here we have been using the "events" for the states.
When we define an event in our model, it creates a method with the same name and an exclamation point. So approve can be triggered by approve! in our controllers.
"pending! , approve! " are the two events. that are declared in model.
Step 4:
In view you need to place
<table>
<tr><td>UserId</td><td>Title</td><td>Description</td><td>State</td><td colspan="2">Actions</td></tr>
<% for post in @posts %>
<tr><td><%= post.user_id %></td>
<td><%= h(post.title) %></td>
<td><%= h(post.description) %></td>
<td><%= post.state %></td><br><br>
<td colspan="2"><% if post.state == "pending" %>
<%= link_to 'Approve' , :action=>"post_appr", :id=>post.id %>
<% elsif post.state == "approve" %>
<%= link_to "edit to approve", :action=>"edit_appr",:id=>post.id %> <% end %></td></tr>
<% end %>
</table>
So that the output you looks like as
id title Description State Actions
1 ghgfhgf hgfhfghfgh approve edit to approve
So in your database the field of your state looks like "pending" . When you click the link it will get "approve". Or if the user want to edit the post it wiil show "edit to approve " in your database.
Advantage is it, let's you define transitions from state to state, so that you can control how records move from state to state, and even trigger methods when a state change occurs.
Thank you,
Divya . |
|