Senior & Expert RoR Developers Discussion Forum by Nyros Technologies

HIRE Ruby on Rails Expert Developers Programmers Coders From India
Ruby on Rails PHP .Net Developers Community, Nyros Technologies, Kakinada
 
Log in  or IF not a member please REGISTER
Username:
Password:   


Keyword
Log in | Profile 

acts_as_state_machine plugin

 
Post new topic   Reply to topic    Senior & Expert RoR Developers Discussion Forum by Nyros Technologies Index -> Open sources in ruby on rails
View previous topic :: View next topic  
Author Message
Divya



Joined: 14 Sep 2009
Posts: 57

PostPosted: Thu Dec 03, 2009 7:01 am    Post subject: acts_as_state_machine plugin Reply with quote

------------------------------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 .
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    Senior & Expert RoR Developers Discussion Forum by Nyros Technologies Index -> Open sources in ruby on rails
Page 1 of 1

 latest topics 
 Topics   Replies   Author   Views   Last Post 
No new posts Chat utility in a ROR application
0 shobana 2 Thu Sep 09, 2010 9:45 am
shobana View latest post
No new posts How to customize Google Maps InfoWindow using ym4r_gm
0 kalyanallampalli 54 Thu Sep 02, 2010 5:34 pm
kalyanallampalli View latest post
No new posts Drag and Drop file upload
0 umamageshwari 51 Wed Sep 01, 2010 9:36 am
umamageshwari View latest post
No new posts Flex On Rails
0 Raghu 48 Wed Sep 01, 2010 3:00 am
Raghu View latest post
No new posts MongoDB
0 Raghu 45 Tue Aug 31, 2010 3:30 pm
Raghu View latest post
No new posts Problem with Dreamweaver / java error
3 Raja 37 Tue Aug 31, 2010 3:24 pm
Raghu View latest post
No new posts Generating Model
0 phani.galla 44 Tue Aug 31, 2010 2:09 pm
phani.galla View latest post
No new posts Hide Panels using JQuery
0 Anu 48 Tue Aug 31, 2010 1:50 pm
Anu View latest post
No new posts Load a text file using ajax in jquery
0 Anu 38 Tue Aug 31, 2010 1:39 pm
Anu View latest post
No new posts Generating Migrations
0 phani.galla 31 Tue Aug 31, 2010 1:38 pm
phani.galla View latest post




Hire an expert Ruby on Rails developer / coder / programmer or development team from India now!!

Other Forums : PHP   ::   .Net   |   Free unlimited HTML CSS templates download

Nyros Technologies   |   Kakinada City Portal   |   Developers Blog   |   About Ruby on Rails Experts   |   More