aasm 4.10.1 → 4.11.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +4 -0
- data/CHANGELOG.md +6 -0
- data/README.md +20 -2
- data/lib/aasm/base.rb +9 -1
- data/lib/aasm/configuration.rb +7 -1
- data/lib/aasm/persistence/active_record_persistence.rb +10 -1
- data/lib/aasm/version.rb +1 -1
- data/spec/database.rb +1 -15
- data/spec/models/silent_persistor.rb +31 -0
- data/spec/models/validator.rb +2 -2
- data/spec/unit/override_warning_spec.rb +40 -23
- data/spec/unit/persistence/active_record_persistence_multiple_spec.rb +24 -0
- data/spec/unit/persistence/active_record_persistence_spec.rb +24 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7c0f9cf1b1303a1269e84ada192ce2d56b2ad29b
|
4
|
+
data.tar.gz: 8ad1ec953454bb5fbf113d9a47494fae24238744
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 29373b642050f83f1d44a5241857b9e75b0aa1dd4b224b161f057acdde5595bf79e57554229b610ce3288e058816dbb096c8b344e6f0c056706273dcbbf55b80
|
7
|
+
data.tar.gz: 93dff92101055f58ef493c1e1574c716683315a452add2b43522ef6a4e7b25c6d38caf9e2791a2ed8162c9b541b465d275ec09e699c6f3ad794b537ba8fb3e74
|
data/.travis.yml
CHANGED
@@ -43,3 +43,7 @@ matrix:
|
|
43
43
|
gemfile: gemfiles/rails_3.2_stable.gemfile
|
44
44
|
- rvm: jruby-1.7
|
45
45
|
gemfile: gemfiles/rails_5.0.gemfile
|
46
|
+
|
47
|
+
notifications:
|
48
|
+
slack:
|
49
|
+
secure: MsPY8sUW5TVblcWc5NGkkWJlHxCMnhNa3IFPSVWthlk0fz+CLfOrLjQ+brQup/AJ7BRtyBKQ8udpqLj/R7CJKAiVVDAlUBcHqXk/WDusssGzJkDMrLVDefq++YCg5TdbcIaHt8WBVg6pD8H4kQJMJxmtcziQtiW1Qt0pwfuNJ+k=
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
# CHANGELOG
|
2
2
|
|
3
|
+
## 4.11.0
|
4
|
+
|
5
|
+
* support `logger` configuration (see [issue #370](https://github.com/aasm/aasm/pull/370) for details, thanks to [@HoyaBoya](https://github.com/HoyaBoya))
|
6
|
+
* support configuration to let bang transitions fail if object is invalid (see [issue #366](https://github.com/aasm/aasm/pull/366) and [issue #262](https://github.com/aasm/aasm/issues/262) for details, thanks to [@Wildebeest](https://github.com/Wildebeest))
|
7
|
+
|
8
|
+
|
3
9
|
## 4.10.1
|
4
10
|
|
5
11
|
* fix: suppress warnings when using ActiveRecord enums feature (see [issue #346](https://github.com/aasm/aasm/pull/346) for details, thanks to [@110y](https://github.com/110y), and [issue #353](https://github.com/aasm/aasm/pull/353) for details, thanks to [@nathanstitt](https://github.com/nathanstitt))
|
data/README.md
CHANGED
@@ -522,7 +522,7 @@ Saving includes running all validations on the `Job` class, and returns `true` i
|
|
522
522
|
successful or `false` if errors occur. Exceptions are not raised.
|
523
523
|
|
524
524
|
If you want make sure the state gets saved without running validations (and
|
525
|
-
thereby maybe persisting
|
525
|
+
thereby maybe persisting an invalid object state), simply tell AASM to skip the
|
526
526
|
validations. Be aware that when skipping validations, only the state column will
|
527
527
|
be updated in the database (just like ActiveRecord `update_column` is working).
|
528
528
|
|
@@ -869,6 +869,7 @@ end
|
|
869
869
|
AASM supports query methods for states and events
|
870
870
|
|
871
871
|
Given the following `Job` class:
|
872
|
+
|
872
873
|
```ruby
|
873
874
|
class Job
|
874
875
|
include AASM
|
@@ -889,7 +890,7 @@ class Job
|
|
889
890
|
transitions :from => [:running, :cleaning], :to => :sleeping
|
890
891
|
end
|
891
892
|
end
|
892
|
-
|
893
|
+
|
893
894
|
def cleaning_needed?
|
894
895
|
false
|
895
896
|
end
|
@@ -937,6 +938,23 @@ Job.aasm.states_for_select
|
|
937
938
|
```
|
938
939
|
|
939
940
|
|
941
|
+
### Warning output
|
942
|
+
|
943
|
+
Warnings are by default printed to `STDERR`. If you want to log those warnings to another output,
|
944
|
+
use
|
945
|
+
|
946
|
+
```ruby
|
947
|
+
class Job
|
948
|
+
include AASM
|
949
|
+
|
950
|
+
aasm :logger => Rails.logger do
|
951
|
+
...
|
952
|
+
end
|
953
|
+
end
|
954
|
+
```
|
955
|
+
|
956
|
+
Be aware though, that this is not yet released. It will be part of _AASM_ version `4.11.0`.
|
957
|
+
|
940
958
|
### RubyMotion support
|
941
959
|
|
942
960
|
Now supports [CodeDataQuery](https://github.com/infinitered/cdq.git) !
|
data/lib/aasm/base.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'logger'
|
2
|
+
|
1
3
|
module AASM
|
2
4
|
class Base
|
3
5
|
|
@@ -21,6 +23,9 @@ module AASM
|
|
21
23
|
# don't store any new state if the model is invalid (in ActiveRecord)
|
22
24
|
configure :skip_validation_on_save, false
|
23
25
|
|
26
|
+
# raise if the model is invalid (in ActiveRecord)
|
27
|
+
configure :whiny_persistence, false
|
28
|
+
|
24
29
|
# use requires_new for nested transactions (in ActiveRecord)
|
25
30
|
configure :requires_new_transaction, true
|
26
31
|
|
@@ -40,6 +45,9 @@ module AASM
|
|
40
45
|
# Set to true to namespace reader methods and constants
|
41
46
|
configure :namespace, false
|
42
47
|
|
48
|
+
# Configure a logger, with default being a Logger to STDERR
|
49
|
+
configure :logger, Logger.new(STDERR)
|
50
|
+
|
43
51
|
# make sure to raise an error if no_direct_assignment is enabled
|
44
52
|
# and attribute is directly assigned though
|
45
53
|
aasm_name = @name
|
@@ -200,7 +208,7 @@ module AASM
|
|
200
208
|
klass.defined_enums.values.any?{ |methods|
|
201
209
|
methods.keys{| enum | enum + '?' == method_name }
|
202
210
|
})
|
203
|
-
|
211
|
+
@state_machine.config.logger.warn "#{klass.name}: overriding method '#{method_name}'!"
|
204
212
|
end
|
205
213
|
|
206
214
|
klass.send(:define_method, method_name, method_definition)
|
data/lib/aasm/configuration.rb
CHANGED
@@ -9,7 +9,10 @@ module AASM
|
|
9
9
|
# for all persistence layers: create named scopes for each state
|
10
10
|
attr_accessor :create_scopes
|
11
11
|
|
12
|
-
# for ActiveRecord:
|
12
|
+
# for ActiveRecord: when the model is invalid, true -> raise, false -> return false
|
13
|
+
attr_accessor :whiny_persistence
|
14
|
+
|
15
|
+
# for ActiveRecord: store the new state even if the model is invalid and return true
|
13
16
|
attr_accessor :skip_validation_on_save
|
14
17
|
|
15
18
|
# for ActiveRecord: use requires_new for nested transactions?
|
@@ -28,5 +31,8 @@ module AASM
|
|
28
31
|
|
29
32
|
# namespace reader methods and constants
|
30
33
|
attr_accessor :namespace
|
34
|
+
|
35
|
+
# Configure a logger, with default being a Logger to STDERR
|
36
|
+
attr_accessor :logger
|
31
37
|
end
|
32
38
|
end
|
@@ -76,7 +76,12 @@ module AASM
|
|
76
76
|
self.save
|
77
77
|
end
|
78
78
|
|
79
|
-
success
|
79
|
+
unless success
|
80
|
+
aasm_rollback(name, old_value)
|
81
|
+
raise ActiveRecord::RecordInvalid.new(self) if aasm_whiny_persistence(name)
|
82
|
+
end
|
83
|
+
|
84
|
+
success
|
80
85
|
end
|
81
86
|
|
82
87
|
# Writes <tt>state</tt> to the state column, but does not persist it to the database
|
@@ -130,6 +135,10 @@ module AASM
|
|
130
135
|
AASM::StateMachineStore.fetch(self.class, true).machine(state_machine_name).config.skip_validation_on_save
|
131
136
|
end
|
132
137
|
|
138
|
+
def aasm_whiny_persistence(state_machine_name)
|
139
|
+
AASM::StateMachineStore.fetch(self.class, true).machine(state_machine_name).config.whiny_persistence
|
140
|
+
end
|
141
|
+
|
133
142
|
def aasm_write_attribute(state, name=:default)
|
134
143
|
write_attribute(self.class.aasm(name).attribute_name, aasm_raw_attribute_value(state, name))
|
135
144
|
end
|
data/lib/aasm/version.rb
CHANGED
data/spec/database.rb
CHANGED
@@ -17,7 +17,7 @@ ActiveRecord::Migration.suppress_messages do
|
|
17
17
|
t.string "right"
|
18
18
|
end
|
19
19
|
|
20
|
-
%w(validators multiple_validators).each do |table_name|
|
20
|
+
%w(validators multiple_validators workers invalid_persistors multiple_invalid_persistors silent_persistors multiple_silent_persistors).each do |table_name|
|
21
21
|
ActiveRecord::Migration.create_table table_name, :force => true do |t|
|
22
22
|
t.string "name"
|
23
23
|
t.string "status"
|
@@ -32,20 +32,6 @@ ActiveRecord::Migration.suppress_messages do
|
|
32
32
|
end
|
33
33
|
end
|
34
34
|
|
35
|
-
ActiveRecord::Migration.create_table "workers", :force => true do |t|
|
36
|
-
t.string "name"
|
37
|
-
t.string "status"
|
38
|
-
end
|
39
|
-
|
40
|
-
ActiveRecord::Migration.create_table "invalid_persistors", :force => true do |t|
|
41
|
-
t.string "name"
|
42
|
-
t.string "status"
|
43
|
-
end
|
44
|
-
ActiveRecord::Migration.create_table "multiple_invalid_persistors", :force => true do |t|
|
45
|
-
t.string "name"
|
46
|
-
t.string "status"
|
47
|
-
end
|
48
|
-
|
49
35
|
ActiveRecord::Migration.create_table "fathers", :force => true do |t|
|
50
36
|
t.string "aasm_state"
|
51
37
|
t.string "type"
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
class SilentPersistor < ActiveRecord::Base
|
4
|
+
include AASM
|
5
|
+
aasm :column => :status, :whiny_persistence => false do
|
6
|
+
state :sleeping, :initial => true
|
7
|
+
state :running
|
8
|
+
event :run do
|
9
|
+
transitions :to => :running, :from => :sleeping
|
10
|
+
end
|
11
|
+
event :sleep do
|
12
|
+
transitions :to => :sleeping, :from => :running
|
13
|
+
end
|
14
|
+
end
|
15
|
+
validates_presence_of :name
|
16
|
+
end
|
17
|
+
|
18
|
+
class MultipleSilentPersistor < ActiveRecord::Base
|
19
|
+
include AASM
|
20
|
+
aasm :left, :column => :status, :whiny_persistence => false do
|
21
|
+
state :sleeping, :initial => true
|
22
|
+
state :running
|
23
|
+
event :run do
|
24
|
+
transitions :to => :running, :from => :sleeping
|
25
|
+
end
|
26
|
+
event :sleep do
|
27
|
+
transitions :to => :sleeping, :from => :running
|
28
|
+
end
|
29
|
+
end
|
30
|
+
validates_presence_of :name
|
31
|
+
end
|
data/spec/models/validator.rb
CHANGED
@@ -10,7 +10,7 @@ class Validator < ActiveRecord::Base
|
|
10
10
|
|
11
11
|
include AASM
|
12
12
|
|
13
|
-
aasm :column => :status do
|
13
|
+
aasm :column => :status, :whiny_persistence => true do
|
14
14
|
before_all_transactions :before_all_transactions
|
15
15
|
after_all_transactions :after_all_transactions
|
16
16
|
|
@@ -78,7 +78,7 @@ end
|
|
78
78
|
class MultipleValidator < ActiveRecord::Base
|
79
79
|
|
80
80
|
include AASM
|
81
|
-
aasm :left, :column => :status do
|
81
|
+
aasm :left, :column => :status, :whiny_persistence => true do
|
82
82
|
state :sleeping, :initial => true
|
83
83
|
state :running
|
84
84
|
state :failed, :after_enter => :fail
|
@@ -28,42 +28,59 @@ describe 'warns when overrides a method' do
|
|
28
28
|
end
|
29
29
|
|
30
30
|
describe 'state' do
|
31
|
-
|
32
|
-
|
31
|
+
let(:base_klass) do
|
32
|
+
Class.new do
|
33
|
+
def valid?; end
|
34
|
+
end
|
33
35
|
end
|
34
|
-
|
35
|
-
|
36
|
-
|
36
|
+
|
37
|
+
subject { base_klass.send :include, Clumsy }
|
38
|
+
|
39
|
+
it 'should log to warn' do
|
40
|
+
expect_any_instance_of(Logger).to receive(:warn).with(": overriding method 'valid?'!")
|
41
|
+
subject
|
37
42
|
end
|
38
43
|
end
|
39
44
|
|
40
45
|
describe 'enum' do
|
41
|
-
|
42
|
-
|
46
|
+
let(:enum_base_klass) do
|
47
|
+
Class.new do
|
48
|
+
def valid?; end
|
49
|
+
end
|
43
50
|
end
|
44
|
-
|
45
|
-
|
46
|
-
|
51
|
+
|
52
|
+
subject { enum_base_klass.send :include, WithEnumBase }
|
53
|
+
|
54
|
+
it 'should not log to warn' do
|
55
|
+
expect_any_instance_of(Logger).to receive(:warn).never
|
56
|
+
subject
|
47
57
|
end
|
48
58
|
end
|
49
59
|
|
50
60
|
describe 'event' do
|
51
61
|
context 'may?' do
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
62
|
+
let(:base_klass) do
|
63
|
+
Class.new do
|
64
|
+
def may_save?; end
|
65
|
+
def save!; end
|
66
|
+
def save; end
|
67
|
+
end
|
56
68
|
end
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
69
|
+
|
70
|
+
subject { base_klass.send :include, Clumsy }
|
71
|
+
|
72
|
+
it 'should log to warn' do
|
73
|
+
expect_any_instance_of(Logger).to receive(:warn).exactly(3).times do |logger, message|
|
74
|
+
expect(
|
75
|
+
[
|
76
|
+
": overriding method 'may_save?'!",
|
77
|
+
": overriding method 'save!'!",
|
78
|
+
": overriding method 'save'!"
|
79
|
+
]
|
80
|
+
).to include(message)
|
81
|
+
end
|
82
|
+
subject
|
65
83
|
end
|
66
84
|
end
|
67
85
|
end
|
68
|
-
|
69
86
|
end
|
@@ -384,6 +384,30 @@ describe 'transitions with persistence' do
|
|
384
384
|
expect(validator).to be_valid
|
385
385
|
expect(validator).to be_sleeping
|
386
386
|
|
387
|
+
validator.name = nil
|
388
|
+
expect(validator).not_to be_valid
|
389
|
+
expect { validator.run! }.to raise_error(ActiveRecord::RecordInvalid)
|
390
|
+
expect(validator).to be_sleeping
|
391
|
+
|
392
|
+
validator.reload
|
393
|
+
expect(validator).not_to be_running
|
394
|
+
expect(validator).to be_sleeping
|
395
|
+
|
396
|
+
validator.name = 'another name'
|
397
|
+
expect(validator).to be_valid
|
398
|
+
expect(validator.run!).to be_truthy
|
399
|
+
expect(validator).to be_running
|
400
|
+
|
401
|
+
validator.reload
|
402
|
+
expect(validator).to be_running
|
403
|
+
expect(validator).not_to be_sleeping
|
404
|
+
end
|
405
|
+
|
406
|
+
it 'should not store states for invalid models silently if configured' do
|
407
|
+
validator = MultipleSilentPersistor.create(:name => 'name')
|
408
|
+
expect(validator).to be_valid
|
409
|
+
expect(validator).to be_sleeping
|
410
|
+
|
387
411
|
validator.name = nil
|
388
412
|
expect(validator).not_to be_valid
|
389
413
|
expect(validator.run!).to be_falsey
|
@@ -385,6 +385,30 @@ describe 'transitions with persistence' do
|
|
385
385
|
expect(validator).to be_valid
|
386
386
|
expect(validator).to be_sleeping
|
387
387
|
|
388
|
+
validator.name = nil
|
389
|
+
expect(validator).not_to be_valid
|
390
|
+
expect { validator.run! }.to raise_error(ActiveRecord::RecordInvalid)
|
391
|
+
expect(validator).to be_sleeping
|
392
|
+
|
393
|
+
validator.reload
|
394
|
+
expect(validator).not_to be_running
|
395
|
+
expect(validator).to be_sleeping
|
396
|
+
|
397
|
+
validator.name = 'another name'
|
398
|
+
expect(validator).to be_valid
|
399
|
+
expect(validator.run!).to be_truthy
|
400
|
+
expect(validator).to be_running
|
401
|
+
|
402
|
+
validator.reload
|
403
|
+
expect(validator).to be_running
|
404
|
+
expect(validator).not_to be_sleeping
|
405
|
+
end
|
406
|
+
|
407
|
+
it 'should not store states for invalid models silently if configured' do
|
408
|
+
validator = SilentPersistor.create(:name => 'name')
|
409
|
+
expect(validator).to be_valid
|
410
|
+
expect(validator).to be_sleeping
|
411
|
+
|
388
412
|
validator.name = nil
|
389
413
|
expect(validator).not_to be_valid
|
390
414
|
expect(validator.run!).to be_falsey
|
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: 4.
|
4
|
+
version: 4.11.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Scott Barron
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2016-
|
13
|
+
date: 2016-06-19 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rake
|
@@ -220,6 +220,7 @@ files:
|
|
220
220
|
- spec/models/sequel/sequel_multiple.rb
|
221
221
|
- spec/models/sequel/sequel_simple.rb
|
222
222
|
- spec/models/silencer.rb
|
223
|
+
- spec/models/silent_persistor.rb
|
223
224
|
- spec/models/simple_custom_example.rb
|
224
225
|
- spec/models/simple_example.rb
|
225
226
|
- spec/models/simple_multiple_example.rb
|
@@ -377,6 +378,7 @@ test_files:
|
|
377
378
|
- spec/models/sequel/sequel_multiple.rb
|
378
379
|
- spec/models/sequel/sequel_simple.rb
|
379
380
|
- spec/models/silencer.rb
|
381
|
+
- spec/models/silent_persistor.rb
|
380
382
|
- spec/models/simple_custom_example.rb
|
381
383
|
- spec/models/simple_example.rb
|
382
384
|
- spec/models/simple_multiple_example.rb
|