aasm 3.0.22 → 3.0.23

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,9 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 3.0.23
4
+
5
+ * added support for `after_commit` callback (transaction support) (thanks to [@tisba](https://github.com/tisba))
6
+
3
7
  ## 3.0.22
4
8
 
5
9
  * fixed [issue 88](https://github.com/aasm/aasm/issues/88): wrong number of arguments for transaction method
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # AASM - Ruby state machines
2
2
 
3
- [![Build Status](https://secure.travis-ci.org/aasm/aasm.png?branch=master)](http://travis-ci.org/aasm/aasm) [![Code Climate](https://codeclimate.com/github/aasm/aasm.png)](https://codeclimate.com/github/aasm/aasm) [![Coverage Status](https://coveralls.io/repos/aasm/aasm/badge.png?branch=master)](https://coveralls.io/r/aasm/aasm)
3
+ [![Gem Version](https://badge.fury.io/rb/aasm.png)](http://badge.fury.io/rb/aasm) [![Build Status](https://secure.travis-ci.org/aasm/aasm.png?branch=master)](http://travis-ci.org/aasm/aasm) [![Code Climate](https://codeclimate.com/github/aasm/aasm.png)](https://codeclimate.com/github/aasm/aasm) [![Coverage Status](https://coveralls.io/repos/aasm/aasm/badge.png?branch=master)](https://coveralls.io/r/aasm/aasm)
4
4
 
5
5
  This package contains AASM, a library for adding finite state machines to Ruby classes.
6
6
 
@@ -307,6 +307,28 @@ Since version *3.0.13* AASM supports ActiveRecord transactions. So whenever a tr
307
307
  callback or the state update fails, all changes to any database record are rolled back.
308
308
  Mongodb does not support transactions.
309
309
 
310
+ If you want to make sure a depending action happens only after the transaction is committed,
311
+ use the `after_commit` callback, like this:
312
+
313
+ ```ruby
314
+ class Job < ActiveRecord::Base
315
+ include AASM
316
+
317
+ aasm do
318
+ state :sleeping, :initial => true
319
+ state :running
320
+
321
+ event :run, :after_commit => :notify_about_running_job do
322
+ transitions :from => :sleeping, :to => :running
323
+ end
324
+ end
325
+
326
+ def notify_about_running_job
327
+ ...
328
+ end
329
+ end
330
+ ```
331
+
310
332
  ### Column name & migration
311
333
 
312
334
  As a default AASM uses the column `aasm_state` to store the states. You can override
@@ -338,7 +360,7 @@ class AddJobState < ActiveRecord::Migration
338
360
  end
339
361
  ```
340
362
 
341
- ### <a id="inspection">Inspection
363
+ ### Inspection
342
364
 
343
365
  AASM supports a couple of methods to find out which states or events are provided or permissible.
344
366
 
@@ -132,9 +132,16 @@ module AASM
132
132
  end
133
133
 
134
134
  def aasm_fire_event(name, options, *args)
135
- self.class.transaction(:requires_new => true) do
135
+ success = self.class.transaction(:requires_new => true) do
136
136
  super
137
137
  end
138
+
139
+ if success
140
+ new_state = aasm.state_object_for_name(aasm.current_state)
141
+ new_state.fire_callbacks(:after_commit, self)
142
+ end
143
+
144
+ success
138
145
  end
139
146
  end # InstanceMethods
140
147
 
@@ -1,3 +1,3 @@
1
1
  module AASM
2
- VERSION = "3.0.22"
2
+ VERSION = "3.0.23"
3
3
  end
@@ -4,13 +4,26 @@ class Validator < ActiveRecord::Base
4
4
  include AASM
5
5
  aasm :column => :status do
6
6
  state :sleeping, :initial => true
7
- state :running
7
+ state :running, :after_commit => :change_name!
8
+ state :failed, :after_enter => :fail, :after_commit => :change_name!
8
9
  event :run do
9
10
  transitions :to => :running, :from => :sleeping
10
11
  end
11
12
  event :sleep do
12
13
  transitions :to => :sleeping, :from => :running
13
14
  end
15
+ event :fail do
16
+ transitions :to => :failed, :from => [:sleeping, :running]
17
+ end
14
18
  end
15
19
  validates_presence_of :name
20
+
21
+ def change_name!
22
+ self.name = "name changed"
23
+ save!
24
+ end
25
+
26
+ def fail
27
+ raise StandardError.new('failed on purpose')
28
+ end
16
29
  end
@@ -202,5 +202,22 @@ describe 'transitions with persistence' do
202
202
  transactor.should be_running
203
203
  worker.reload.status.should == 'sleeping'
204
204
  end
205
+
206
+ describe "after_commit callback" do
207
+ it "should fire :after_commit if transaction was successful" do
208
+ validator = Validator.create(:name => 'name')
209
+ validator.should be_sleeping
210
+ validator.run!
211
+ validator.should be_running
212
+ validator.name.should_not == "name"
213
+ end
214
+
215
+ it "should not fire :after_commit if transaction failed" do
216
+ validator = Validator.create(:name => 'name')
217
+ lambda { validator.fail! }.should raise_error(StandardError, 'failed on purpose')
218
+ validator.name.should == "name"
219
+ end
220
+
221
+ end
205
222
  end
206
223
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aasm
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.22
4
+ version: 3.0.23
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -12,22 +12,27 @@ authors:
12
12
  autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
- date: 2013-08-31 00:00:00.000000000 Z
15
+ date: 2013-10-24 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: activerecord
19
- requirement: &70096173850320 !ruby/object:Gem::Requirement
19
+ requirement: !ruby/object:Gem::Requirement
20
20
  none: false
21
21
  requirements:
22
- - - =
22
+ - - '='
23
23
  - !ruby/object:Gem::Version
24
24
  version: 3.2.12
25
25
  type: :development
26
26
  prerelease: false
27
- version_requirements: *70096173850320
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - '='
31
+ - !ruby/object:Gem::Version
32
+ version: 3.2.12
28
33
  - !ruby/object:Gem::Dependency
29
34
  name: mongoid
30
- requirement: &70096173849660 !ruby/object:Gem::Requirement
35
+ requirement: !ruby/object:Gem::Requirement
31
36
  none: false
32
37
  requirements:
33
38
  - - ! '>='
@@ -35,10 +40,15 @@ dependencies:
35
40
  version: '0'
36
41
  type: :development
37
42
  prerelease: false
38
- version_requirements: *70096173849660
43
+ version_requirements: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
39
49
  - !ruby/object:Gem::Dependency
40
50
  name: rake
41
- requirement: &70096173849200 !ruby/object:Gem::Requirement
51
+ requirement: !ruby/object:Gem::Requirement
42
52
  none: false
43
53
  requirements:
44
54
  - - ! '>='
@@ -46,10 +56,15 @@ dependencies:
46
56
  version: '0'
47
57
  type: :development
48
58
  prerelease: false
49
- version_requirements: *70096173849200
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
50
65
  - !ruby/object:Gem::Dependency
51
66
  name: sdoc
52
- requirement: &70096173848780 !ruby/object:Gem::Requirement
67
+ requirement: !ruby/object:Gem::Requirement
53
68
  none: false
54
69
  requirements:
55
70
  - - ! '>='
@@ -57,10 +72,15 @@ dependencies:
57
72
  version: '0'
58
73
  type: :development
59
74
  prerelease: false
60
- version_requirements: *70096173848780
75
+ version_requirements: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
61
81
  - !ruby/object:Gem::Dependency
62
82
  name: rspec
63
- requirement: &70096173848280 !ruby/object:Gem::Requirement
83
+ requirement: !ruby/object:Gem::Requirement
64
84
  none: false
65
85
  requirements:
66
86
  - - ~>
@@ -68,10 +88,15 @@ dependencies:
68
88
  version: '2.0'
69
89
  type: :development
70
90
  prerelease: false
71
- version_requirements: *70096173848280
91
+ version_requirements: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: '2.0'
72
97
  - !ruby/object:Gem::Dependency
73
98
  name: rr
74
- requirement: &70096173847860 !ruby/object:Gem::Requirement
99
+ requirement: !ruby/object:Gem::Requirement
75
100
  none: false
76
101
  requirements:
77
102
  - - ! '>='
@@ -79,10 +104,15 @@ dependencies:
79
104
  version: '0'
80
105
  type: :development
81
106
  prerelease: false
82
- version_requirements: *70096173847860
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ! '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
83
113
  - !ruby/object:Gem::Dependency
84
114
  name: sqlite3
85
- requirement: &70096173847400 !ruby/object:Gem::Requirement
115
+ requirement: !ruby/object:Gem::Requirement
86
116
  none: false
87
117
  requirements:
88
118
  - - ! '>='
@@ -90,10 +120,15 @@ dependencies:
90
120
  version: '0'
91
121
  type: :development
92
122
  prerelease: false
93
- version_requirements: *70096173847400
123
+ version_requirements: !ruby/object:Gem::Requirement
124
+ none: false
125
+ requirements:
126
+ - - ! '>='
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
94
129
  - !ruby/object:Gem::Dependency
95
130
  name: minitest
96
- requirement: &70096173382180 !ruby/object:Gem::Requirement
131
+ requirement: !ruby/object:Gem::Requirement
97
132
  none: false
98
133
  requirements:
99
134
  - - ! '>='
@@ -101,10 +136,15 @@ dependencies:
101
136
  version: '0'
102
137
  type: :development
103
138
  prerelease: false
104
- version_requirements: *70096173382180
139
+ version_requirements: !ruby/object:Gem::Requirement
140
+ none: false
141
+ requirements:
142
+ - - ! '>='
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
105
145
  - !ruby/object:Gem::Dependency
106
146
  name: ruby-debug-completion
107
- requirement: &70096173381240 !ruby/object:Gem::Requirement
147
+ requirement: !ruby/object:Gem::Requirement
108
148
  none: false
109
149
  requirements:
110
150
  - - ! '>='
@@ -112,10 +152,15 @@ dependencies:
112
152
  version: '0'
113
153
  type: :development
114
154
  prerelease: false
115
- version_requirements: *70096173381240
155
+ version_requirements: !ruby/object:Gem::Requirement
156
+ none: false
157
+ requirements:
158
+ - - ! '>='
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
116
161
  - !ruby/object:Gem::Dependency
117
162
  name: coveralls
118
- requirement: &70096173380220 !ruby/object:Gem::Requirement
163
+ requirement: !ruby/object:Gem::Requirement
119
164
  none: false
120
165
  requirements:
121
166
  - - ! '>='
@@ -123,7 +168,12 @@ dependencies:
123
168
  version: '0'
124
169
  type: :development
125
170
  prerelease: false
126
- version_requirements: *70096173380220
171
+ version_requirements: !ruby/object:Gem::Requirement
172
+ none: false
173
+ requirements:
174
+ - - ! '>='
175
+ - !ruby/object:Gem::Version
176
+ version: '0'
127
177
  description: AASM is a continuation of the acts as state machine rails plugin, built
128
178
  for plain Ruby objects.
129
179
  email: scott@elitists.net, ttilley@gmail.com, aasm@mt7.de
@@ -223,7 +273,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
223
273
  version: '0'
224
274
  requirements: []
225
275
  rubyforge_project:
226
- rubygems_version: 1.8.11
276
+ rubygems_version: 1.8.24
227
277
  signing_key:
228
278
  specification_version: 3
229
279
  summary: State machine mixin for Ruby objects