after_commit hooks are now event-based #112

This commit is contained in:
Thorsten Böttger 2014-03-17 21:58:44 +01:00
parent 21e4ffe419
commit d9472c7a29
4 changed files with 12 additions and 8 deletions

View File

@ -2,12 +2,13 @@
## 4.0.0 (not yet released)
* callbacks don't require `to_state` parameter anymore, but still support it
* **DSL change**: callbacks don't require `to_state` parameter anymore, but still support it
(closing issues
[#11](https://github.com/aasm/aasm/issues/11),
[#58](https://github.com/aasm/aasm/issues/58) and
[#80](https://github.com/aasm/aasm/issues/80)
thanks to [@ejlangev](https://github.com/ejlangev))
* **DSL change**: `after_commit` hooks are now event-based (see [issue #112](https://github.com/aasm/aasm/issues/112))
## 3.9.0 (not yet released)

View File

@ -356,9 +356,9 @@ class Job < ActiveRecord::Base
aasm do
state :sleeping, :initial => true
state :running, :after_commit => :notify_about_running_job
state :running
event :run do
event :run, :after_commit => :notify_about_running_job do
transitions :from => :sleeping, :to => :running
end
end

View File

@ -143,8 +143,8 @@ module AASM
end
if success
new_state = aasm.state_object_for_name(aasm.current_state)
new_state.fire_callbacks(:after_commit, self)
event = self.class.aasm.events[name]
event.fire_callbacks(:after_commit, self)
end
success

View File

@ -1,12 +1,14 @@
require 'active_record'
class Validator < ActiveRecord::Base
include AASM
aasm :column => :status do
state :sleeping, :initial => true
state :running, :after_commit => :change_name!
state :failed, :after_enter => :fail, :after_commit => :change_name!
event :run do
state :running
state :failed, :after_enter => :fail
event :run, :after_commit => :change_name! do
transitions :to => :running, :from => :sleeping
end
event :sleep do
@ -16,6 +18,7 @@ class Validator < ActiveRecord::Base
transitions :to => :failed, :from => [:sleeping, :running]
end
end
validates_presence_of :name
def change_name!