aasm 4.2.0 → 4.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -1
- data/Gemfile +2 -2
- data/PLANNED_CHANGES.md +24 -4
- data/README.md +75 -5
- data/lib/aasm/aasm.rb +50 -36
- data/lib/aasm/base.rb +36 -18
- data/lib/aasm/core/event.rb +6 -5
- data/lib/aasm/core/state.rb +3 -2
- data/lib/aasm/core/transition.rb +5 -4
- data/lib/aasm/errors.rb +7 -4
- data/lib/aasm/instance_base.rb +14 -13
- data/lib/aasm/localizer.rb +1 -1
- data/lib/aasm/persistence/active_record_persistence.rb +41 -66
- data/lib/aasm/persistence/base.rb +7 -7
- data/lib/aasm/persistence/mongo_mapper_persistence.rb +34 -51
- data/lib/aasm/persistence/mongoid_persistence.rb +15 -36
- data/lib/aasm/persistence/plain_persistence.rb +8 -7
- data/lib/aasm/persistence/sequel_persistence.rb +12 -10
- data/lib/aasm/state_machine.rb +11 -6
- data/lib/aasm/version.rb +1 -1
- data/spec/database.rb +27 -1
- data/spec/models/active_record/basic_active_record_two_state_machines_example.rb +25 -0
- data/spec/models/active_record/complex_active_record_example.rb +33 -0
- data/spec/models/active_record/derivate_new_dsl.rb +4 -0
- data/spec/models/active_record/false_state.rb +18 -0
- data/spec/models/active_record/gate.rb +20 -0
- data/spec/models/active_record/no_direct_assignment.rb +11 -0
- data/spec/models/active_record/no_scope.rb +11 -0
- data/spec/models/active_record/provided_and_persisted_state.rb +3 -3
- data/spec/models/active_record/simple_new_dsl.rb +9 -0
- data/spec/models/active_record/thief.rb +15 -0
- data/spec/models/active_record/with_enum.rb +20 -0
- data/spec/models/active_record/with_false_enum.rb +16 -0
- data/spec/models/active_record/with_true_enum.rb +20 -0
- data/spec/models/basic_two_state_machines_example.rb +25 -0
- data/spec/models/callbacks/basic_multiple.rb +75 -0
- data/spec/models/callbacks/guard_within_block_multiple.rb +66 -0
- data/spec/models/callbacks/multiple_transitions_transition_guard_multiple.rb +65 -0
- data/spec/models/callbacks/private_method_multiple.rb +44 -0
- data/spec/models/callbacks/with_args_multiple.rb +61 -0
- data/spec/models/callbacks/{with_state_args.rb → with_state_arg.rb} +0 -0
- data/spec/models/callbacks/with_state_arg_multiple.rb +26 -0
- data/spec/models/complex_example.rb +134 -0
- data/spec/models/conversation.rb +47 -1
- data/spec/models/foo.rb +57 -0
- data/spec/models/foo_callback_multiple.rb +45 -0
- data/spec/models/guardian_multiple.rb +48 -0
- data/spec/models/initial_state_proc.rb +16 -0
- data/spec/models/invalid_persistor.rb +15 -0
- data/spec/models/mongo_mapper/complex_mongo_mapper_example.rb +37 -0
- data/spec/models/mongo_mapper/no_scope_mongo_mapper.rb +11 -0
- data/spec/models/mongo_mapper/simple_mongo_mapper.rb +12 -0
- data/spec/models/mongo_mapper/simple_new_dsl_mongo_mapper.rb +13 -0
- data/spec/models/mongoid/complex_mongoid_example.rb +37 -0
- data/spec/models/mongoid/no_scope_mongoid.rb +11 -0
- data/spec/models/mongoid/simple_mongoid.rb +12 -0
- data/spec/models/mongoid/simple_new_dsl_mongoid.rb +13 -0
- data/spec/models/no_initial_state.rb +13 -0
- data/spec/models/parametrised_event.rb +1 -1
- data/spec/models/parametrised_event_multiple.rb +29 -0
- data/spec/models/provided_state.rb +3 -3
- data/spec/models/sequel/complex_sequel_example.rb +45 -0
- data/spec/models/sequel/sequel_multiple.rb +25 -0
- data/spec/models/sequel/sequel_simple.rb +25 -0
- data/spec/models/simple_multiple_example.rb +30 -0
- data/spec/models/sub_class.rb +4 -0
- data/spec/models/sub_class_with_more_states.rb +11 -0
- data/spec/models/super_class.rb +28 -0
- data/spec/models/transactor.rb +27 -0
- data/spec/models/valid_state_name.rb +12 -0
- data/spec/models/validator.rb +39 -0
- data/spec/unit/basic_two_state_machines_example_spec.rb +10 -0
- data/spec/unit/callback_multiple_spec.rb +295 -0
- data/spec/unit/callbacks_spec.rb +1 -1
- data/spec/unit/complex_multiple_example_spec.rb +99 -0
- data/spec/unit/edge_cases_spec.rb +16 -0
- data/spec/unit/event_multiple_spec.rb +73 -0
- data/spec/unit/event_spec.rb +11 -6
- data/spec/unit/guard_multiple_spec.rb +60 -0
- data/spec/unit/initial_state_multiple_spec.rb +15 -0
- data/spec/unit/inspection_multiple_spec.rb +201 -0
- data/spec/unit/persistence/active_record_persistence_multiple_spec.rb +560 -0
- data/spec/unit/persistence/active_record_persistence_spec.rb +17 -12
- data/spec/unit/persistence/mongo_mapper_persistence_multiple_spec.rb +146 -0
- data/spec/unit/persistence/{mongo_mapper_persistance_spec.rb → mongo_mapper_persistence_spec.rb} +7 -49
- data/spec/unit/persistence/mongoid_persistence_multiple_spec.rb +127 -0
- data/spec/unit/persistence/mongoid_persistence_spec.rb +79 -0
- data/spec/unit/persistence/sequel_persistence_multiple_spec.rb +153 -0
- data/spec/unit/persistence/sequel_persistence_spec.rb +7 -24
- data/spec/unit/reloading_spec.rb +1 -1
- data/spec/unit/simple_multiple_example_spec.rb +63 -0
- data/spec/unit/state_spec.rb +3 -1
- data/spec/unit/subclassing_multiple_spec.rb +39 -0
- data/spec/unit/transition_spec.rb +31 -22
- metadata +73 -9
- data/spec/unit/persistence/mongoid_persistance_spec.rb +0 -146
@@ -1,19 +1,15 @@
|
|
1
1
|
require 'active_record'
|
2
2
|
require 'spec_helper'
|
3
|
-
Dir[File.dirname(__FILE__) + "/../../models/active_record/*.rb"].sort.each
|
3
|
+
Dir[File.dirname(__FILE__) + "/../../models/active_record/*.rb"].sort.each do |f|
|
4
|
+
require File.expand_path(f)
|
5
|
+
end
|
6
|
+
|
4
7
|
load_schema
|
5
8
|
|
6
9
|
# if you want to see the statements while running the spec enable the following line
|
7
10
|
# require 'logger'
|
8
11
|
# ActiveRecord::Base.logger = Logger.new(STDERR)
|
9
12
|
|
10
|
-
shared_examples_for "aasm model" do
|
11
|
-
it "should include persistence mixins" do
|
12
|
-
expect(klass.included_modules).to be_include(AASM::Persistence::ActiveRecordPersistence)
|
13
|
-
expect(klass.included_modules).to be_include(AASM::Persistence::ActiveRecordPersistence::InstanceMethods)
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
13
|
describe "instance methods" do
|
18
14
|
let(:gate) {Gate.new}
|
19
15
|
|
@@ -160,7 +156,7 @@ describe "instance methods" do
|
|
160
156
|
|
161
157
|
gate.aasm_write_state state_sym
|
162
158
|
|
163
|
-
expect(gate).to have_received(:aasm_write_attribute).with(state_sym)
|
159
|
+
expect(gate).to have_received(:aasm_write_attribute).with(state_sym, :default)
|
164
160
|
expect(gate).to_not have_received :write_attribute
|
165
161
|
end
|
166
162
|
end
|
@@ -170,7 +166,7 @@ describe "instance methods" do
|
|
170
166
|
it "delegates state update to the helper method" do
|
171
167
|
gate.aasm_write_state_without_persistence state_sym
|
172
168
|
|
173
|
-
expect(gate).to have_received(:aasm_write_attribute).with(state_sym)
|
169
|
+
expect(gate).to have_received(:aasm_write_attribute).with(state_sym, :default)
|
174
170
|
expect(gate).to_not have_received :write_attribute
|
175
171
|
end
|
176
172
|
end
|
@@ -210,7 +206,7 @@ describe "instance methods" do
|
|
210
206
|
end
|
211
207
|
|
212
208
|
it "generates attribute value using a helper method" do
|
213
|
-
expect(gate).to have_received(:aasm_raw_attribute_value).with(sym)
|
209
|
+
expect(gate).to have_received(:aasm_raw_attribute_value).with(sym, :default)
|
214
210
|
end
|
215
211
|
|
216
212
|
it "writes attribute to the model" do
|
@@ -431,7 +427,7 @@ describe 'transitions with persistence' do
|
|
431
427
|
|
432
428
|
it "should only rollback changes in the main transaction not the nested one" do
|
433
429
|
# change configuration to not require new transaction
|
434
|
-
AASM::StateMachine[Transactor].config.requires_new_transaction = false
|
430
|
+
AASM::StateMachine[Transactor][:default].config.requires_new_transaction = false
|
435
431
|
|
436
432
|
expect(transactor).to be_sleeping
|
437
433
|
expect(worker.status).to eq('sleeping')
|
@@ -516,3 +512,12 @@ describe "invalid states with persistence" do
|
|
516
512
|
end
|
517
513
|
|
518
514
|
end
|
515
|
+
|
516
|
+
describe 'basic example with two state machines' do
|
517
|
+
let(:example) { BasicActiveRecordTwoStateMachinesExample.new }
|
518
|
+
|
519
|
+
it 'should initialise properly' do
|
520
|
+
expect(example.aasm(:search).current_state).to eql :initialised
|
521
|
+
expect(example.aasm(:sync).current_state).to eql :unsynced
|
522
|
+
end
|
523
|
+
end
|
@@ -0,0 +1,146 @@
|
|
1
|
+
describe 'mongo_mapper' do
|
2
|
+
begin
|
3
|
+
require 'mongo_mapper'
|
4
|
+
require 'logger'
|
5
|
+
require 'spec_helper'
|
6
|
+
|
7
|
+
Dir[File.dirname(__FILE__) + "/../../models/mongo_mapper/*.rb"].sort.each do |f|
|
8
|
+
require File.expand_path(f)
|
9
|
+
end
|
10
|
+
|
11
|
+
before(:all) do
|
12
|
+
config = {
|
13
|
+
'test' => {
|
14
|
+
'database' => "mongo_mapper_#{Process.pid}"
|
15
|
+
}
|
16
|
+
}
|
17
|
+
|
18
|
+
MongoMapper.setup(config, 'test') #, :logger => Logger.new(STDERR))
|
19
|
+
end
|
20
|
+
|
21
|
+
after do
|
22
|
+
# Clear Out all non-system Mongo collections between tests
|
23
|
+
MongoMapper.database.collections.each do |collection|
|
24
|
+
collection.drop unless collection.capped? || (collection.name =~ /\Asystem/)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "named scopes with the old DSL" do
|
29
|
+
|
30
|
+
context "Does not already respond_to? the scope name" do
|
31
|
+
it "should add a scope" do
|
32
|
+
expect(SimpleMongoMapperMultiple).to respond_to(:unknown_scope)
|
33
|
+
expect(SimpleMongoMapperMultiple.unknown_scope.class).to eq(MongoMapper::Plugins::Querying::DecoratedPluckyQuery)
|
34
|
+
#expect(SimpleMongoMapperMultiple.unknown_scope.is_a?(ActiveRecord::Relation)).to be_truthy
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
context "Already respond_to? the scope name" do
|
39
|
+
it "should not add a scope" do
|
40
|
+
expect(SimpleMongoMapperMultiple).to respond_to(:next)
|
41
|
+
expect(SimpleMongoMapperMultiple.new.class).to eq(SimpleMongoMapperMultiple)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
describe "named scopes with the new DSL" do
|
48
|
+
|
49
|
+
context "Does not already respond_to? the scope name" do
|
50
|
+
it "should add a scope" do
|
51
|
+
expect(SimpleNewDslMongoMapperMultiple).to respond_to(:unknown_scope)
|
52
|
+
expect(SimpleNewDslMongoMapperMultiple.unknown_scope.class).to eq(MongoMapper::Plugins::Querying::DecoratedPluckyQuery)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
context "Already respond_to? the scope name" do
|
57
|
+
it "should not add a scope" do
|
58
|
+
expect(SimpleNewDslMongoMapperMultiple).to respond_to(:next)
|
59
|
+
expect(SimpleNewDslMongoMapperMultiple.new.class).to eq(SimpleNewDslMongoMapperMultiple)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
it "does not create scopes if requested" do
|
64
|
+
expect(NoScopeMongoMapperMultiple).not_to respond_to(:ignored_scope)
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
describe "instance methods" do
|
70
|
+
|
71
|
+
let(:simple) {SimpleNewDslMongoMapperMultiple.new}
|
72
|
+
|
73
|
+
it "should call aasm_ensure_initial_state on validation before create" do
|
74
|
+
expect(SimpleNewDslMongoMapperMultiple.aasm(:left).initial_state).to eq(:unknown_scope)
|
75
|
+
expect(SimpleNewDslMongoMapperMultiple.aasm(:left).attribute_name).to eq(:status)
|
76
|
+
expect(simple.status).to eq(nil)
|
77
|
+
simple.valid?
|
78
|
+
expect(simple.status).to eq('unknown_scope')
|
79
|
+
end
|
80
|
+
|
81
|
+
it "should call aasm_ensure_initial_state before create, even if skipping validations" do
|
82
|
+
expect(simple.status).to eq(nil)
|
83
|
+
simple.save(:validate => false)
|
84
|
+
expect(simple.status).to eq('unknown_scope')
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
describe "complex example" do
|
89
|
+
it "works" do
|
90
|
+
record = ComplexMongoMapperExample.new
|
91
|
+
expect(record.aasm(:left).current_state).to eql :one
|
92
|
+
expect(record.left).to be_nil
|
93
|
+
expect(record.aasm(:right).current_state).to eql :alpha
|
94
|
+
expect(record.right).to be_nil
|
95
|
+
|
96
|
+
record.save!
|
97
|
+
expect_aasm_states record, :one, :alpha
|
98
|
+
record.reload
|
99
|
+
expect_aasm_states record, :one, :alpha
|
100
|
+
|
101
|
+
record.increment!
|
102
|
+
expect_aasm_states record, :two, :alpha
|
103
|
+
record.reload
|
104
|
+
expect_aasm_states record, :two, :alpha
|
105
|
+
|
106
|
+
record.level_up!
|
107
|
+
expect_aasm_states record, :two, :beta
|
108
|
+
record.reload
|
109
|
+
expect_aasm_states record, :two, :beta
|
110
|
+
|
111
|
+
record.increment!
|
112
|
+
expect { record.increment! }.to raise_error(AASM::InvalidTransition)
|
113
|
+
expect_aasm_states record, :three, :beta
|
114
|
+
record.reload
|
115
|
+
expect_aasm_states record, :three, :beta
|
116
|
+
|
117
|
+
record.level_up!
|
118
|
+
expect_aasm_states record, :three, :gamma
|
119
|
+
record.reload
|
120
|
+
expect_aasm_states record, :three, :gamma
|
121
|
+
|
122
|
+
record.level_down # without saving
|
123
|
+
expect_aasm_states record, :three, :beta
|
124
|
+
record.reload
|
125
|
+
expect_aasm_states record, :three, :gamma
|
126
|
+
|
127
|
+
record.level_down # without saving
|
128
|
+
expect_aasm_states record, :three, :beta
|
129
|
+
record.reset!
|
130
|
+
expect_aasm_states record, :one, :beta
|
131
|
+
end
|
132
|
+
|
133
|
+
def expect_aasm_states(record, left_state, right_state)
|
134
|
+
expect(record.aasm(:left).current_state).to eql left_state.to_sym
|
135
|
+
expect(record.left).to eql left_state.to_s
|
136
|
+
expect(record.aasm(:right).current_state).to eql right_state.to_sym
|
137
|
+
expect(record.right).to eql right_state.to_s
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
rescue LoadError
|
142
|
+
puts "--------------------------------------------------------------------------"
|
143
|
+
puts "Not running MongoMapper specs because mongo_mapper gem is not installed!!!"
|
144
|
+
puts "--------------------------------------------------------------------------"
|
145
|
+
end
|
146
|
+
end
|
data/spec/unit/persistence/{mongo_mapper_persistance_spec.rb → mongo_mapper_persistence_spec.rb}
RENAMED
@@ -4,9 +4,11 @@ describe 'mongo_mapper' do
|
|
4
4
|
require 'logger'
|
5
5
|
require 'spec_helper'
|
6
6
|
|
7
|
-
|
8
|
-
|
7
|
+
Dir[File.dirname(__FILE__) + "/../../models/mongo_mapper/*.rb"].sort.each do |f|
|
8
|
+
require File.expand_path(f)
|
9
|
+
end
|
9
10
|
|
11
|
+
before(:all) do
|
10
12
|
config = {
|
11
13
|
'test' => {
|
12
14
|
'database' => "mongo_mapper_#{Process.pid}"
|
@@ -64,52 +66,6 @@ describe 'mongo_mapper' do
|
|
64
66
|
|
65
67
|
end
|
66
68
|
|
67
|
-
describe "#find_in_state" do
|
68
|
-
|
69
|
-
let!(:model) { SimpleNewDslMongoMapper.create!(:status => :unknown_scope) }
|
70
|
-
let!(:model_id) { model._id }
|
71
|
-
|
72
|
-
it "should respond to method" do
|
73
|
-
expect(SimpleNewDslMongoMapper).to respond_to(:find_in_state)
|
74
|
-
end
|
75
|
-
|
76
|
-
it "should find the model when given the correct scope and model id" do
|
77
|
-
expect(SimpleNewDslMongoMapper.find_in_state(model_id, 'unknown_scope').class).to eq(SimpleNewDslMongoMapper)
|
78
|
-
expect(SimpleNewDslMongoMapper.find_in_state(model_id, 'unknown_scope')).to eq(model)
|
79
|
-
end
|
80
|
-
|
81
|
-
it "should raise DocumentNotFound error when given incorrect scope" do
|
82
|
-
expect {SimpleNewDslMongoMapper.find_in_state(model_id, 'next')}.to raise_error MongoMapper::DocumentNotFound
|
83
|
-
end
|
84
|
-
|
85
|
-
it "should raise DocumentNotFound error when given incorrect model id" do
|
86
|
-
expect {SimpleNewDslMongoMapper.find_in_state('bad_id', 'unknown_scope')}.to raise_error MongoMapper::DocumentNotFound
|
87
|
-
end
|
88
|
-
|
89
|
-
end
|
90
|
-
|
91
|
-
describe "#count_in_state" do
|
92
|
-
|
93
|
-
before do
|
94
|
-
3.times { SimpleNewDslMongoMapper.create!(:status => :unknown_scope) }
|
95
|
-
end
|
96
|
-
|
97
|
-
it "should respond to method" do
|
98
|
-
expect(SimpleNewDslMongoMapper).to respond_to(:count_in_state)
|
99
|
-
end
|
100
|
-
|
101
|
-
it "should return n for a scope with n records persisted" do
|
102
|
-
expect(SimpleNewDslMongoMapper.count_in_state('unknown_scope').class).to eq(Fixnum)
|
103
|
-
expect(SimpleNewDslMongoMapper.count_in_state('unknown_scope')).to eq(3)
|
104
|
-
end
|
105
|
-
|
106
|
-
it "should return zero for a scope without records persisted" do
|
107
|
-
expect(SimpleNewDslMongoMapper.count_in_state('next').class).to eq(Fixnum)
|
108
|
-
expect(SimpleNewDslMongoMapper.count_in_state('next')).to eq(0)
|
109
|
-
end
|
110
|
-
|
111
|
-
end
|
112
|
-
|
113
69
|
describe "instance methods" do
|
114
70
|
|
115
71
|
let(:simple) {SimpleNewDslMongoMapper.new}
|
@@ -130,6 +86,8 @@ describe 'mongo_mapper' do
|
|
130
86
|
end
|
131
87
|
|
132
88
|
rescue LoadError
|
133
|
-
puts "
|
89
|
+
puts "--------------------------------------------------------------------------"
|
90
|
+
puts "Not running MongoMapper multiple-specs because mongo_mapper gem is not installed!!!"
|
91
|
+
puts "--------------------------------------------------------------------------"
|
134
92
|
end
|
135
93
|
end
|
@@ -0,0 +1,127 @@
|
|
1
|
+
describe 'mongoid' do
|
2
|
+
begin
|
3
|
+
require 'mongoid'
|
4
|
+
require 'logger'
|
5
|
+
require 'spec_helper'
|
6
|
+
|
7
|
+
Dir[File.dirname(__FILE__) + "/../../models/mongoid/*.rb"].sort.each do |f|
|
8
|
+
require File.expand_path(f)
|
9
|
+
end
|
10
|
+
|
11
|
+
before(:all) do
|
12
|
+
# if you want to see the statements while running the spec enable the following line
|
13
|
+
# Mongoid.logger = Logger.new(STDERR)
|
14
|
+
|
15
|
+
Mongoid.configure do |config|
|
16
|
+
config.connect_to "mongoid_#{Process.pid}"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
after do
|
21
|
+
Mongoid.purge!
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "named scopes with the old DSL" do
|
25
|
+
|
26
|
+
context "Does not already respond_to? the scope name" do
|
27
|
+
it "should add a scope" do
|
28
|
+
expect(SimpleMongoidMultiple).to respond_to(:unknown_scope)
|
29
|
+
expect(SimpleMongoidMultiple.unknown_scope.class).to eq(Mongoid::Criteria)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context "Already respond_to? the scope name" do
|
34
|
+
it "should not add a scope" do
|
35
|
+
expect(SimpleMongoidMultiple).to respond_to(:new)
|
36
|
+
expect(SimpleMongoidMultiple.new.class).to eq(SimpleMongoidMultiple)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "named scopes with the new DSL" do
|
43
|
+
context "Does not already respond_to? the scope name" do
|
44
|
+
it "should add a scope" do
|
45
|
+
expect(SimpleNewDslMongoidMultiple).to respond_to(:unknown_scope)
|
46
|
+
expect(SimpleNewDslMongoidMultiple.unknown_scope.class).to eq(Mongoid::Criteria)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
context "Already respond_to? the scope name" do
|
51
|
+
it "should not add a scope" do
|
52
|
+
expect(SimpleNewDslMongoidMultiple).to respond_to(:new)
|
53
|
+
expect(SimpleNewDslMongoidMultiple.new.class).to eq(SimpleNewDslMongoidMultiple)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
it "does not create scopes if requested" do
|
58
|
+
expect(NoScopeMongoidMultiple).not_to respond_to(:ignored_scope)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe "instance methods" do
|
63
|
+
let(:simple) {SimpleNewDslMongoidMultiple.new}
|
64
|
+
|
65
|
+
it "should initialize the aasm state on instantiation" do
|
66
|
+
expect(SimpleNewDslMongoidMultiple.new.status).to eql 'unknown_scope'
|
67
|
+
expect(SimpleNewDslMongoidMultiple.new.aasm(:left).current_state).to eql :unknown_scope
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
|
72
|
+
describe "complex example" do
|
73
|
+
it "works" do
|
74
|
+
record = ComplexMongoidExample.new
|
75
|
+
expect_aasm_states record, :one, :alpha
|
76
|
+
|
77
|
+
record.save!
|
78
|
+
expect_aasm_states record, :one, :alpha
|
79
|
+
record.reload
|
80
|
+
expect_aasm_states record, :one, :alpha
|
81
|
+
|
82
|
+
record.increment!
|
83
|
+
expect_aasm_states record, :two, :alpha
|
84
|
+
record.reload
|
85
|
+
expect_aasm_states record, :two, :alpha
|
86
|
+
|
87
|
+
record.level_up!
|
88
|
+
expect_aasm_states record, :two, :beta
|
89
|
+
record.reload
|
90
|
+
expect_aasm_states record, :two, :beta
|
91
|
+
|
92
|
+
record.increment!
|
93
|
+
expect { record.increment! }.to raise_error(AASM::InvalidTransition)
|
94
|
+
expect_aasm_states record, :three, :beta
|
95
|
+
record.reload
|
96
|
+
expect_aasm_states record, :three, :beta
|
97
|
+
|
98
|
+
record.level_up!
|
99
|
+
expect_aasm_states record, :three, :gamma
|
100
|
+
record.reload
|
101
|
+
expect_aasm_states record, :three, :gamma
|
102
|
+
|
103
|
+
record.level_down # without saving
|
104
|
+
expect_aasm_states record, :three, :beta
|
105
|
+
record.reload
|
106
|
+
expect_aasm_states record, :three, :gamma
|
107
|
+
|
108
|
+
record.level_down # without saving
|
109
|
+
expect_aasm_states record, :three, :beta
|
110
|
+
record.reset!
|
111
|
+
expect_aasm_states record, :one, :beta
|
112
|
+
end
|
113
|
+
|
114
|
+
def expect_aasm_states(record, left_state, right_state)
|
115
|
+
expect(record.aasm(:left).current_state).to eql left_state.to_sym
|
116
|
+
expect(record.left).to eql left_state.to_s
|
117
|
+
expect(record.aasm(:right).current_state).to eql right_state.to_sym
|
118
|
+
expect(record.right).to eql right_state.to_s
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
rescue LoadError
|
123
|
+
puts "--------------------------------------------------------------------------"
|
124
|
+
puts "Not running Mongoid multiple-specs because mongoid gem is not installed!!!"
|
125
|
+
puts "--------------------------------------------------------------------------"
|
126
|
+
end
|
127
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
describe 'mongoid' do
|
2
|
+
begin
|
3
|
+
require 'mongoid'
|
4
|
+
require 'logger'
|
5
|
+
require 'spec_helper'
|
6
|
+
|
7
|
+
Dir[File.dirname(__FILE__) + "/../../models/mongoid/*.rb"].sort.each do |f|
|
8
|
+
require File.expand_path(f)
|
9
|
+
end
|
10
|
+
|
11
|
+
before(:all) do
|
12
|
+
# if you want to see the statements while running the spec enable the following line
|
13
|
+
# Mongoid.logger = Logger.new(STDERR)
|
14
|
+
|
15
|
+
Mongoid.configure do |config|
|
16
|
+
config.connect_to "mongoid_#{Process.pid}"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
after do
|
21
|
+
Mongoid.purge!
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "named scopes with the old DSL" do
|
25
|
+
|
26
|
+
context "Does not already respond_to? the scope name" do
|
27
|
+
it "should add a scope" do
|
28
|
+
expect(SimpleMongoid).to respond_to(:unknown_scope)
|
29
|
+
expect(SimpleMongoid.unknown_scope.class).to eq(Mongoid::Criteria)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context "Already respond_to? the scope name" do
|
34
|
+
it "should not add a scope" do
|
35
|
+
expect(SimpleMongoid).to respond_to(:new)
|
36
|
+
expect(SimpleMongoid.new.class).to eq(SimpleMongoid)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "named scopes with the new DSL" do
|
43
|
+
|
44
|
+
context "Does not already respond_to? the scope name" do
|
45
|
+
it "should add a scope" do
|
46
|
+
expect(SimpleNewDslMongoid).to respond_to(:unknown_scope)
|
47
|
+
expect(SimpleNewDslMongoid.unknown_scope.class).to eq(Mongoid::Criteria)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
context "Already respond_to? the scope name" do
|
52
|
+
it "should not add a scope" do
|
53
|
+
expect(SimpleNewDslMongoid).to respond_to(:new)
|
54
|
+
expect(SimpleNewDslMongoid.new.class).to eq(SimpleNewDslMongoid)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
it "does not create scopes if requested" do
|
59
|
+
expect(NoScopeMongoid).not_to respond_to(:ignored_scope)
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
describe "instance methods" do
|
65
|
+
let(:simple) {SimpleNewDslMongoid.new}
|
66
|
+
|
67
|
+
it "should initialize the aasm state on instantiation" do
|
68
|
+
expect(SimpleNewDslMongoid.new.status).to eql 'unknown_scope'
|
69
|
+
expect(SimpleNewDslMongoid.new.aasm.current_state).to eql :unknown_scope
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
rescue LoadError
|
75
|
+
puts "--------------------------------------------------------------------------"
|
76
|
+
puts "Not running Mongoid specs because mongoid gem is not installed!!!"
|
77
|
+
puts "--------------------------------------------------------------------------"
|
78
|
+
end
|
79
|
+
end
|