From 67f12f26d106151527d8b0d705675d9ee93c537b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorsten=20B=C3=B6ttger?= Date: Wed, 13 May 2015 18:52:13 +1200 Subject: [PATCH] support block notation for :after_commit event callbacks --- CHANGELOG.md | 1 + lib/aasm/core/event.rb | 2 +- spec/models/validator.rb | 8 ++++++++ spec/unit/persistence/active_record_persistence_spec.rb | 7 ++++++- 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 01f80403..72ede332 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ## 4.1.1 (not yet released) + * support block notation for `:after_commit` event callbacks (see [issue #224](https://github.com/aasm/aasm/issues/224) for details) * event arguments are now passed to state callbacks as well (not only to event callbacks) (see [issue #219](https://github.com/aasm/aasm/issues/219), thanks to [@tobithiel](https://github.com/tobithiel)) * `AASM::InvalidTransition` now references the current object (with the state machine) and the _AASM_ event name (see [issue #217](https://github.com/aasm/aasm/issues/217), thanks to [@awsmsrc](https://github.com/awsmsrc)) * bugfix: do not update unloaded state for [Sequel](http://sequel.jeremyevans.net/) (see [issue #218](https://github.com/aasm/aasm/issues/218), thanks to [@godfat](https://github.com/godfat)) diff --git a/lib/aasm/core/event.rb b/lib/aasm/core/event.rb index 40e77818..aca4a1d4 100644 --- a/lib/aasm/core/event.rb +++ b/lib/aasm/core/event.rb @@ -12,7 +12,7 @@ def initialize(name, options = {}, &block) # from aasm4 @options = options # QUESTION: .dup ? - add_options_from_dsl(@options, [:after, :before, :error, :success], &block) if block + add_options_from_dsl(@options, [:after, :before, :error, :success, :after_commit], &block) if block end # a neutered version of fire - it doesn't actually fire the event, it just diff --git a/spec/models/validator.rb b/spec/models/validator.rb index f0333a29..2da81ef8 100644 --- a/spec/models/validator.rb +++ b/spec/models/validator.rb @@ -12,6 +12,9 @@ class Validator < ActiveRecord::Base transitions :to => :running, :from => :sleeping end event :sleep do + after_commit do + change_name_on_sleep + end transitions :to => :sleeping, :from => :running end event :fail do @@ -26,6 +29,11 @@ def change_name! save! end + def change_name_on_sleep + self.name = "sleeper" + save! + end + def fail raise StandardError.new('failed on purpose') end diff --git a/spec/unit/persistence/active_record_persistence_spec.rb b/spec/unit/persistence/active_record_persistence_spec.rb index a696d108..b7ca0fda 100644 --- a/spec/unit/persistence/active_record_persistence_spec.rb +++ b/spec/unit/persistence/active_record_persistence_spec.rb @@ -431,9 +431,14 @@ it "should fire :after_commit if transaction was successful" do validator = Validator.create(:name => 'name') expect(validator).to be_sleeping + validator.run! expect(validator).to be_running - expect(validator.name).not_to eq("name") + expect(validator.name).to eq("name changed") + + validator.sleep! + expect(validator).to be_sleeping + expect(validator.name).to eq("sleeper") end it "should not fire :after_commit if transaction failed" do