Skip to content

Commit

Permalink
support block notation for :after_commit event callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
alto committed May 13, 2015
1 parent a8e10dc commit 67f12f2
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion lib/aasm/core/event.rb
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions spec/models/validator.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down
7 changes: 6 additions & 1 deletion spec/unit/persistence/active_record_persistence_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit 67f12f2

Please sign in to comment.