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:
parent
cd8a2ef12d
commit
dbcc7c4a2a
1 changed files with 23 additions and 0 deletions
23
README.md
23
README.md
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue