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 validates_multiparameter_assignments plugin usage:
0 vijayalakshmi 27 Wed Jul 28, 2010 4:24 am
vijayalakshmi View latest post
No new posts paperclip plugin usage in rails:
0 vijayalakshmi 23 Wed Jul 28, 2010 4:07 am
vijayalakshmi View latest post
No new posts Polymorphism
1 phani.galla 46 Tue Jul 27, 2010 3:27 pm
umamahesh_nyros View latest post
No new posts Deploying Ruby on Rails Application with Passenger
2 sivakrishna.m 630 Tue Jul 27, 2010 2:16 pm
criether View latest post
No new posts Steps to implement star-rating using acts_as_rateable plugin
0 vijayalakshmi 46 Mon Jul 26, 2010 5:47 am
vijayalakshmi View latest post
No new posts Twitter Retweet Integartion
0 Anu 41 Thu Jul 22, 2010 12:27 pm
Anu View latest post
No new posts Usage of build method in rails
0 ktulasi 30 Thu Jul 22, 2010 12:19 pm
ktulasi View latest post
No new posts Facebook Like Integration in Rails
0 Anu 53 Thu Jul 22, 2010 12:16 pm
Anu View latest post
No new posts Fshare in your Rails Application.
0 Anu 39 Thu Jul 22, 2010 12:10 pm
Anu View latest post
No new posts Installation of Postgresql in windows.
1 swaminadhan 198 Thu Jul 15, 2010 10:17 am
proximity3 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