ruby on rails - Where can we use Gem AASM? -


since new ror, want know gem aasm. actually, have ruby code built aasm gem.

it's hard me understand why using gem here.

i want know basic ideas of why/when/where can use gem.

thanks in advance.

the intention allow defined values attribute , allow predefined transitions 1 state only. example, state can transferred in state b or state c, state b can not transferred in state c. in addition aasm offers callback methods controll transitions or before or after transition.

i used aasm within user model ensure user states can changed on predefined ways. see example:

# state machine ---------------------------------------------------------- initial_state = :pending states = {   initial_state    => 'new user accounts initial state',   :waiting         => 'new user account waiting confirmation',   :confirmed       => 'user confirmed registration',   :changing_email  => 'user changed email address',   :blocked         => 'user can not log in',   :anonymized      => 'personal user data deleted',   :not_deletable   => 'can not delete user, because has content' }  include aasm attr_protected :state aasm({:column => :state})   states.keys.each |key|     state(key, (key == initial_state ? {:initial => true} : {}))   end    # creates instance methods confirm, confirm! , confirmed? later usage   event :confirm     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 event block define how user state can become confirmed. therefore user.state attribute has "waiting", "blocked", "changing_email" or "not_deleteable". in these cases can call magic methods aasm brings in.

user.confirmed? # => false 

now can confirm user.

user.confirm user.state # => confirmed 

or

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

now

user.confirmed? # => true 

if user has state "anonymized" can't confirm , error. way user instances have defined states @ time. can't make mistake while changing user state. defined methods aasm only. defined states , defined transitions possible do.


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -