-2

Since I am new to ROR, I just want to know about the Gem AASM. Actually, I have Ruby code which is already built with AASM Gem.

It's hard for me to understand that why they are using this Gem here.

I just want to know the basic ideas of why/when/where can we use this Gem.

Thanks in Advance.

  • 1
    The [project page](https://github.com/aasm/aasm) is a good place to start. This [SO post](http://stackoverflow.com/a/256011/85125) also has good information on uses for state machines. – Bart Jedrocha Jan 27 '16 at 19:45
  • Your question is too broad. We have no idea what your level of expertise is, so any explanation would have to start at the basics and move up, which would be a tutorial. Stack Overflow isn't a substitute for your own research; Please tell us what you've tried and why that didn't help. Please read "[ask]" and http://meta.stackoverflow.com/q/261592/128421. – the Tin Man Jan 27 '16 at 22:38

1 Answers1

2

The intention is to allow defined values for an attribute only and allow predefined transitions from one state to another only. For example, state A can be transferred in state B or state C, but state B can not be transferred in state C. In addition AASM offers some callback methods to controll transitions or do something before or after a transition.

I used AASM within a user model to ensure that the user states can only be changed on predefined ways. See the example:

# THE STATE MACHINE ----------------------------------------------------------
INITIAL_STATE = :pending
STATES = {
  INITIAL_STATE    => 'new user accounts initial state',
  :waiting         => 'new user account is waiting for confirmation',
  :confirmed       => 'user confirmed the registration',
  :changing_email  => 'user changed its email address',
  :blocked         => 'user can not log in',
  :anonymized      => 'personal user data deleted',
  :not_deletable   => 'can not delete user, because he has some content'
}

include AASM
attr_protected :state
aasm({:column => :state}) do
  STATES.keys.each do |key|
    state(key, (key == INITIAL_STATE ? {:initial => true} : {}))
  end

  # creates instance methods confirm, confirm! and confirmed? for later usage
  event :confirm do
    transitions :to => :confirmed, :from => :waiting
    transitions :to => :confirmed, :from => :blocked
    transitions :to => :confirmed, :from => :changing_email
    transitions :to => :confirmed, :from => :not_deletable
  end
end

In the event block I define how a user state can become confirmed. Therefore the user.state attribute has to be "waiting", "blocked", "changing_email" or "not_deleteable". In these cases you can call some magic methods which AASM brings in.

user.confirmed?
# => false

Now you can confirm the user.

user.confirm
user.state
# => confirmed

Or

user.confirm! 
# => true
user.state
# => confirmed

Now

user.confirmed?
# => true

If my user has the state "anonymized" then I can't confirm it and will get an error. This way my user instances have well defined states at any time. I can't make a mistake while changing a user state. I do it with the defined methods from AASM only. Only defined states and defined transitions are possible to do.

guitarman
  • 3,290
  • 1
  • 17
  • 27