1
0
Fork 0
mirror of https://github.com/aasm/aasm synced 2023-03-27 23:22:41 -04:00

Add example of Class option for after transition callback to README

This commit is contained in:
Ronnie Miller 2016-02-07 08:51:52 -08:00
parent cd8a2ef12d
commit dbcc7c4a2a

View file

@ -106,6 +106,7 @@ class Job
end end
transitions :from => :sleeping, :to => :running, :after => Proc.new {|*args| set_process(*args) } transitions :from => :sleeping, :to => :running, :after => Proc.new {|*args| set_process(*args) }
transitions :from => :running, :to => :finished, :after => LogRunTime
end end
event :sleep do event :sleep do
@ -136,12 +137,34 @@ class Job
end end
end end
class LogRunTime
def call
log "Job was running for X seconds"
end
end
``` ```
In this case `do_something` is called before actually entering the state `sleeping`, In this case `do_something` is called before actually entering the state `sleeping`,
while `notify_somebody` is called after the transition `run` (from `sleeping` to `running`) while `notify_somebody` is called after the transition `run` (from `sleeping` to `running`)
is finished. is finished.
AASM will also initialize `LogRunTime` and run the `call` method for you after the transition from `runnung` to `finished` in the example above. You can pass arguments to the class by defining an initialize method on it, like this:
```
class LogRunTime
# optional args parameter can be omitted, but if you define initialize
# you must accept the model instance as the first parameter to it.
def initialize(job, args = {})
@job = job
end
def call
log "Job was running for #{@job.run_time} seconds"
end
end
```
Here you can see a list of all possible callbacks, together with their order of calling: Here you can see a list of all possible callbacks, together with their order of calling:
```ruby ```ruby