Your SlideShare is downloading. ×
Act as state machine
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Act as state machine

1,535
views

Published on

Act as state machine description

Act as state machine description

Published in: Technology

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,535
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Acts As State Machine rails plugin
    • What is State diagram ?
    • 2. State diagrams are used to give an abstract description of the behavior of a system. This behavior is analyzed and represented in series of events, that could occur in one or more possible states.
  • 3. Acts As State Machine rails plugin
    • Example of State Machine
  • 4. Acts As State Machine rails plugin
    • Class Article < ActiveRecord::Base
    • 5. include AASM
    • 6. aasm_column :state
    • 7. aasm_initial_state :created
    • 8. aasm_state :created
    • 9. aasm_state :published
    • 10. aasm_state :unpublished
    • 11. aasm_state :deleted
  • 12. Acts As State Machine rails plugin
      aasm_event :publish do transitions :to => :published, :from => [:created] end aasm_event :unpublish do transitions :to => :unpublished, :from => [:created, :published] end
  • 13. Acts As State Machine rails plugin
      aasm_event :delete do transitions :to => :deleted, :from => [:published, :unpublished] end aasm_states.each { |s| named_scope s, :conditions => { :state => s.to_s } } end
  • 14. Acts As State Machine rails plugin
    • article = Article.new
    • 15. article.state # nil
    • 16. article.save # true
    • 17. article.state # &quot;created&quot;
    • 18. article.created? # true
    • 19. article.published? # false
  • 20. Acts As State Machine rails plugin
      Create instance Method , to transition the model from one state to another.
    • article.publish!
    • 21. article.unpublish!
    • 22. article.delete!
  • 23. Acts As State Machine rails plugin
    • Callbacks:
    • 24. The state also comes with a few callbacks that can be used.
    • 25. 1. enter
    • 26. 2.after
    • 27. 3. exit
    • 28. state :published, :enter => :do_publish
  • 29. Acts As State Machine rails plugin
    • Guarding States
    • 30. Using :guard option you can add some sort of validation for a transition.
    • 31. Eg:
    • 32. aasm_event :publish do
    • 33. transitions :to => :published, :from => [:created], :guard => Proc.new { |article| article.valid?}
    • 34. end