aasm 3.0.24 → 3.0.25

Sign up to get free protection for your applications and to get access to all the features.
@@ -10,12 +10,12 @@ class LocalizerTestModel < ActiveRecord::Base
10
10
 
11
11
  attr_accessor :aasm_state
12
12
 
13
- aasm_initial_state :opened
14
- aasm_state :opened
15
- aasm_state :closed
16
-
17
- aasm_event :close
18
- aasm_event :open
13
+ aasm do
14
+ state :opened, :initial => true
15
+ state :closed
16
+ event :close
17
+ event :open
18
+ end
19
19
  end
20
20
 
21
21
  describe 'localized state names' do
@@ -52,13 +52,13 @@ describe AASM::Localizer, "new style" do
52
52
  let (:foo_opened) { LocalizerTestModel.new }
53
53
  let (:foo_closed) { LocalizerTestModel.new.tap { |x| x.aasm_state = :closed } }
54
54
 
55
- context 'aasm_human_state' do
55
+ context 'aasm.human_state' do
56
56
  it 'should return translated state value' do
57
- foo_opened.aasm_human_state.should == "It's open now!"
57
+ foo_opened.aasm.human_state.should == "It's open now!"
58
58
  end
59
59
 
60
60
  it 'should return humanized value if not localized' do
61
- foo_closed.aasm_human_state.should == "Closed"
61
+ foo_closed.aasm.human_state.should == "Closed"
62
62
  end
63
63
  end
64
64
 
@@ -87,13 +87,13 @@ describe AASM::Localizer, "deprecated style" do
87
87
  let (:foo_opened) { LocalizerTestModel.new }
88
88
  let (:foo_closed) { LocalizerTestModel.new.tap { |x| x.aasm_state = :closed } }
89
89
 
90
- context 'aasm_human_state' do
90
+ context 'aasm.human_state' do
91
91
  it 'should return translated state value' do
92
- foo_opened.aasm_human_state.should == "It's open now!"
92
+ foo_opened.aasm.human_state.should == "It's open now!"
93
93
  end
94
94
 
95
95
  it 'should return humanized value if not localized' do
96
- foo_closed.aasm_human_state.should == "Closed"
96
+ foo_closed.aasm.human_state.should == "Closed"
97
97
  end
98
98
  end
99
99
 
@@ -24,24 +24,24 @@ describe "instance methods" do
24
24
  end
25
25
 
26
26
  it "should return the initial state when new and the aasm field is nil" do
27
- gate.aasm_current_state.should == :opened
27
+ gate.aasm.current_state.should == :opened
28
28
  end
29
29
 
30
30
  it "should return the aasm column when new and the aasm field is not nil" do
31
31
  gate.aasm_state = "closed"
32
- gate.aasm_current_state.should == :closed
32
+ gate.aasm.current_state.should == :closed
33
33
  end
34
34
 
35
35
  it "should return the aasm column when not new and the aasm_column is not nil" do
36
36
  gate.stub(:new_record?).and_return(false)
37
37
  gate.aasm_state = "state"
38
- gate.aasm_current_state.should == :state
38
+ gate.aasm.current_state.should == :state
39
39
  end
40
40
 
41
41
  it "should allow a nil state" do
42
42
  gate.stub(:new_record?).and_return(false)
43
43
  gate.aasm_state = nil
44
- gate.aasm_current_state.should be_nil
44
+ gate.aasm.current_state.should be_nil
45
45
  end
46
46
 
47
47
  it "should call aasm_ensure_initial_state on validation before create" do
@@ -49,6 +49,11 @@ describe "instance methods" do
49
49
  gate.valid?
50
50
  end
51
51
 
52
+ it "should call aasm_ensure_initial_state before create, even if skipping validations" do
53
+ gate.should_receive(:aasm_ensure_initial_state).and_return(true)
54
+ gate.save(:validate => false)
55
+ end
56
+
52
57
  it "should not call aasm_ensure_initial_state on validation before update" do
53
58
  gate.stub(:new_record?).and_return(false)
54
59
  gate.should_not_receive(:aasm_ensure_initial_state)
@@ -59,15 +64,11 @@ end
59
64
 
60
65
  describe 'subclasses' do
61
66
  it "should have the same states as its parent class" do
62
- Derivate.aasm_states.should == Simple.aasm_states
67
+ DerivateNewDsl.aasm.states.should == SimpleNewDsl.aasm.states
63
68
  end
64
69
 
65
70
  it "should have the same events as its parent class" do
66
- Derivate.aasm_events.should == Simple.aasm_events
67
- end
68
-
69
- it "should have the same column as its parent class" do
70
- Derivate.aasm_column.should == :status
71
+ DerivateNewDsl.aasm.events.should == SimpleNewDsl.aasm.events
71
72
  end
72
73
 
73
74
  it "should have the same column as its parent even for the new dsl" do
@@ -76,26 +77,7 @@ describe 'subclasses' do
76
77
  end
77
78
  end
78
79
 
79
- describe "named scopes with the old DSL" do
80
-
81
- context "Does not already respond_to? the scope name" do
82
- it "should add a scope" do
83
- Simple.should respond_to(:unknown_scope)
84
- SimpleNewDsl.unknown_scope.is_a?(ActiveRecord::Relation).should be_true
85
- end
86
- end
87
-
88
- context "Already respond_to? the scope name" do
89
- it "should not add a scope" do
90
- Simple.should respond_to(:new)
91
- Simple.new.class.should == Simple
92
- end
93
- end
94
-
95
- end
96
-
97
80
  describe "named scopes with the new DSL" do
98
-
99
81
  context "Does not already respond_to? the scope name" do
100
82
  it "should add a scope" do
101
83
  SimpleNewDsl.should respond_to(:unknown_scope)
@@ -119,8 +101,8 @@ end
119
101
  describe 'initial states' do
120
102
 
121
103
  it 'should support conditions' do
122
- Thief.new(:skilled => true).aasm_current_state.should == :rich
123
- Thief.new(:skilled => false).aasm_current_state.should == :jailed
104
+ Thief.new(:skilled => true).aasm.current_state.should == :rich
105
+ Thief.new(:skilled => false).aasm.current_state.should == :jailed
124
106
  end
125
107
  end
126
108
 
@@ -1,131 +1,152 @@
1
1
  describe 'mongoid', :if => Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version.create('1.9.3') do
2
2
  # describe 'mongoid' do
3
3
 
4
- before(:all) do
4
+ begin
5
5
  require 'mongoid'
6
6
  require 'logger'
7
7
  require 'spec_helper'
8
- Dir[File.dirname(__FILE__) + "/../../models/mongoid/*.rb"].sort.each { |f| require File.expand_path(f) }
9
8
 
10
- # if you want to see the statements while running the spec enable the following line
11
- # Mongoid.logger = Logger.new(STDERR)
9
+ before(:all) do
10
+ Dir[File.dirname(__FILE__) + "/../../models/mongoid/*.rb"].sort.each { |f| require File.expand_path(f) }
12
11
 
13
- DATABASE_NAME = "mongoid_#{Process.pid}"
12
+ # if you want to see the statements while running the spec enable the following line
13
+ # Mongoid.logger = Logger.new(STDERR)
14
14
 
15
- Mongoid.configure do |config|
16
- config.connect_to DATABASE_NAME
15
+ DATABASE_NAME = "mongoid_#{Process.pid}"
16
+
17
+ Mongoid.configure do |config|
18
+ config.connect_to DATABASE_NAME
19
+ end
17
20
  end
18
- end
19
21
 
20
- after do
21
- Mongoid.purge!
22
- end
22
+ after do
23
+ Mongoid.purge!
24
+ end
23
25
 
24
- describe "named scopes with the old DSL" do
26
+ describe "named scopes with the old DSL" do
25
27
 
26
- context "Does not already respond_to? the scope name" do
27
- it "should add a scope" do
28
- SimpleMongoid.should respond_to(:unknown_scope)
29
- SimpleMongoid.unknown_scope.class.should == Mongoid::Criteria
28
+ context "Does not already respond_to? the scope name" do
29
+ it "should add a scope" do
30
+ SimpleMongoid.should respond_to(:unknown_scope)
31
+ SimpleMongoid.unknown_scope.class.should == Mongoid::Criteria
32
+ end
30
33
  end
31
- end
32
34
 
33
- context "Already respond_to? the scope name" do
34
- it "should not add a scope" do
35
- SimpleMongoid.should respond_to(:new)
36
- SimpleMongoid.new.class.should == SimpleMongoid
35
+ context "Already respond_to? the scope name" do
36
+ it "should not add a scope" do
37
+ SimpleMongoid.should respond_to(:new)
38
+ SimpleMongoid.new.class.should == SimpleMongoid
39
+ end
37
40
  end
41
+
38
42
  end
39
43
 
40
- end
44
+ describe "named scopes with the new DSL" do
41
45
 
42
- describe "named scopes with the new DSL" do
46
+ context "Does not already respond_to? the scope name" do
47
+ it "should add a scope" do
48
+ SimpleNewDslMongoid.should respond_to(:unknown_scope)
49
+ SimpleNewDslMongoid.unknown_scope.class.should == Mongoid::Criteria
50
+ end
51
+ end
43
52
 
44
- context "Does not already respond_to? the scope name" do
45
- it "should add a scope" do
46
- SimpleNewDslMongoid.should respond_to(:unknown_scope)
47
- SimpleNewDslMongoid.unknown_scope.class.should == Mongoid::Criteria
53
+ context "Already respond_to? the scope name" do
54
+ it "should not add a scope" do
55
+ SimpleNewDslMongoid.should respond_to(:new)
56
+ SimpleNewDslMongoid.new.class.should == SimpleNewDslMongoid
57
+ end
48
58
  end
49
- end
50
59
 
51
- context "Already respond_to? the scope name" do
52
- it "should not add a scope" do
53
- SimpleNewDslMongoid.should respond_to(:new)
54
- SimpleNewDslMongoid.new.class.should == SimpleNewDslMongoid
60
+ it "does not create scopes if requested" do
61
+ NoScopeMongoid.should_not respond_to(:ignored_scope)
55
62
  end
56
- end
57
63
 
58
- it "does not create scopes if requested" do
59
- NoScopeMongoid.should_not respond_to(:ignored_scope)
60
64
  end
61
65
 
62
- end
66
+ describe "#find_in_state" do
63
67
 
64
- describe "#find_in_state" do
68
+ let!(:model) { SimpleNewDslMongoid.create!(:status => :unknown_scope) }
69
+ let!(:model_id) { model._id }
65
70
 
66
- let!(:model) { SimpleNewDslMongoid.create!(:status => :unknown_scope) }
67
- let!(:model_id) { model._id }
71
+ it "should respond to method" do
72
+ SimpleNewDslMongoid.should respond_to(:find_in_state)
73
+ end
68
74
 
69
- it "should respond to method" do
70
- SimpleNewDslMongoid.should respond_to(:find_in_state)
71
- end
75
+ it "should find the model when given the correct scope and model id" do
76
+ SimpleNewDslMongoid.find_in_state(model_id, 'unknown_scope').class.should == SimpleNewDslMongoid
77
+ SimpleNewDslMongoid.find_in_state(model_id, 'unknown_scope').should == model
78
+ end
72
79
 
73
- it "should find the model when given the correct scope and model id" do
74
- SimpleNewDslMongoid.find_in_state(model_id, 'unknown_scope').class.should == SimpleNewDslMongoid
75
- SimpleNewDslMongoid.find_in_state(model_id, 'unknown_scope').should == model
76
- end
80
+ it "should raise DocumentNotFound error when given incorrect scope" do
81
+ expect {SimpleNewDslMongoid.find_in_state(model_id, 'new')}.to raise_error Mongoid::Errors::DocumentNotFound
82
+ end
77
83
 
78
- it "should raise DocumentNotFound error when given incorrect scope" do
79
- expect {SimpleNewDslMongoid.find_in_state(model_id, 'new')}.to raise_error Mongoid::Errors::DocumentNotFound
80
- end
84
+ it "should raise DocumentNotFound error when given incorrect model id" do
85
+ expect {SimpleNewDslMongoid.find_in_state('bad_id', 'unknown_scope')}.to raise_error Mongoid::Errors::DocumentNotFound
86
+ end
81
87
 
82
- it "should raise DocumentNotFound error when given incorrect model id" do
83
- expect {SimpleNewDslMongoid.find_in_state('bad_id', 'unknown_scope')}.to raise_error Mongoid::Errors::DocumentNotFound
84
88
  end
85
89
 
86
- end
90
+ describe "#count_in_state" do
87
91
 
88
- describe "#count_in_state" do
92
+ before do
93
+ 3.times { SimpleNewDslMongoid.create!(:status => :unknown_scope) }
94
+ end
89
95
 
90
- before do
91
- 3.times { SimpleNewDslMongoid.create!(:status => :unknown_scope) }
92
- end
96
+ it "should respond to method" do
97
+ SimpleNewDslMongoid.should respond_to(:count_in_state)
98
+ end
93
99
 
94
- it "should respond to method" do
95
- SimpleNewDslMongoid.should respond_to(:count_in_state)
96
- end
100
+ it "should return n for a scope with n records persisted" do
101
+ SimpleNewDslMongoid.count_in_state('unknown_scope').class.should == Fixnum
102
+ SimpleNewDslMongoid.count_in_state('unknown_scope').should == 3
103
+ end
97
104
 
98
- it "should return n for a scope with n records persisted" do
99
- SimpleNewDslMongoid.count_in_state('unknown_scope').class.should == Fixnum
100
- SimpleNewDslMongoid.count_in_state('unknown_scope').should == 3
101
- end
105
+ it "should return zero for a scope without records persisted" do
106
+ SimpleNewDslMongoid.count_in_state('new').class.should == Fixnum
107
+ SimpleNewDslMongoid.count_in_state('new').should == 0
108
+ end
102
109
 
103
- it "should return zero for a scope without records persisted" do
104
- SimpleNewDslMongoid.count_in_state('new').class.should == Fixnum
105
- SimpleNewDslMongoid.count_in_state('new').should == 0
106
110
  end
107
111
 
108
- end
112
+ describe "#with_state_scope" do
109
113
 
110
- describe "#with_state_scope" do
114
+ before do
115
+ 3.times { SimpleNewDslMongoid.create!(:status => :unknown_scope) }
116
+ 2.times { SimpleNewDslMongoid.create!(:status => :new) }
117
+ end
111
118
 
112
- before do
113
- 3.times { SimpleNewDslMongoid.create!(:status => :unknown_scope) }
114
- 2.times { SimpleNewDslMongoid.create!(:status => :new) }
115
- end
119
+ it "should respond to method" do
120
+ SimpleNewDslMongoid.should respond_to(:with_state_scope)
121
+ end
122
+
123
+ it "should correctly process block" do
124
+ SimpleNewDslMongoid.with_state_scope('unknown_scope') do
125
+ SimpleNewDslMongoid.count
126
+ end.should == 3
127
+ SimpleNewDslMongoid.with_state_scope('new') do
128
+ SimpleNewDslMongoid.count
129
+ end.should == 2
130
+ end
116
131
 
117
- it "should respond to method" do
118
- SimpleNewDslMongoid.should respond_to(:with_state_scope)
119
132
  end
120
133
 
121
- it "should correctly process block" do
122
- SimpleNewDslMongoid.with_state_scope('unknown_scope') do
123
- SimpleNewDslMongoid.count
124
- end.should == 3
125
- SimpleNewDslMongoid.with_state_scope('new') do
126
- SimpleNewDslMongoid.count
127
- end.should == 2
134
+
135
+ describe "instance methods" do
136
+ let(:simple) {SimpleNewDslMongoid.new}
137
+
138
+ it "should call aasm_ensure_initial_state on validation before create" do
139
+ simple.should_receive(:aasm_ensure_initial_state).and_return(true)
140
+ simple.valid?
141
+ end
142
+
143
+ it "should call aasm_ensure_initial_state before create, even if skipping validations" do
144
+ simple.should_receive(:aasm_ensure_initial_state).and_return(true)
145
+ simple.save(:validate => false)
146
+ end
128
147
  end
129
148
 
149
+ rescue LoadError
150
+ puts "Not running Mongoid specs because mongoid gem if not installed!!!"
130
151
  end
131
152
  end
@@ -20,8 +20,7 @@ describe 'state machine' do
20
20
  let(:payment) {Payment.new}
21
21
 
22
22
  it 'starts with an initial state' do
23
- payment.aasm_current_state.should == :initialised
24
- # payment.aasm.current_state.should == :initialised # not yet supported
23
+ payment.aasm.current_state.should == :initialised
25
24
  payment.should respond_to(:initialised?)
26
25
  payment.should be_initialised
27
26
  end
@@ -4,18 +4,18 @@ describe 'subclassing' do
4
4
  let(:son) {Son.new}
5
5
 
6
6
  it 'should have the parent states' do
7
- Foo.aasm_states.each do |state|
8
- FooTwo.aasm_states.should include(state)
7
+ Foo.aasm.states.each do |state|
8
+ FooTwo.aasm.states.should include(state)
9
9
  end
10
- Baz.aasm_states.should == Bar.aasm_states
10
+ Baz.aasm.states.should == Bar.aasm.states
11
11
  end
12
12
 
13
13
  it 'should not add the child states to the parent machine' do
14
- Foo.aasm_states.should_not include(:foo)
14
+ Foo.aasm.states.should_not include(:foo)
15
15
  end
16
16
 
17
17
  it "should have the same events as its parent" do
18
- Baz.aasm_events.should == Bar.aasm_events
18
+ Baz.aasm.events.should == Bar.aasm.events
19
19
  end
20
20
 
21
21
  it 'should know how to respond to `may_add_details?`' do
@@ -24,8 +24,8 @@ describe 'subclassing' do
24
24
 
25
25
  it 'should not break if I call Son#update_state' do
26
26
  son.update_state
27
- son.aasm_current_state.should == :pending_details_confirmation
27
+ son.aasm.current_state.should == :pending_details_confirmation
28
28
  end
29
-
29
+
30
30
  end
31
31
 
@@ -187,13 +187,13 @@ describe AASM::Transition, '- when executing the transition with an :on_transtio
187
187
  args = {:arg1 => '1', :arg2 => '2'}
188
188
  obj = double('object')
189
189
 
190
- obj.class.class_eval do
191
- define_method(:test) {|*args| 'success'}
190
+ def obj.test(args)
191
+ "arg1: #{args[:arg1]} arg2: #{args[:arg2]}"
192
192
  end
193
193
 
194
194
  return_value = st.execute(obj, args)
195
195
 
196
- return_value.should == 'success'
196
+ return_value.should == 'arg1: 1 arg2: 2'
197
197
  end
198
198
 
199
199
  it 'should NOT pass args if the target method does NOT accept them' do
@@ -202,8 +202,8 @@ describe AASM::Transition, '- when executing the transition with an :on_transtio
202
202
  args = {:arg1 => '1', :arg2 => '2'}
203
203
  obj = double('object')
204
204
 
205
- obj.class.class_eval do
206
- define_method(:test) {|*args| 'success'}
205
+ def obj.test
206
+ 'success'
207
207
  end
208
208
 
209
209
  return_value = st.execute(obj, args)