State Machine Simulator
April 1st, 2008
If you need to create a wizard, with forward and back buttons for several different steps, a great way to do it is to use the acts_as_state_machine plugin.
But for just two or three steps, loading a whole plugin seems like overkill. Instead of loading a whole plugin, you can simulate the same functionality by dropping next!
and previous!
methods into your model like so:
# State Machine
# Guilt-free stateful modeling!
# These two simple methods simulate all the goodness of
# acts_as_state_machine without the added fat of an included plugin.
def next!
next_step = case self.state
when "step1"
"step2"
when "step2"
"step3"
when "step3"
"step3"
end
self.update_attribute(:state, next_step)
end
def previous!
previous_step = case self.state
when "step1"
"step1"
when "step2"
"step1"
when "step3"
"step2"
end
self.update_attribute(:state, previous_step)
end
def current_step
@current_step ||= self.state
end
Subscribe
Network
Archives
- July 2009 (1)
- May 2009 (4)
- March 2009 (1)
- February 2009 (1)
- December 2008 (2)
- November 2008 (1)
- October 2008 (7)
- September 2008 (4)
- August 2008 (4)
- June 2008 (2)
- April 2008 (1)
- March 2008 (1)
- February 2008 (5)
- January 2008 (1)
- December 2007 (1)
- November 2007 (4)
- October 2007 (1)
- September 2007 (1)
- August 2007 (3)
- July 2007 (2)
- June 2007 (5)
- May 2007 (6)
- April 2007 (3)
- March 2007 (4)
- February 2007 (5)
- January 2007 (5)
- December 2006 (6)
- November 2006 (1)
- October 2006 (8)
- September 2006 (12)
- August 2006 (7)
- July 2006 (6)
- June 2006 (6)
- May 2006 (2)
- April 2006 (17)
- March 2006 (15)
- February 2006 (7)
- January 2006 (12)
- December 2005 (25)
- November 2005 (11)
- October 2005 (3)
- September 2005 (2)
- August 2005 (4)
- July 2005 (5)
- June 2005 (1)