aasm 3.0.17 → 3.0.18

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 3.0.18
6
+
7
+ * fixing issue #66
8
+
5
9
  ## 3.0.17
6
10
 
7
11
  * supporting instance level inspection for states (including permissible state, see issue #54)
data/lib/aasm.rb CHANGED
@@ -1,17 +1,18 @@
1
1
  require 'ostruct'
2
2
 
3
- # TODO shorten this [thorsten, 2011-12-20]
4
- require File.join(File.dirname(__FILE__), 'aasm', 'version')
5
- require File.join(File.dirname(__FILE__), 'aasm', 'errors')
6
- require File.join(File.dirname(__FILE__), 'aasm', 'base')
7
- require File.join(File.dirname(__FILE__), 'aasm', 'instance_base')
8
- require File.join(File.dirname(__FILE__), 'aasm', 'transition')
9
- require File.join(File.dirname(__FILE__), 'aasm', 'event')
10
- require File.join(File.dirname(__FILE__), 'aasm', 'state')
11
- require File.join(File.dirname(__FILE__), 'aasm', 'localizer')
12
- require File.join(File.dirname(__FILE__), 'aasm', 'state_machine')
13
- require File.join(File.dirname(__FILE__), 'aasm', 'persistence')
14
- require File.join(File.dirname(__FILE__), 'aasm', 'aasm')
3
+ %w(
4
+ version
5
+ errors
6
+ base
7
+ instance_base
8
+ transition
9
+ event
10
+ state
11
+ localizer
12
+ state_machine
13
+ persistence
14
+ aasm
15
+ ).each { |file| require File.join(File.dirname(__FILE__), 'aasm', file) }
15
16
 
16
17
  # load the deprecated methods and modules
17
18
  Dir[File.join(File.dirname(__FILE__), 'aasm', 'deprecated', '*.rb')].sort.each { |f| require File.expand_path(f) }
data/lib/aasm/aasm.rb CHANGED
@@ -83,7 +83,10 @@ module AASM
83
83
 
84
84
  # may be overwritten by persistence mixins
85
85
  def aasm_read_state
86
- aasm.enter_initial_state
86
+ # all the following lines behave like @current_state ||= aasm.enter_initial_state
87
+ current = aasm.instance_variable_get("@current_state")
88
+ return current if current
89
+ aasm.instance_variable_set("@current_state", aasm.enter_initial_state)
87
90
  end
88
91
 
89
92
  # may be overwritten by persistence mixins
@@ -134,61 +137,67 @@ module AASM
134
137
 
135
138
  private
136
139
 
137
- def aasm_fire_event(name, options, *args)
138
- persist = options[:persist]
139
-
140
- event = self.class.aasm_events[name]
140
+ def aasm_fire_event(event_name, options, *args)
141
+ event = self.class.aasm_events[event_name]
141
142
  begin
142
143
  old_state = aasm.state_object_for_name(aasm.current_state)
143
-
144
-
145
144
  old_state.fire_callbacks(:exit, self)
146
145
 
147
146
  # new event before callback
148
147
  event.fire_callbacks(:before, self)
149
148
 
150
149
  if new_state_name = event.fire(self, *args)
151
- new_state = aasm.state_object_for_name(new_state_name)
150
+ fired(event, old_state, new_state_name, options)
151
+ else
152
+ failed(event_name, old_state)
153
+ end
154
+ rescue StandardError => e
155
+ event.fire_callbacks(:error, self, e) || raise(e)
156
+ end
157
+ end
158
+
159
+ def fired(event, old_state, new_state_name, options)
160
+ persist = options[:persist]
152
161
 
153
- # new before_ callbacks
154
- old_state.fire_callbacks(:before_exit, self)
155
- new_state.fire_callbacks(:before_enter, self)
162
+ new_state = aasm.state_object_for_name(new_state_name)
156
163
 
157
- new_state.fire_callbacks(:enter, self)
164
+ # new before_ callbacks
165
+ old_state.fire_callbacks(:before_exit, self)
166
+ new_state.fire_callbacks(:before_enter, self)
158
167
 
159
- persist_successful = true
160
- if persist
161
- persist_successful = aasm.set_current_state_with_persistence(new_state_name)
162
- event.fire_callbacks(:success, self) if persist_successful
163
- else
164
- aasm.current_state = new_state_name
165
- end
168
+ new_state.fire_callbacks(:enter, self)
166
169
 
167
- if persist_successful
168
- old_state.fire_callbacks(:after_exit, self)
169
- new_state.fire_callbacks(:after_enter, self)
170
- event.fire_callbacks(:after, self)
170
+ persist_successful = true
171
+ if persist
172
+ persist_successful = aasm.set_current_state_with_persistence(new_state_name)
173
+ event.fire_callbacks(:success, self) if persist_successful
174
+ else
175
+ aasm.current_state = new_state_name
176
+ end
171
177
 
172
- self.aasm_event_fired(name, old_state.name, aasm.current_state) if self.respond_to?(:aasm_event_fired)
173
- else
174
- self.aasm_event_failed(name, old_state.name) if self.respond_to?(:aasm_event_failed)
175
- end
178
+ if persist_successful
179
+ old_state.fire_callbacks(:after_exit, self)
180
+ new_state.fire_callbacks(:after_enter, self)
181
+ event.fire_callbacks(:after, self)
176
182
 
177
- persist_successful
183
+ self.aasm_event_fired(event.name, old_state.name, aasm.current_state) if self.respond_to?(:aasm_event_fired)
184
+ else
185
+ self.aasm_event_failed(event.name, old_state.name) if self.respond_to?(:aasm_event_failed)
186
+ end
178
187
 
179
- else
180
- if self.respond_to?(:aasm_event_failed)
181
- self.aasm_event_failed(name, old_state.name)
182
- end
183
-
184
- if AASM::StateMachine[self.class].config.whiny_transitions
185
- raise AASM::InvalidTransition, "Event '#{event.name}' cannot transition from '#{aasm.current_state}'"
186
- else
187
- false
188
- end
189
- end
190
- rescue StandardError => e
191
- event.fire_callbacks(:error, self, e) || raise(e)
188
+ persist_successful
189
+ end
190
+
191
+ def failed(event_name, old_state)
192
+ if self.respond_to?(:aasm_event_failed)
193
+ self.aasm_event_failed(event_name, old_state.name)
194
+ end
195
+
196
+ if AASM::StateMachine[self.class].config.whiny_transitions
197
+ raise AASM::InvalidTransition, "Event '#{event_name}' cannot transition from '#{aasm.current_state}'"
198
+ else
199
+ false
192
200
  end
193
201
  end
202
+
194
203
  end
data/lib/aasm/base.rb CHANGED
@@ -30,7 +30,7 @@ module AASM
30
30
  @state_machine.initial_state = name if options[:initial] || !@state_machine.initial_state
31
31
 
32
32
  @clazz.send(:define_method, "#{name.to_s}?") do
33
- aasm_current_state == name
33
+ aasm.current_state == name
34
34
  end
35
35
 
36
36
  unless @clazz.const_defined?("STATE_#{name.to_s.upcase}")
@@ -6,7 +6,7 @@ module AASM
6
6
  end
7
7
 
8
8
  def current_state
9
- @current_state ||= @instance.aasm_read_state
9
+ @instance.aasm_read_state
10
10
  end
11
11
 
12
12
  def current_state=(state)
@@ -33,10 +33,11 @@ module AASM
33
33
  #
34
34
  # This allows for nil aasm states - be sure to add validation to your model
35
35
  def aasm_read_state
36
+ state = send(self.class.aasm_column)
36
37
  if new_record?
37
- send(self.class.aasm_column).blank? ? aasm.determine_state_name(self.class.aasm_initial_state) : send(self.class.aasm_column).to_sym
38
+ state.blank? ? aasm.determine_state_name(self.class.aasm_initial_state) : state.to_sym
38
39
  else
39
- send(self.class.aasm_column).nil? ? nil : send(self.class.aasm_column).to_sym
40
+ state.nil? ? nil : state.to_sym
40
41
  end
41
42
  end
42
43
 
data/lib/aasm/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module AASM
2
- VERSION = "3.0.17"
2
+ VERSION = "3.0.18"
3
3
  end
@@ -0,0 +1,21 @@
1
+ require 'active_record'
2
+
3
+ class Father < ActiveRecord::Base
4
+ include AASM
5
+
6
+ aasm do
7
+ state :missing_details, :initial => true
8
+ state :pending_details_confirmation
9
+
10
+ event :add_details do
11
+ transitions :from => :missing_details, :to => :pending_details_confirmation
12
+ end
13
+ end
14
+
15
+ def update_state
16
+ if may_add_details?
17
+ add_details!
18
+ end
19
+ end
20
+
21
+ end
@@ -0,0 +1,3 @@
1
+ class Son < Father
2
+ include AASM
3
+ end
data/spec/schema.rb CHANGED
@@ -27,4 +27,9 @@ ActiveRecord::Schema.define(:version => 0) do
27
27
  t.string "status"
28
28
  end
29
29
 
30
+ create_table "fathers", :force => true do |t|
31
+ t.string "aasm_state"
32
+ t.string "type"
33
+ end
34
+
30
35
  end
@@ -122,6 +122,13 @@ end
122
122
 
123
123
  describe 'transitions with persistence' do
124
124
 
125
+ it "should work for valid models" do
126
+ valid_object = Validator.create(:name => 'name')
127
+ valid_object.should be_sleeping
128
+ valid_object.status = :running
129
+ valid_object.should be_running
130
+ end
131
+
125
132
  it 'should not store states for invalid models' do
126
133
  validator = Validator.create(:name => 'name')
127
134
  validator.should be_valid
@@ -1,6 +1,8 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe 'subclassing' do
4
+ let(:son) {Son.new}
5
+
4
6
  it 'should have the parent states' do
5
7
  Foo.aasm_states.each do |state|
6
8
  FooTwo.aasm_states.should include(state)
@@ -15,5 +17,15 @@ describe 'subclassing' do
15
17
  it "should have the same events as its parent" do
16
18
  Baz.aasm_events.should == Bar.aasm_events
17
19
  end
20
+
21
+ it 'should know how to respond to `may_add_details?`' do
22
+ son.may_add_details?.should be_true
23
+ end
24
+
25
+ it 'should not break if I call Son#update_state' do
26
+ son.update_state
27
+ son.aasm_current_state.should == :pending_details_confirmation
28
+ end
29
+
18
30
  end
19
31
 
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.17
4
+ version: 3.0.18
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -12,11 +12,11 @@ authors:
12
12
  autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
- date: 2013-04-28 00:00:00.000000000 Z
15
+ date: 2013-05-16 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: activerecord
19
- requirement: !ruby/object:Gem::Requirement
19
+ requirement: &70315275195540 !ruby/object:Gem::Requirement
20
20
  none: false
21
21
  requirements:
22
22
  - - ! '>='
@@ -24,15 +24,10 @@ dependencies:
24
24
  version: '0'
25
25
  type: :development
26
26
  prerelease: false
27
- version_requirements: !ruby/object:Gem::Requirement
28
- none: false
29
- requirements:
30
- - - ! '>='
31
- - !ruby/object:Gem::Version
32
- version: '0'
27
+ version_requirements: *70315275195540
33
28
  - !ruby/object:Gem::Dependency
34
29
  name: mongoid
35
- requirement: !ruby/object:Gem::Requirement
30
+ requirement: &70315275193680 !ruby/object:Gem::Requirement
36
31
  none: false
37
32
  requirements:
38
33
  - - ! '>='
@@ -40,15 +35,10 @@ dependencies:
40
35
  version: '0'
41
36
  type: :development
42
37
  prerelease: false
43
- version_requirements: !ruby/object:Gem::Requirement
44
- none: false
45
- requirements:
46
- - - ! '>='
47
- - !ruby/object:Gem::Version
48
- version: '0'
38
+ version_requirements: *70315275193680
49
39
  - !ruby/object:Gem::Dependency
50
40
  name: rake
51
- requirement: !ruby/object:Gem::Requirement
41
+ requirement: &70315275191760 !ruby/object:Gem::Requirement
52
42
  none: false
53
43
  requirements:
54
44
  - - ! '>='
@@ -56,15 +46,10 @@ dependencies:
56
46
  version: '0'
57
47
  type: :development
58
48
  prerelease: false
59
- version_requirements: !ruby/object:Gem::Requirement
60
- none: false
61
- requirements:
62
- - - ! '>='
63
- - !ruby/object:Gem::Version
64
- version: '0'
49
+ version_requirements: *70315275191760
65
50
  - !ruby/object:Gem::Dependency
66
51
  name: sdoc
67
- requirement: !ruby/object:Gem::Requirement
52
+ requirement: &70315275191120 !ruby/object:Gem::Requirement
68
53
  none: false
69
54
  requirements:
70
55
  - - ! '>='
@@ -72,15 +57,10 @@ dependencies:
72
57
  version: '0'
73
58
  type: :development
74
59
  prerelease: false
75
- version_requirements: !ruby/object:Gem::Requirement
76
- none: false
77
- requirements:
78
- - - ! '>='
79
- - !ruby/object:Gem::Version
80
- version: '0'
60
+ version_requirements: *70315275191120
81
61
  - !ruby/object:Gem::Dependency
82
62
  name: rspec
83
- requirement: !ruby/object:Gem::Requirement
63
+ requirement: &70315267341460 !ruby/object:Gem::Requirement
84
64
  none: false
85
65
  requirements:
86
66
  - - ~>
@@ -88,15 +68,10 @@ dependencies:
88
68
  version: '2.0'
89
69
  type: :development
90
70
  prerelease: false
91
- version_requirements: !ruby/object:Gem::Requirement
92
- none: false
93
- requirements:
94
- - - ~>
95
- - !ruby/object:Gem::Version
96
- version: '2.0'
71
+ version_requirements: *70315267341460
97
72
  - !ruby/object:Gem::Dependency
98
73
  name: rr
99
- requirement: !ruby/object:Gem::Requirement
74
+ requirement: &70315267340520 !ruby/object:Gem::Requirement
100
75
  none: false
101
76
  requirements:
102
77
  - - ! '>='
@@ -104,15 +79,10 @@ dependencies:
104
79
  version: '0'
105
80
  type: :development
106
81
  prerelease: false
107
- version_requirements: !ruby/object:Gem::Requirement
108
- none: false
109
- requirements:
110
- - - ! '>='
111
- - !ruby/object:Gem::Version
112
- version: '0'
82
+ version_requirements: *70315267340520
113
83
  - !ruby/object:Gem::Dependency
114
84
  name: shoulda
115
- requirement: !ruby/object:Gem::Requirement
85
+ requirement: &70315267339020 !ruby/object:Gem::Requirement
116
86
  none: false
117
87
  requirements:
118
88
  - - ! '>='
@@ -120,15 +90,10 @@ dependencies:
120
90
  version: '0'
121
91
  type: :development
122
92
  prerelease: false
123
- version_requirements: !ruby/object:Gem::Requirement
124
- none: false
125
- requirements:
126
- - - ! '>='
127
- - !ruby/object:Gem::Version
128
- version: '0'
93
+ version_requirements: *70315267339020
129
94
  - !ruby/object:Gem::Dependency
130
95
  name: sqlite3
131
- requirement: !ruby/object:Gem::Requirement
96
+ requirement: &70315267338380 !ruby/object:Gem::Requirement
132
97
  none: false
133
98
  requirements:
134
99
  - - ! '>='
@@ -136,15 +101,10 @@ dependencies:
136
101
  version: '0'
137
102
  type: :development
138
103
  prerelease: false
139
- version_requirements: !ruby/object:Gem::Requirement
140
- none: false
141
- requirements:
142
- - - ! '>='
143
- - !ruby/object:Gem::Version
144
- version: '0'
104
+ version_requirements: *70315267338380
145
105
  - !ruby/object:Gem::Dependency
146
106
  name: minitest
147
- requirement: !ruby/object:Gem::Requirement
107
+ requirement: &70315267336860 !ruby/object:Gem::Requirement
148
108
  none: false
149
109
  requirements:
150
110
  - - ! '>='
@@ -152,15 +112,10 @@ dependencies:
152
112
  version: '0'
153
113
  type: :development
154
114
  prerelease: false
155
- version_requirements: !ruby/object:Gem::Requirement
156
- none: false
157
- requirements:
158
- - - ! '>='
159
- - !ruby/object:Gem::Version
160
- version: '0'
115
+ version_requirements: *70315267336860
161
116
  - !ruby/object:Gem::Dependency
162
117
  name: ruby-debug-completion
163
- requirement: !ruby/object:Gem::Requirement
118
+ requirement: &70315267334180 !ruby/object:Gem::Requirement
164
119
  none: false
165
120
  requirements:
166
121
  - - ! '>='
@@ -168,15 +123,10 @@ dependencies:
168
123
  version: '0'
169
124
  type: :development
170
125
  prerelease: false
171
- version_requirements: !ruby/object:Gem::Requirement
172
- none: false
173
- requirements:
174
- - - ! '>='
175
- - !ruby/object:Gem::Version
176
- version: '0'
126
+ version_requirements: *70315267334180
177
127
  - !ruby/object:Gem::Dependency
178
128
  name: coveralls
179
- requirement: !ruby/object:Gem::Requirement
129
+ requirement: &70315267318500 !ruby/object:Gem::Requirement
180
130
  none: false
181
131
  requirements:
182
132
  - - ! '>='
@@ -184,12 +134,7 @@ dependencies:
184
134
  version: '0'
185
135
  type: :development
186
136
  prerelease: false
187
- version_requirements: !ruby/object:Gem::Requirement
188
- none: false
189
- requirements:
190
- - - ! '>='
191
- - !ruby/object:Gem::Version
192
- version: '0'
137
+ version_requirements: *70315267318500
193
138
  description: AASM is a continuation of the acts as state machine rails plugin, built
194
139
  for plain Ruby objects.
195
140
  email: scott@elitists.net, ttilley@gmail.com, aasm@mt7.de
@@ -234,6 +179,7 @@ files:
234
179
  - spec/models/callback_new_dsl.rb
235
180
  - spec/models/callback_old_dsl.rb
236
181
  - spec/models/conversation.rb
182
+ - spec/models/father.rb
237
183
  - spec/models/foo.rb
238
184
  - spec/models/invalid_persistor.rb
239
185
  - spec/models/mongoid/simple_mongoid.rb
@@ -243,6 +189,7 @@ files:
243
189
  - spec/models/persistence.rb
244
190
  - spec/models/process_with_new_dsl.rb
245
191
  - spec/models/silencer.rb
192
+ - spec/models/son.rb
246
193
  - spec/models/sub_classing.rb
247
194
  - spec/models/this_name_better_not_be_in_use.rb
248
195
  - spec/models/transactor.rb
@@ -286,7 +233,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
286
233
  version: '0'
287
234
  requirements: []
288
235
  rubyforge_project:
289
- rubygems_version: 1.8.24
236
+ rubygems_version: 1.8.11
290
237
  signing_key:
291
238
  specification_version: 3
292
239
  summary: State machine mixin for Ruby objects
@@ -301,6 +248,7 @@ test_files:
301
248
  - spec/models/callback_new_dsl.rb
302
249
  - spec/models/callback_old_dsl.rb
303
250
  - spec/models/conversation.rb
251
+ - spec/models/father.rb
304
252
  - spec/models/foo.rb
305
253
  - spec/models/invalid_persistor.rb
306
254
  - spec/models/mongoid/simple_mongoid.rb
@@ -310,6 +258,7 @@ test_files:
310
258
  - spec/models/persistence.rb
311
259
  - spec/models/process_with_new_dsl.rb
312
260
  - spec/models/silencer.rb
261
+ - spec/models/son.rb
313
262
  - spec/models/sub_classing.rb
314
263
  - spec/models/this_name_better_not_be_in_use.rb
315
264
  - spec/models/transactor.rb