aasm 3.0.15 → 3.0.16
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +1 -0
- data/CHANGELOG.md +4 -0
- data/README.md +31 -0
- data/aasm.gemspec +1 -0
- data/lib/aasm/persistence/active_record_persistence.rb +0 -16
- data/lib/aasm/persistence/base.rb +19 -0
- data/lib/aasm/persistence/mongoid_persistence.rb +20 -14
- data/lib/aasm/version.rb +1 -1
- data/spec/models/mongoid/mongoid_models.rb +23 -0
- data/spec/unit/persistence/mongoid_persistance_spec.rb +126 -0
- metadata +24 -20
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
# CHANGELOG
|
2
2
|
|
3
|
+
## 3.0.16
|
4
|
+
|
5
|
+
* added autocreation of state scopes for Mongoid (thanks to [@jonnyshields](https://github.com/johnnyshields))
|
6
|
+
|
3
7
|
## 3.0.15
|
4
8
|
|
5
9
|
* added support for localized state names (on a class level, like Record.aasm.states.map(&:localized_name))
|
data/README.md
CHANGED
@@ -215,6 +215,37 @@ class Job < ActiveRecord::Base
|
|
215
215
|
end
|
216
216
|
```
|
217
217
|
|
218
|
+
### Automatic Scopes
|
219
|
+
|
220
|
+
AASM will automatically create scope methods for each state in the model.
|
221
|
+
|
222
|
+
```ruby
|
223
|
+
class Job < ActiveRecord::Base
|
224
|
+
include AASM
|
225
|
+
|
226
|
+
aasm do
|
227
|
+
state :sleeping, :initial => true
|
228
|
+
state :running
|
229
|
+
state :cleaning
|
230
|
+
end
|
231
|
+
|
232
|
+
def sleeping
|
233
|
+
"This method name is in already use"
|
234
|
+
end
|
235
|
+
end
|
236
|
+
```
|
237
|
+
|
238
|
+
```ruby
|
239
|
+
class JobsController < ApplicationController
|
240
|
+
def index
|
241
|
+
@running_jobs = jobs.running
|
242
|
+
@recent_cleaning_jobs = jobs.cleaning.where('created_at >= ?', 3.days.ago)
|
243
|
+
|
244
|
+
# @sleeping_jobs = jobs.sleeping #=> "This method name is in already use"
|
245
|
+
end
|
246
|
+
end
|
247
|
+
```
|
248
|
+
|
218
249
|
### Transaction support
|
219
250
|
|
220
251
|
Since version *3.0.13* AASM supports ActiveRecord transactions. So whenever a transition
|
data/aasm.gemspec
CHANGED
@@ -14,6 +14,7 @@ Gem::Specification.new do |s|
|
|
14
14
|
s.licenses = ["MIT"]
|
15
15
|
|
16
16
|
s.add_development_dependency 'activerecord'
|
17
|
+
s.add_development_dependency 'mongoid' if Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version.create('1.9.3')
|
17
18
|
s.add_development_dependency 'rake'
|
18
19
|
s.add_development_dependency 'sdoc'
|
19
20
|
s.add_development_dependency 'rspec', '~> 2.0'
|
@@ -172,19 +172,3 @@ module AASM
|
|
172
172
|
end
|
173
173
|
end
|
174
174
|
end
|
175
|
-
|
176
|
-
class AASM::Base
|
177
|
-
def state_with_scope(name, *args)
|
178
|
-
state_without_scope(name, *args)
|
179
|
-
if @clazz.ancestors.map {|klass| klass.to_s}.include?("ActiveRecord::Base") && !@clazz.respond_to?(name)
|
180
|
-
# puts "setting scope #{@clazz.name}.#{name}"
|
181
|
-
scope_options = {:conditions => { "#{@clazz.table_name}.#{@clazz.aasm_column}" => name.to_s}}
|
182
|
-
scope_method = ActiveRecord::VERSION::MAJOR >= 3 ? :scope : :named_scope
|
183
|
-
@clazz.send(scope_method, name, scope_options)
|
184
|
-
# else
|
185
|
-
# puts "not setting scope #{@clazz.name}.#{name}"
|
186
|
-
end
|
187
|
-
end
|
188
|
-
alias_method :state_without_scope, :state
|
189
|
-
alias_method :state, :state_with_scope
|
190
|
-
end
|
@@ -43,4 +43,23 @@ module AASM
|
|
43
43
|
|
44
44
|
end # Base
|
45
45
|
end # Persistence
|
46
|
+
|
47
|
+
class Base
|
48
|
+
def state_with_scope(name, *args)
|
49
|
+
state_without_scope(name, *args)
|
50
|
+
unless @clazz.respond_to?(name)
|
51
|
+
if @clazz.ancestors.map {|klass| klass.to_s}.include?("ActiveRecord::Base")
|
52
|
+
scope_options = {:conditions => { "#{@clazz.table_name}.#{@clazz.aasm_column}" => name.to_s}}
|
53
|
+
scope_method = ActiveRecord::VERSION::MAJOR >= 3 ? :scope : :named_scope
|
54
|
+
@clazz.send(scope_method, name, scope_options)
|
55
|
+
elsif @clazz.ancestors.map {|klass| klass.to_s}.include?("Mongoid::Document")
|
56
|
+
scope_options = lambda { @clazz.send(:where, {@clazz.aasm_column.to_sym => name.to_s}) }
|
57
|
+
@clazz.send(:scope, name, scope_options)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
alias_method :state_without_scope, :state
|
62
|
+
alias_method :state, :state_with_scope
|
63
|
+
end # Base
|
64
|
+
|
46
65
|
end # AASM
|
@@ -41,25 +41,31 @@ module AASM
|
|
41
41
|
base.send(:include, AASM::Persistence::MongoidPersistence::WriteState) unless base.method_defined?(:aasm_write_state)
|
42
42
|
base.send(:include, AASM::Persistence::MongoidPersistence::WriteStateWithoutPersistence) unless base.method_defined?(:aasm_write_state_without_persistence)
|
43
43
|
|
44
|
-
# if base.respond_to?(:named_scope)
|
45
|
-
# base.extend(AASM::Persistence::MongoidPersistence::NamedScopeMethods)
|
46
|
-
#
|
47
|
-
# base.class_eval do
|
48
|
-
# class << self
|
49
|
-
# unless method_defined?(:aasm_state_without_named_scope)
|
50
|
-
# alias_method :aasm_state_without_named_scope, :aasm_state
|
51
|
-
# alias_method :aasm_state, :aasm_state_with_named_scope
|
52
|
-
# end
|
53
|
-
# end
|
54
|
-
# end
|
55
|
-
# end
|
56
|
-
|
57
44
|
# Mongoid's Validatable gem dependency goes not have a before_validation_on_xxx hook yet.
|
58
45
|
# base.before_validation_on_create :aasm_ensure_initial_state
|
59
46
|
base.before_validation :aasm_ensure_initial_state
|
60
47
|
end
|
61
48
|
|
62
49
|
module ClassMethods
|
50
|
+
|
51
|
+
def find_in_state(number, state, *args)
|
52
|
+
with_state_scope state do
|
53
|
+
find(number, *args)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def count_in_state(state, *args)
|
58
|
+
with_state_scope state do
|
59
|
+
count(*args)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def with_state_scope(state)
|
64
|
+
with_scope where(aasm_column.to_sym => state.to_s) do
|
65
|
+
yield if block_given?
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
63
69
|
end
|
64
70
|
|
65
71
|
module InstanceMethods
|
@@ -155,4 +161,4 @@ module AASM
|
|
155
161
|
end
|
156
162
|
end
|
157
163
|
end
|
158
|
-
end
|
164
|
+
end
|
data/lib/aasm/version.rb
CHANGED
@@ -0,0 +1,23 @@
|
|
1
|
+
class SimpleMongoid
|
2
|
+
include Mongoid::Document
|
3
|
+
include AASM
|
4
|
+
|
5
|
+
field :status, type: String
|
6
|
+
|
7
|
+
aasm_column :status
|
8
|
+
aasm_state :unknown_scope
|
9
|
+
aasm_state :new
|
10
|
+
end
|
11
|
+
|
12
|
+
class SimpleNewDslMongoid
|
13
|
+
include Mongoid::Document
|
14
|
+
include AASM
|
15
|
+
|
16
|
+
field :status, type: String
|
17
|
+
|
18
|
+
aasm :column => :status
|
19
|
+
aasm do
|
20
|
+
state :unknown_scope
|
21
|
+
state :new
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,126 @@
|
|
1
|
+
describe 'mongoid', :if => Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version.create('1.9.3') do
|
2
|
+
|
3
|
+
before(:all) do
|
4
|
+
require 'mongoid'
|
5
|
+
require 'logger'
|
6
|
+
require 'spec_helper'
|
7
|
+
require File.dirname(__FILE__) + '/../../models/mongoid/mongoid_models'
|
8
|
+
|
9
|
+
# if you want to see the statements while running the spec enable the following line
|
10
|
+
# Mongoid.logger = Logger.new(STDERR)
|
11
|
+
|
12
|
+
DATABASE_NAME = "mongoid_#{Process.pid}"
|
13
|
+
|
14
|
+
Mongoid.configure do |config|
|
15
|
+
config.connect_to DATABASE_NAME
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
after do
|
20
|
+
Mongoid.purge!
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "named scopes with the old DSL" do
|
24
|
+
|
25
|
+
context "Does not already respond_to? the scope name" do
|
26
|
+
it "should add a scope" do
|
27
|
+
SimpleMongoid.should respond_to(:unknown_scope)
|
28
|
+
SimpleMongoid.unknown_scope.class.should == Mongoid::Criteria
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context "Already respond_to? the scope name" do
|
33
|
+
it "should not add a scope" do
|
34
|
+
SimpleMongoid.should respond_to(:new)
|
35
|
+
SimpleMongoid.new.class.should == SimpleMongoid
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "named scopes with the new DSL" do
|
42
|
+
|
43
|
+
context "Does not already respond_to? the scope name" do
|
44
|
+
it "should add a scope" do
|
45
|
+
SimpleNewDslMongoid.should respond_to(:unknown_scope)
|
46
|
+
SimpleNewDslMongoid.unknown_scope.class.should == 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
|
+
SimpleNewDslMongoid.should respond_to(:new)
|
53
|
+
SimpleNewDslMongoid.new.class.should == SimpleNewDslMongoid
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
describe "#find_in_state" do
|
60
|
+
|
61
|
+
let!(:model) { SimpleNewDslMongoid.create!(:status => :unknown_scope) }
|
62
|
+
let!(:model_id) { model._id }
|
63
|
+
|
64
|
+
it "should respond to method" do
|
65
|
+
SimpleNewDslMongoid.should respond_to(:find_in_state)
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should find the model when given the correct scope and model id" do
|
69
|
+
SimpleNewDslMongoid.find_in_state(model_id, 'unknown_scope').class.should == SimpleNewDslMongoid
|
70
|
+
SimpleNewDslMongoid.find_in_state(model_id, 'unknown_scope').should == model
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should raise DocumentNotFound error when given incorrect scope" do
|
74
|
+
expect {SimpleNewDslMongoid.find_in_state(model_id, 'new')}.to raise_error Mongoid::Errors::DocumentNotFound
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should raise DocumentNotFound error when given incorrect model id" do
|
78
|
+
expect {SimpleNewDslMongoid.find_in_state('bad_id', 'unknown_scope')}.to raise_error Mongoid::Errors::DocumentNotFound
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
describe "#count_in_state" do
|
84
|
+
|
85
|
+
before do
|
86
|
+
3.times { SimpleNewDslMongoid.create!(:status => :unknown_scope) }
|
87
|
+
end
|
88
|
+
|
89
|
+
it "should respond to method" do
|
90
|
+
SimpleNewDslMongoid.should respond_to(:count_in_state)
|
91
|
+
end
|
92
|
+
|
93
|
+
it "should return n for a scope with n records persisted" do
|
94
|
+
SimpleNewDslMongoid.count_in_state('unknown_scope').class.should == Fixnum
|
95
|
+
SimpleNewDslMongoid.count_in_state('unknown_scope').should == 3
|
96
|
+
end
|
97
|
+
|
98
|
+
it "should return zero for a scope without records persisted" do
|
99
|
+
SimpleNewDslMongoid.count_in_state('new').class.should == Fixnum
|
100
|
+
SimpleNewDslMongoid.count_in_state('new').should == 0
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
104
|
+
|
105
|
+
describe "#with_state_scope" do
|
106
|
+
|
107
|
+
before do
|
108
|
+
3.times { SimpleNewDslMongoid.create!(:status => :unknown_scope) }
|
109
|
+
2.times { SimpleNewDslMongoid.create!(:status => :new) }
|
110
|
+
end
|
111
|
+
|
112
|
+
it "should respond to method" do
|
113
|
+
SimpleNewDslMongoid.should respond_to(:with_state_scope)
|
114
|
+
end
|
115
|
+
|
116
|
+
it "should correctly process block" do
|
117
|
+
SimpleNewDslMongoid.with_state_scope('unknown_scope') do
|
118
|
+
SimpleNewDslMongoid.count
|
119
|
+
end.should == 3
|
120
|
+
SimpleNewDslMongoid.with_state_scope('new') do
|
121
|
+
SimpleNewDslMongoid.count
|
122
|
+
end.should == 2
|
123
|
+
end
|
124
|
+
|
125
|
+
end
|
126
|
+
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.
|
4
|
+
version: 3.0.16
|
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:
|
15
|
+
date: 2013-01-16 00:00:00.000000000Z
|
16
16
|
dependencies:
|
17
17
|
- !ruby/object:Gem::Dependency
|
18
18
|
name: activerecord
|
19
|
-
requirement: &
|
19
|
+
requirement: &70169537118900 !ruby/object:Gem::Requirement
|
20
20
|
none: false
|
21
21
|
requirements:
|
22
22
|
- - ! '>='
|
@@ -24,10 +24,10 @@ dependencies:
|
|
24
24
|
version: '0'
|
25
25
|
type: :development
|
26
26
|
prerelease: false
|
27
|
-
version_requirements: *
|
27
|
+
version_requirements: *70169537118900
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
29
|
name: rake
|
30
|
-
requirement: &
|
30
|
+
requirement: &70169537117620 !ruby/object:Gem::Requirement
|
31
31
|
none: false
|
32
32
|
requirements:
|
33
33
|
- - ! '>='
|
@@ -35,10 +35,10 @@ dependencies:
|
|
35
35
|
version: '0'
|
36
36
|
type: :development
|
37
37
|
prerelease: false
|
38
|
-
version_requirements: *
|
38
|
+
version_requirements: *70169537117620
|
39
39
|
- !ruby/object:Gem::Dependency
|
40
40
|
name: sdoc
|
41
|
-
requirement: &
|
41
|
+
requirement: &70169537117180 !ruby/object:Gem::Requirement
|
42
42
|
none: false
|
43
43
|
requirements:
|
44
44
|
- - ! '>='
|
@@ -46,10 +46,10 @@ dependencies:
|
|
46
46
|
version: '0'
|
47
47
|
type: :development
|
48
48
|
prerelease: false
|
49
|
-
version_requirements: *
|
49
|
+
version_requirements: *70169537117180
|
50
50
|
- !ruby/object:Gem::Dependency
|
51
51
|
name: rspec
|
52
|
-
requirement: &
|
52
|
+
requirement: &70169537116680 !ruby/object:Gem::Requirement
|
53
53
|
none: false
|
54
54
|
requirements:
|
55
55
|
- - ~>
|
@@ -57,10 +57,10 @@ dependencies:
|
|
57
57
|
version: '2.0'
|
58
58
|
type: :development
|
59
59
|
prerelease: false
|
60
|
-
version_requirements: *
|
60
|
+
version_requirements: *70169537116680
|
61
61
|
- !ruby/object:Gem::Dependency
|
62
62
|
name: rr
|
63
|
-
requirement: &
|
63
|
+
requirement: &70169537116260 !ruby/object:Gem::Requirement
|
64
64
|
none: false
|
65
65
|
requirements:
|
66
66
|
- - ! '>='
|
@@ -68,10 +68,10 @@ dependencies:
|
|
68
68
|
version: '0'
|
69
69
|
type: :development
|
70
70
|
prerelease: false
|
71
|
-
version_requirements: *
|
71
|
+
version_requirements: *70169537116260
|
72
72
|
- !ruby/object:Gem::Dependency
|
73
73
|
name: shoulda
|
74
|
-
requirement: &
|
74
|
+
requirement: &70169537147800 !ruby/object:Gem::Requirement
|
75
75
|
none: false
|
76
76
|
requirements:
|
77
77
|
- - ! '>='
|
@@ -79,10 +79,10 @@ dependencies:
|
|
79
79
|
version: '0'
|
80
80
|
type: :development
|
81
81
|
prerelease: false
|
82
|
-
version_requirements: *
|
82
|
+
version_requirements: *70169537147800
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: sqlite3
|
85
|
-
requirement: &
|
85
|
+
requirement: &70169537147380 !ruby/object:Gem::Requirement
|
86
86
|
none: false
|
87
87
|
requirements:
|
88
88
|
- - ! '>='
|
@@ -90,10 +90,10 @@ dependencies:
|
|
90
90
|
version: '0'
|
91
91
|
type: :development
|
92
92
|
prerelease: false
|
93
|
-
version_requirements: *
|
93
|
+
version_requirements: *70169537147380
|
94
94
|
- !ruby/object:Gem::Dependency
|
95
95
|
name: minitest
|
96
|
-
requirement: &
|
96
|
+
requirement: &70169537146960 !ruby/object:Gem::Requirement
|
97
97
|
none: false
|
98
98
|
requirements:
|
99
99
|
- - ! '>='
|
@@ -101,10 +101,10 @@ dependencies:
|
|
101
101
|
version: '0'
|
102
102
|
type: :development
|
103
103
|
prerelease: false
|
104
|
-
version_requirements: *
|
104
|
+
version_requirements: *70169537146960
|
105
105
|
- !ruby/object:Gem::Dependency
|
106
106
|
name: ruby-debug-completion
|
107
|
-
requirement: &
|
107
|
+
requirement: &70169537146540 !ruby/object:Gem::Requirement
|
108
108
|
none: false
|
109
109
|
requirements:
|
110
110
|
- - ! '>='
|
@@ -112,7 +112,7 @@ dependencies:
|
|
112
112
|
version: '0'
|
113
113
|
type: :development
|
114
114
|
prerelease: false
|
115
|
-
version_requirements: *
|
115
|
+
version_requirements: *70169537146540
|
116
116
|
description: AASM is a continuation of the acts as state machine rails plugin, built
|
117
117
|
for plain Ruby objects.
|
118
118
|
email: scott@elitists.net, ttilley@gmail.com, aasm@mt7.de
|
@@ -154,6 +154,7 @@ files:
|
|
154
154
|
- spec/models/callback_old_dsl.rb
|
155
155
|
- spec/models/conversation.rb
|
156
156
|
- spec/models/invalid_persistor.rb
|
157
|
+
- spec/models/mongoid/mongoid_models.rb
|
157
158
|
- spec/models/not_auto_loaded/process.rb
|
158
159
|
- spec/models/parametrised_event.rb
|
159
160
|
- spec/models/persistence.rb
|
@@ -173,6 +174,7 @@ files:
|
|
173
174
|
- spec/unit/memory_leak_spec.rb
|
174
175
|
- spec/unit/new_dsl_spec.rb
|
175
176
|
- spec/unit/persistence/active_record_persistence_spec.rb
|
177
|
+
- spec/unit/persistence/mongoid_persistance_spec.rb
|
176
178
|
- spec/unit/simple_example_spec.rb
|
177
179
|
- spec/unit/subclassing_spec.rb
|
178
180
|
- spec/unit/supporting_classes/event_spec.rb
|
@@ -214,6 +216,7 @@ test_files:
|
|
214
216
|
- spec/models/callback_old_dsl.rb
|
215
217
|
- spec/models/conversation.rb
|
216
218
|
- spec/models/invalid_persistor.rb
|
219
|
+
- spec/models/mongoid/mongoid_models.rb
|
217
220
|
- spec/models/not_auto_loaded/process.rb
|
218
221
|
- spec/models/parametrised_event.rb
|
219
222
|
- spec/models/persistence.rb
|
@@ -233,6 +236,7 @@ test_files:
|
|
233
236
|
- spec/unit/memory_leak_spec.rb
|
234
237
|
- spec/unit/new_dsl_spec.rb
|
235
238
|
- spec/unit/persistence/active_record_persistence_spec.rb
|
239
|
+
- spec/unit/persistence/mongoid_persistance_spec.rb
|
236
240
|
- spec/unit/simple_example_spec.rb
|
237
241
|
- spec/unit/subclassing_spec.rb
|
238
242
|
- spec/unit/supporting_classes/event_spec.rb
|