From dbcc7c4a2abcf6d3f3b61cf1d02c056e9fe60f7d Mon Sep 17 00:00:00 2001 From: Ronnie Miller Date: Sun, 7 Feb 2016 08:51:52 -0800 Subject: [PATCH] Add example of Class option for after transition callback to README --- README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/README.md b/README.md index 1367cfa..0630544 100644 --- a/README.md +++ b/README.md @@ -106,6 +106,7 @@ class Job end transitions :from => :sleeping, :to => :running, :after => Proc.new {|*args| set_process(*args) } + transitions :from => :running, :to => :finished, :after => LogRunTime end event :sleep do @@ -136,12 +137,34 @@ class Job 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`, while `notify_somebody` is called after the transition `run` (from `sleeping` to `running`) 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: ```ruby