aasm 4.0.8 → 4.5.2
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/.travis.yml +7 -2
- data/CHANGELOG.md +43 -2
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +6 -3
- data/LICENSE +1 -1
- data/PLANNED_CHANGES.md +8 -4
- data/README.md +171 -14
- data/aasm.gemspec +1 -5
- data/gemfiles/rails_3.2.gemfile +3 -2
- data/gemfiles/rails_4.0.gemfile +2 -6
- data/gemfiles/rails_4.0_mongo_mapper.gemfile +14 -0
- data/gemfiles/rails_4.1.gemfile +2 -6
- data/gemfiles/rails_4.1_mongo_mapper.gemfile +14 -0
- data/gemfiles/rails_4.2.gemfile +1 -5
- data/gemfiles/rails_4.2_mongo_mapper.gemfile +14 -0
- data/gemfiles/rails_4.2_mongoid_5.gemfile +12 -0
- data/lib/aasm/aasm.rb +69 -36
- data/lib/aasm/base.rb +51 -21
- data/lib/aasm/core/event.rb +7 -6
- data/lib/aasm/core/state.rb +11 -8
- data/lib/aasm/core/transition.rb +7 -5
- data/lib/aasm/errors.rb +15 -1
- data/lib/aasm/instance_base.rb +17 -13
- data/lib/aasm/localizer.rb +1 -1
- data/lib/aasm/persistence/active_record_persistence.rb +63 -73
- data/lib/aasm/persistence/base.rb +55 -20
- data/lib/aasm/persistence/mongo_mapper_persistence.rb +157 -0
- data/lib/aasm/persistence/mongoid_persistence.rb +16 -41
- data/lib/aasm/persistence/plain_persistence.rb +8 -7
- data/lib/aasm/persistence/sequel_persistence.rb +12 -9
- data/lib/aasm/persistence.rb +2 -0
- data/lib/aasm/rspec/allow_event.rb +22 -0
- data/lib/aasm/rspec/allow_transition_to.rb +22 -0
- data/lib/aasm/rspec/have_state.rb +22 -0
- data/lib/aasm/rspec/transition_from.rb +32 -0
- data/lib/aasm/rspec.rb +5 -0
- data/lib/aasm/state_machine.rb +20 -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 +7 -0
- data/spec/models/active_record/false_state.rb +35 -0
- data/spec/models/active_record/gate.rb +39 -0
- data/spec/models/active_record/localizer_test_model.rb +34 -0
- data/spec/models/active_record/no_direct_assignment.rb +21 -0
- data/spec/models/active_record/no_scope.rb +21 -0
- data/spec/models/active_record/persisted_state.rb +12 -0
- data/spec/models/active_record/provided_and_persisted_state.rb +24 -0
- data/spec/models/active_record/reader.rb +7 -0
- data/spec/models/active_record/readme_job.rb +21 -0
- data/spec/models/active_record/simple_new_dsl.rb +17 -0
- data/spec/models/active_record/thief.rb +29 -0
- data/spec/models/active_record/transient.rb +6 -0
- data/spec/models/active_record/with_enum.rb +39 -0
- data/spec/models/active_record/with_false_enum.rb +31 -0
- data/spec/models/active_record/with_true_enum.rb +39 -0
- data/spec/models/active_record/writer.rb +6 -0
- data/spec/models/basic_two_state_machines_example.rb +25 -0
- data/spec/models/callbacks/basic.rb +3 -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.rb +9 -9
- data/spec/models/callbacks/with_args_multiple.rb +61 -0
- data/spec/models/callbacks/{with_state_args.rb → with_state_arg.rb} +1 -1
- data/spec/models/callbacks/with_state_arg_multiple.rb +26 -0
- data/spec/models/complex_example.rb +222 -0
- data/spec/models/conversation.rb +47 -1
- data/spec/models/default_state.rb +12 -0
- data/spec/models/foo.rb +57 -0
- data/spec/models/foo_callback_multiple.rb +45 -0
- data/spec/models/guardian.rb +10 -0
- data/spec/models/guardian_multiple.rb +48 -0
- data/spec/models/initial_state_proc.rb +31 -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 +21 -0
- data/spec/models/mongo_mapper/simple_mongo_mapper.rb +23 -0
- data/spec/models/mongo_mapper/simple_new_dsl_mongo_mapper.rb +25 -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 +14 -1
- data/spec/models/no_initial_state.rb +25 -0
- data/spec/models/parametrised_event.rb +1 -1
- data/spec/models/parametrised_event_multiple.rb +29 -0
- data/spec/models/provided_state.rb +24 -0
- 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/silencer.rb +5 -0
- data/spec/models/simple_example.rb +15 -0
- data/spec/models/simple_multiple_example.rb +30 -0
- data/spec/models/state_machine_with_failed_event.rb +12 -0
- data/spec/models/sub_class.rb +7 -0
- data/spec/models/sub_class_with_more_states.rb +18 -0
- data/spec/models/super_class.rb +46 -0
- data/spec/models/transactor.rb +27 -0
- data/spec/models/valid_state_name.rb +23 -0
- data/spec/models/validator.rb +47 -0
- data/spec/spec_helper.rb +4 -4
- data/spec/unit/api_spec.rb +6 -1
- 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 +3 -2
- data/spec/unit/complex_example_spec.rb +2 -2
- 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_naming_spec.rb +2 -15
- data/spec/unit/event_spec.rb +11 -6
- data/spec/unit/guard_multiple_spec.rb +60 -0
- data/spec/unit/guard_spec.rb +12 -0
- data/spec/unit/initial_state_multiple_spec.rb +15 -0
- data/spec/unit/initial_state_spec.rb +3 -18
- data/spec/unit/inspection_multiple_spec.rb +201 -0
- data/spec/unit/inspection_spec.rb +12 -7
- data/spec/unit/localizer_spec.rb +0 -38
- data/spec/unit/persistence/active_record_persistence_multiple_spec.rb +573 -0
- data/spec/unit/persistence/active_record_persistence_spec.rb +70 -13
- data/spec/unit/persistence/mongo_mapper_persistence_multiple_spec.rb +146 -0
- data/spec/unit/persistence/mongo_mapper_persistence_spec.rb +93 -0
- 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 +20 -23
- data/spec/unit/readme_spec.rb +42 -0
- data/spec/unit/reloading_spec.rb +1 -1
- data/spec/unit/rspec_matcher_spec.rb +79 -0
- data/spec/unit/simple_example_spec.rb +26 -42
- 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/subclassing_spec.rb +10 -10
- data/spec/unit/transition_spec.rb +39 -25
- metadata +152 -23
- data/spec/models/active_record/api.rb +0 -75
- data/spec/models/argument.rb +0 -11
- data/spec/models/auth_machine.rb +0 -88
- data/spec/models/bar.rb +0 -15
- data/spec/models/father.rb +0 -21
- data/spec/models/persistence.rb +0 -164
- data/spec/models/son.rb +0 -3
- data/spec/unit/persistence/mongoid_persistance_spec.rb +0 -152
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 497994639dac4183fdf996cafad384f308784855
|
|
4
|
+
data.tar.gz: 731e388e865ffb526e4cbb3c20443659aab8605a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b3572795abc66917c392e8736afc6d8ea882b12f0fdfcb15cd8046ae0f1977e8dd01b2eeab5d2fcb7e659295c3f45b6aa438aa008c61c8707370be7b27b40e31
|
|
7
|
+
data.tar.gz: 42a4dc5b04c7a13e2a15e745a42b960a80cb4a90f4f62e1eb51c09b4493565314c85ae4aeeb1dd985fc85094706b440fc14ec11dbb7e7275c641e64309b7106d
|
data/.travis.yml
CHANGED
|
@@ -10,7 +10,8 @@ rvm:
|
|
|
10
10
|
- 2.1
|
|
11
11
|
- 2.2
|
|
12
12
|
# - jruby-18mode # JRuby in 1.8 mode
|
|
13
|
-
- jruby-
|
|
13
|
+
- jruby-1.7 # JRuby in 1.9 mode
|
|
14
|
+
- jruby-9.0.3.0
|
|
14
15
|
- rbx-2.2.1
|
|
15
16
|
|
|
16
17
|
services: mongodb
|
|
@@ -18,13 +19,17 @@ services: mongodb
|
|
|
18
19
|
gemfile:
|
|
19
20
|
- gemfiles/rails_3.2.gemfile
|
|
20
21
|
- gemfiles/rails_4.0.gemfile
|
|
22
|
+
- gemfiles/rails_4.0_mongo_mapper.gemfile
|
|
21
23
|
- gemfiles/rails_4.1.gemfile
|
|
24
|
+
- gemfiles/rails_4.1_mongo_mapper.gemfile
|
|
22
25
|
- gemfiles/rails_4.2.gemfile
|
|
26
|
+
- gemfiles/rails_4.2_mongoid_5.gemfile
|
|
27
|
+
- gemfiles/rails_4.2_mongo_mapper.gemfile
|
|
23
28
|
|
|
24
29
|
matrix:
|
|
25
30
|
allow_failures:
|
|
26
31
|
- rvm: rbx-2.2.1
|
|
27
|
-
- rvm: jruby-
|
|
32
|
+
- rvm: jruby-1.7
|
|
28
33
|
exclude:
|
|
29
34
|
- rvm: 1.9.3
|
|
30
35
|
gemfile: gemfiles/rails_4.1.gemfile
|
data/CHANGELOG.md
CHANGED
|
@@ -1,13 +1,54 @@
|
|
|
1
1
|
# CHANGELOG
|
|
2
2
|
|
|
3
|
-
## 4.
|
|
3
|
+
## 4.5.2
|
|
4
4
|
|
|
5
|
+
* fix arity difference between Procs and lambdas (see [issue #293](https://github.com/aasm/aasm/issues/293) for details)
|
|
6
|
+
|
|
7
|
+
## 4.5.1
|
|
8
|
+
|
|
9
|
+
* make sure to use override configuration options if state machine is defined more than once (see [issue #287](https://github.com/aasm/aasm/issues/287) for details)
|
|
10
|
+
|
|
11
|
+
## 4.5.0
|
|
12
|
+
|
|
13
|
+
* add RSpec matchers `have_state`, `allow_event` and `allow_transition_to` (see [issue #147](https://github.com/aasm/aasm/issues/147) for details)
|
|
14
|
+
* add RSpec matcher `transition_from` (see [issue #178](https://github.com/aasm/aasm/issues/178) for details, thanks to [@thomasstephane](https://github.com/thomasstephane))
|
|
15
|
+
|
|
16
|
+
## 4.4.1
|
|
17
|
+
|
|
18
|
+
* add support for rejecting certain events on inspection (see [issue #272](https://github.com/aasm/aasm/issues/272) for details, thanks to [@dubroe](https://github.com/dubroe))
|
|
19
|
+
|
|
20
|
+
## 4.4.0
|
|
21
|
+
|
|
22
|
+
* add support global transation callbacks (see [issue #221](https://github.com/aasm/aasm/issues/221) and [issue #253](https://github.com/aasm/aasm/issues/253) for details)
|
|
23
|
+
* add support (bugfix) for Mongoid >= 5.0 (see [issue #277](https://github.com/aasm/aasm/issues/277) and [issue #278](https://github.com/aasm/aasm/issues/278) for details)
|
|
24
|
+
|
|
25
|
+
## 4.3.0
|
|
26
|
+
|
|
27
|
+
* add support for multiple state machines per class (see [issue #158](https://github.com/aasm/aasm/issues/158) and [issue #240](https://github.com/aasm/aasm/issues/240) for details)
|
|
28
|
+
* special thanks to [@evadne](https://github.com/evadne) for testing this feature, and providing comments and patches (see [issue #245](https://github.com/aasm/aasm/issues/245) for details)
|
|
29
|
+
|
|
30
|
+
## 4.2.0
|
|
31
|
+
|
|
32
|
+
* support turning off and on the configuration option for `no_direct_assignment` (see [issue #223](https://github.com/aasm/aasm/issues/223) for details)
|
|
33
|
+
* event arguments are now passed to `:after_commit` callbacks as well (see [issue #238](https://github.com/aasm/aasm/pull/238), thanks to [@kuinak](https://github.com/kuinak))
|
|
34
|
+
|
|
35
|
+
## 4.1.1
|
|
36
|
+
|
|
37
|
+
* support block notation for `:after_commit` event callbacks (see [issue #224](https://github.com/aasm/aasm/issues/224) for details)
|
|
38
|
+
* event arguments are now passed to state callbacks as well (not only to event callbacks) (see [issue #219](https://github.com/aasm/aasm/issues/219), thanks to [@tobithiel](https://github.com/tobithiel))
|
|
39
|
+
* `AASM::InvalidTransition` now references the current object (with the state machine) and the _AASM_ event name (see [issue #217](https://github.com/aasm/aasm/issues/217), thanks to [@awsmsrc](https://github.com/awsmsrc))
|
|
40
|
+
* bugfix: do not update unloaded state for [Sequel](http://sequel.jeremyevans.net/) (see [issue #218](https://github.com/aasm/aasm/issues/218), thanks to [@godfat](https://github.com/godfat))
|
|
41
|
+
|
|
42
|
+
## 4.1.0
|
|
43
|
+
|
|
44
|
+
* bugfix: initialize the aasm state column after initialization of the _Mongoid_ instance (see [issue #206](https://github.com/aasm/aasm/issues/206), thanks to [@Shwetakale ](https://github.com/Shwetakale ))
|
|
45
|
+
* added support for mongomapper ORM (see [issue #203](https://github.com/aasm/aasm/issues/203), thanks to [@reidmorrison ](https://github.com/reidmorrison ))
|
|
5
46
|
* `aasm_column` has been removed. Use `aasm.attribute_name` instead
|
|
6
47
|
* `aasm_human_event_name` has been removed. Use `aasm.human_event_name` instead
|
|
7
48
|
|
|
8
49
|
## 4.0.8
|
|
9
50
|
|
|
10
|
-
* bugfix:
|
|
51
|
+
* bugfix: may_event_name? should return true or false only (see [issue #200](https://github.com/aasm/aasm/issues/200) for details)
|
|
11
52
|
|
|
12
53
|
## 4.0.7
|
|
13
54
|
|
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Contributor Code of Conduct
|
|
2
|
+
|
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
|
4
|
+
|
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
|
6
|
+
|
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
|
8
|
+
|
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
|
10
|
+
|
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
|
12
|
+
|
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
source "https://rubygems.org"
|
|
2
2
|
|
|
3
3
|
gem "sqlite3", :platforms => :ruby
|
|
4
|
-
gem "coveralls", :platforms => :ruby
|
|
5
4
|
gem 'rubysl', :platforms => :rbx
|
|
6
5
|
gem "jruby-openssl", :platforms => :jruby
|
|
7
6
|
gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
|
|
8
|
-
gem "rails", "~>4.
|
|
9
|
-
gem 'mongoid' if Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version.create('1.9.3')
|
|
7
|
+
gem "rails", "~>4.2"
|
|
8
|
+
gem 'mongoid', '~>4.0' if Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version.create('1.9.3')
|
|
10
9
|
gem 'sequel'
|
|
10
|
+
# Since mongoid V4 requires incompatible bson V2, cannot have mongoid (V4 or greater)
|
|
11
|
+
# and mongo_mapper ( or mongo ) in the same application
|
|
12
|
+
# gem 'mongo_mapper', '~> 0.13'
|
|
13
|
+
# gem 'bson_ext', :platforms => :ruby
|
|
11
14
|
|
|
12
15
|
gemspec
|
data/LICENSE
CHANGED
data/PLANNED_CHANGES.md
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
# Planned changes
|
|
1
|
+
# Planned changes
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
## later
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
* drop support for aasm_column ?
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
|
|
8
|
+
# Currently working on
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
# Changes so far
|
data/README.md
CHANGED
|
@@ -2,15 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
[](http://badge.fury.io/rb/aasm)
|
|
4
4
|
[](https://travis-ci.org/aasm/aasm)
|
|
5
|
+
[](https://gemnasium.com/aasm/aasm)
|
|
5
6
|
[](https://codeclimate.com/github/aasm/aasm)
|
|
6
|
-
[](https://coveralls.io/r/aasm/aasm?branch=master)
|
|
7
7
|
|
|
8
8
|
This package contains AASM, a library for adding finite state machines to Ruby classes.
|
|
9
9
|
|
|
10
10
|
AASM started as the *acts_as_state_machine* plugin but has evolved into a more generic library
|
|
11
11
|
that no longer targets only ActiveRecord models. It currently provides adapters for
|
|
12
|
-
[ActiveRecord](http://api.rubyonrails.org/classes/ActiveRecord/Base.html)
|
|
13
|
-
[Mongoid](http://mongoid.org/), but it can be used for any Ruby class, no matter what
|
|
12
|
+
[ActiveRecord](http://api.rubyonrails.org/classes/ActiveRecord/Base.html),
|
|
13
|
+
[Mongoid](http://mongoid.org/), and [Mongomapper](http://mongomapper.com/) but it can be used for any Ruby class, no matter what
|
|
14
14
|
parent class it has (if any).
|
|
15
15
|
|
|
16
16
|
## Upgrade from version 3 to 4
|
|
@@ -98,12 +98,14 @@ class Job
|
|
|
98
98
|
state :sleeping, :initial => true, :before_enter => :do_something
|
|
99
99
|
state :running
|
|
100
100
|
|
|
101
|
+
after_all_transitions :log_status_change
|
|
102
|
+
|
|
101
103
|
event :run, :after => :notify_somebody do
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
log('Preparing to run')
|
|
105
|
-
end
|
|
104
|
+
before do
|
|
105
|
+
log('Preparing to run')
|
|
106
106
|
end
|
|
107
|
+
|
|
108
|
+
transitions :from => :sleeping, :to => :running, :after => Proc.new {|*args| set_process(*args) }
|
|
107
109
|
end
|
|
108
110
|
|
|
109
111
|
event :sleep do
|
|
@@ -117,6 +119,10 @@ class Job
|
|
|
117
119
|
end
|
|
118
120
|
end
|
|
119
121
|
|
|
122
|
+
def log_status_change
|
|
123
|
+
puts "changing from #{aasm.from_state} to #{aasm.to_state} (event: #{aasm.current_event})"
|
|
124
|
+
end
|
|
125
|
+
|
|
120
126
|
def set_process(name)
|
|
121
127
|
...
|
|
122
128
|
end
|
|
@@ -145,6 +151,7 @@ begin
|
|
|
145
151
|
transition guards
|
|
146
152
|
old_state before_exit
|
|
147
153
|
old_state exit
|
|
154
|
+
after_all_transitions
|
|
148
155
|
transition after
|
|
149
156
|
new_state before_enter
|
|
150
157
|
new_state enter
|
|
@@ -172,8 +179,9 @@ Note that when passing arguments to a state transition, the first argument must
|
|
|
172
179
|
In case of an error during the event processing the error is rescued and passed to `:error`
|
|
173
180
|
callback, which can handle it or re-raise it for further propagation.
|
|
174
181
|
|
|
175
|
-
During the transition's `:after` callback (and reliably only then
|
|
176
|
-
|
|
182
|
+
During the transition's `:after` callback (and reliably only then, or in the global
|
|
183
|
+
`after_all_transitions` callback) you can access the originating state (the from-state)
|
|
184
|
+
and the target state (the to state), like this:
|
|
177
185
|
|
|
178
186
|
```ruby
|
|
179
187
|
def set_process(name)
|
|
@@ -312,9 +320,79 @@ job.stage1_completed
|
|
|
312
320
|
job.aasm.current_state # stage3
|
|
313
321
|
```
|
|
314
322
|
|
|
323
|
+
|
|
324
|
+
### Multiple state machines per class
|
|
325
|
+
|
|
326
|
+
Multiple state machines per class are supported. Be aware though that _AASM_ has been
|
|
327
|
+
built with one state machine per class in mind. Nonetheless, here's how to do it:
|
|
328
|
+
|
|
329
|
+
```ruby
|
|
330
|
+
class SimpleMultipleExample
|
|
331
|
+
include AASM
|
|
332
|
+
aasm(:move) do
|
|
333
|
+
state :standing, :initial => true
|
|
334
|
+
state :walking
|
|
335
|
+
state :running
|
|
336
|
+
|
|
337
|
+
event :walk do
|
|
338
|
+
transitions :from => :standing, :to => :walking
|
|
339
|
+
end
|
|
340
|
+
event :run do
|
|
341
|
+
transitions :from => [:standing, :walking], :to => :running
|
|
342
|
+
end
|
|
343
|
+
event :hold do
|
|
344
|
+
transitions :from => [:walking, :running], :to => :standing
|
|
345
|
+
end
|
|
346
|
+
end
|
|
347
|
+
|
|
348
|
+
aasm(:work) do
|
|
349
|
+
state :sleeping, :initial => true
|
|
350
|
+
state :processing
|
|
351
|
+
|
|
352
|
+
event :start do
|
|
353
|
+
transitions :from => :sleeping, :to => :processing
|
|
354
|
+
end
|
|
355
|
+
event :stop do
|
|
356
|
+
transitions :from => :processing, :to => :sleeping
|
|
357
|
+
end
|
|
358
|
+
end
|
|
359
|
+
end
|
|
360
|
+
|
|
361
|
+
simple = SimpleMultipleExample.new
|
|
362
|
+
|
|
363
|
+
simple.aasm(:move).current_state
|
|
364
|
+
# => :standing
|
|
365
|
+
simple.aasm(:work).current
|
|
366
|
+
# => :sleeping
|
|
367
|
+
|
|
368
|
+
simple.start
|
|
369
|
+
simple.aasm(:move).current_state
|
|
370
|
+
# => :standing
|
|
371
|
+
simple.aasm(:work).current
|
|
372
|
+
# => :processing
|
|
373
|
+
|
|
374
|
+
```
|
|
375
|
+
|
|
376
|
+
_AASM_ doesn't prohibit to define the same event in more than one state machine. The
|
|
377
|
+
latest definition "wins" and overrides previous definitions. Nonetheless, a warning is issued:
|
|
378
|
+
`SimpleMultipleExample: The aasm event name run is already used!`.
|
|
379
|
+
|
|
380
|
+
All _AASM_ class- and instance-level `aasm` methods accept a state machine selector.
|
|
381
|
+
So, for example, to use inspection on a class level, you have to use
|
|
382
|
+
|
|
383
|
+
```ruby
|
|
384
|
+
SimpleMultipleExample.aasm(:work).states
|
|
385
|
+
# => [:standing, :walking, :running]
|
|
386
|
+
```
|
|
387
|
+
|
|
388
|
+
*Final note*: Support for multiple state machines per class is a pretty new feature
|
|
389
|
+
(since version `4.3`), so please bear with us in case it doesn't work as expected.
|
|
390
|
+
|
|
391
|
+
|
|
392
|
+
|
|
315
393
|
### ActiveRecord
|
|
316
394
|
|
|
317
|
-
AASM comes with support for ActiveRecord and allows
|
|
395
|
+
AASM comes with support for ActiveRecord and allows automatic persisting of the object's
|
|
318
396
|
state in the database.
|
|
319
397
|
|
|
320
398
|
```ruby
|
|
@@ -347,7 +425,7 @@ job.run! # saved
|
|
|
347
425
|
|
|
348
426
|
Saving includes running all validations on the `Job` class. If you want make sure
|
|
349
427
|
the state gets saved without running validations (and thereby maybe persisting an
|
|
350
|
-
invalid object state), simply tell AASM to skip the validations. Be aware
|
|
428
|
+
invalid object state), simply tell AASM to skip the validations. Be aware that
|
|
351
429
|
when skipping validations, only the state column will be updated in the database
|
|
352
430
|
(just like ActiveRecord `change_column` is working).
|
|
353
431
|
|
|
@@ -435,7 +513,7 @@ to ```false```.
|
|
|
435
513
|
|
|
436
514
|
### Sequel
|
|
437
515
|
|
|
438
|
-
AASM also supports [Sequel](http://sequel.jeremyevans.net/) besides _ActiveRecord_ and
|
|
516
|
+
AASM also supports [Sequel](http://sequel.jeremyevans.net/) besides _ActiveRecord_, _Mongoid_, and _MongoMapper_.
|
|
439
517
|
|
|
440
518
|
```ruby
|
|
441
519
|
class Job < Sequel::Model
|
|
@@ -466,6 +544,23 @@ class Job
|
|
|
466
544
|
end
|
|
467
545
|
```
|
|
468
546
|
|
|
547
|
+
### MongoMapper
|
|
548
|
+
|
|
549
|
+
AASM also supports persistence to Mongodb if you're using MongoMapper. Make sure
|
|
550
|
+
to include MongoMapper::Document before you include AASM.
|
|
551
|
+
|
|
552
|
+
```ruby
|
|
553
|
+
class Job
|
|
554
|
+
include MongoMapper::Document
|
|
555
|
+
include AASM
|
|
556
|
+
|
|
557
|
+
key :aasm_state, Symbol
|
|
558
|
+
aasm do
|
|
559
|
+
...
|
|
560
|
+
end
|
|
561
|
+
end
|
|
562
|
+
```
|
|
563
|
+
|
|
469
564
|
### Automatic Scopes
|
|
470
565
|
|
|
471
566
|
AASM will automatically create scope methods for each state in the model.
|
|
@@ -520,7 +615,7 @@ callback or the state update fails, all changes to any database record are rolle
|
|
|
520
615
|
Mongodb does not support transactions.
|
|
521
616
|
|
|
522
617
|
If you want to make sure a depending action happens only after the transaction is committed,
|
|
523
|
-
use the `after_commit` callback, like this:
|
|
618
|
+
use the `after_commit` callback along with the auto-save (bang) methods, like this:
|
|
524
619
|
|
|
525
620
|
```ruby
|
|
526
621
|
class Job < ActiveRecord::Base
|
|
@@ -539,6 +634,18 @@ class Job < ActiveRecord::Base
|
|
|
539
634
|
...
|
|
540
635
|
end
|
|
541
636
|
end
|
|
637
|
+
|
|
638
|
+
job = Job.where(state: 'sleeping').first!
|
|
639
|
+
job.run! # Saves the model and triggers the after_commit callback
|
|
640
|
+
```
|
|
641
|
+
|
|
642
|
+
Note that the following will not run the `after_commit` callbacks because
|
|
643
|
+
the auto-save method is not used:
|
|
644
|
+
|
|
645
|
+
```ruby
|
|
646
|
+
job = Job.where(state: 'sleeping').first!
|
|
647
|
+
job.run
|
|
648
|
+
job.save! #notify_about_running_job is not run
|
|
542
649
|
```
|
|
543
650
|
|
|
544
651
|
If you want to encapsulate state changes within an own transaction, the behavior
|
|
@@ -617,9 +724,50 @@ job.aasm.states(:permitted => true).map(&:name)
|
|
|
617
724
|
# show all possible (triggerable) events (allowed by transitions)
|
|
618
725
|
job.aasm.events.map(&:name)
|
|
619
726
|
=> [:sleep]
|
|
727
|
+
job.aasm.events(:reject => :sleep).map(&:name)
|
|
728
|
+
=> []
|
|
729
|
+
|
|
730
|
+
# list states for select
|
|
731
|
+
Job.aasm.states_for_select
|
|
732
|
+
=> [["Sleeping", "sleeping"], ["Running", "running"], ["Cleaning", "cleaning"]]
|
|
620
733
|
```
|
|
621
734
|
|
|
622
735
|
|
|
736
|
+
### Testing
|
|
737
|
+
|
|
738
|
+
AASM provides some matchers for [RSpec](http://rspec.info): `transition_from`, `have_state`, `allow_event` and `allow_transition_to`. Add `require 'aasm/rspec'` to your `spec_helper.rb` file and use them like this
|
|
739
|
+
|
|
740
|
+
```ruby
|
|
741
|
+
# classes with only the default state machine
|
|
742
|
+
job = Job.new
|
|
743
|
+
expect(job).to transition_from(:sleeping).to(:running).on_event(:run)
|
|
744
|
+
expect(job).not_to transition_from(:sleeping).to(:cleaning).on_event(:run)
|
|
745
|
+
expect(job).to have_state(:sleeping)
|
|
746
|
+
expect(job).not_to have_state(:running)
|
|
747
|
+
expect(job).to allow_event :run
|
|
748
|
+
expect(job).to_not allow_event :clean
|
|
749
|
+
expect(job).to allow_transition_to(:running)
|
|
750
|
+
expect(job).to_not allow_transition_to(:cleaning)
|
|
751
|
+
|
|
752
|
+
# classes with multiple state machine
|
|
753
|
+
multiple = SimpleMultipleExample.new
|
|
754
|
+
expect(multiple).to transition_from(:standing).to(:walking).on_event(:walk).on(:move)
|
|
755
|
+
expect(multiple).to_not transition_from(:standing).to(:running).on_event(:walk).on(:move)
|
|
756
|
+
expect(multiple).to have_state(:standing).on(:move)
|
|
757
|
+
expect(multiple).not_to have_state(:walking).on(:move)
|
|
758
|
+
expect(multiple).to allow_event(:walk).on(:move)
|
|
759
|
+
expect(multiple).to_not allow_event(:hold).on(:move)
|
|
760
|
+
expect(multiple).to allow_transition_to(:walking).on(:move)
|
|
761
|
+
expect(multiple).to_not allow_transition_to(:running).on(:move)
|
|
762
|
+
expect(multiple).to transition_from(:sleeping).to(:processing).on_event(:start).on(:work)
|
|
763
|
+
expect(multiple).to_not transition_from(:sleeping).to(:sleeping).on_event(:start).on(:work)
|
|
764
|
+
expect(multiple).to have_state(:sleeping).on(:work)
|
|
765
|
+
expect(multiple).not_to have_state(:processing).on(:work)
|
|
766
|
+
expect(multiple).to allow_event(:start).on(:move)
|
|
767
|
+
expect(multiple).to_not allow_event(:stop).on(:move)
|
|
768
|
+
expect(multiple).to allow_transition_to(:processing).on(:move)
|
|
769
|
+
expect(multiple).to_not allow_transition_to(:sleeping).on(:move)
|
|
770
|
+
```
|
|
623
771
|
|
|
624
772
|
## <a id="installation">Installation ##
|
|
625
773
|
|
|
@@ -662,6 +810,15 @@ Feel free to
|
|
|
662
810
|
* [Thorsten Böttger](http://github.com/alto) (since 2011)
|
|
663
811
|
|
|
664
812
|
|
|
813
|
+
## Contributing ##
|
|
814
|
+
|
|
815
|
+
1. Read the [Contributor Code of Conduct](https://github.com/aasm/aasm/blob/master/CODE_OF_CONDUCT.md)
|
|
816
|
+
2. Fork it
|
|
817
|
+
3. Create your feature branch (git checkout -b my-new-feature)
|
|
818
|
+
4. Commit your changes (git commit -am 'Added some feature')
|
|
819
|
+
5. Push to the branch (git push origin my-new-feature)
|
|
820
|
+
6. Create new Pull Request
|
|
821
|
+
|
|
665
822
|
## Warranty ##
|
|
666
823
|
|
|
667
824
|
This software is provided "as is" and without any express or
|
|
@@ -671,7 +828,7 @@ purpose.
|
|
|
671
828
|
|
|
672
829
|
## License ##
|
|
673
830
|
|
|
674
|
-
Copyright (c) 2006-
|
|
831
|
+
Copyright (c) 2006-2015 Scott Barron
|
|
675
832
|
|
|
676
833
|
Permission is hereby granted, free of charge, to any person obtaining
|
|
677
834
|
a copy of this software and associated documentation files (the
|
data/aasm.gemspec
CHANGED
|
@@ -18,16 +18,12 @@ Gem::Specification.new do |s|
|
|
|
18
18
|
|
|
19
19
|
s.add_development_dependency 'rake'
|
|
20
20
|
s.add_development_dependency 'sdoc'
|
|
21
|
-
s.add_development_dependency 'rspec'
|
|
21
|
+
s.add_development_dependency 'rspec', ">= 3"
|
|
22
22
|
|
|
23
23
|
# debugging
|
|
24
24
|
# s.add_development_dependency 'debugger'
|
|
25
25
|
s.add_development_dependency 'pry'
|
|
26
26
|
|
|
27
|
-
# test coverage
|
|
28
|
-
# s.add_development_dependency 'mime-types', '~> 1.25' # needed by coveralls (>= 2.0 needs Ruby >=1.9.2)
|
|
29
|
-
# s.add_development_dependency 'coveralls'
|
|
30
|
-
|
|
31
27
|
s.files = `git ls-files`.split("\n")
|
|
32
28
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
|
33
29
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
data/gemfiles/rails_3.2.gemfile
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
source "https://rubygems.org"
|
|
2
2
|
|
|
3
3
|
gem "sqlite3", :platforms => :ruby
|
|
4
|
-
gem "coveralls"
|
|
5
4
|
gem 'rubysl', :platforms => :rbx
|
|
6
5
|
gem 'rubinius-developer_tools', :platforms => :rbx
|
|
7
6
|
gem "jruby-openssl", :platforms => :jruby
|
|
8
7
|
gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
|
|
9
8
|
gem "rails", "3.2.21"
|
|
10
|
-
gem 'mongoid' if Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version.create('1.9.3')
|
|
9
|
+
gem 'mongoid', '~>3.1' if Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version.create('1.9.3')
|
|
11
10
|
gem 'sequel'
|
|
11
|
+
gem 'mongo_mapper', '~>0.13'
|
|
12
|
+
gem 'bson_ext', :platforms => :ruby
|
|
12
13
|
|
|
13
14
|
gemspec :path => "../"
|
data/gemfiles/rails_4.0.gemfile
CHANGED
|
@@ -1,16 +1,12 @@
|
|
|
1
1
|
source "https://rubygems.org"
|
|
2
2
|
|
|
3
3
|
gem "sqlite3", :platforms => :ruby
|
|
4
|
-
gem "coveralls"
|
|
5
4
|
gem 'rubysl', :platforms => :rbx
|
|
6
5
|
gem 'rubinius-developer_tools', :platforms => :rbx
|
|
7
6
|
gem "jruby-openssl", :platforms => :jruby
|
|
8
7
|
gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
|
|
9
|
-
gem "rails", "4.0.
|
|
10
|
-
|
|
11
|
-
# mongoid is not yet compatible with Rails >= 4
|
|
12
|
-
# gem 'mongoid' if Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version.create('1.9.3')
|
|
13
|
-
|
|
8
|
+
gem "rails", "4.0.13"
|
|
9
|
+
gem 'mongoid', '~>4.0' if Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version.create('1.9.3')
|
|
14
10
|
gem 'sequel'
|
|
15
11
|
|
|
16
12
|
gemspec :path => "../"
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
source "https://rubygems.org"
|
|
2
|
+
|
|
3
|
+
gem "sqlite3", :platforms => :ruby
|
|
4
|
+
gem "coveralls"
|
|
5
|
+
gem 'rubysl', :platforms => :rbx
|
|
6
|
+
gem 'rubinius-developer_tools', :platforms => :rbx
|
|
7
|
+
gem "jruby-openssl", :platforms => :jruby
|
|
8
|
+
gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
|
|
9
|
+
gem "rails", "4.0.13"
|
|
10
|
+
gem 'sequel'
|
|
11
|
+
gem 'mongo_mapper', '~>0.13'
|
|
12
|
+
gem 'bson_ext', :platforms => :ruby
|
|
13
|
+
|
|
14
|
+
gemspec :path => "../"
|
data/gemfiles/rails_4.1.gemfile
CHANGED
|
@@ -1,16 +1,12 @@
|
|
|
1
1
|
source "https://rubygems.org"
|
|
2
2
|
|
|
3
3
|
gem "sqlite3", :platforms => :ruby
|
|
4
|
-
gem "coveralls"
|
|
5
4
|
gem 'rubysl', :platforms => :rbx
|
|
6
5
|
gem 'rubinius-developer_tools', :platforms => :rbx
|
|
7
6
|
gem "jruby-openssl", :platforms => :jruby
|
|
8
7
|
gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
|
|
9
|
-
gem "rails", "4.1.
|
|
10
|
-
|
|
11
|
-
# mongoid is not yet compatible with Rails >= 4
|
|
12
|
-
# gem 'mongoid' if Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version.create('1.9.3')
|
|
13
|
-
|
|
8
|
+
gem "rails", "4.1.9"
|
|
9
|
+
gem 'mongoid', '~>4.0' if Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version.create('1.9.3')
|
|
14
10
|
gem 'sequel'
|
|
15
11
|
|
|
16
12
|
gemspec :path => "../"
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
source "https://rubygems.org"
|
|
2
|
+
|
|
3
|
+
gem "sqlite3", :platforms => :ruby
|
|
4
|
+
gem "coveralls"
|
|
5
|
+
gem 'rubysl', :platforms => :rbx
|
|
6
|
+
gem 'rubinius-developer_tools', :platforms => :rbx
|
|
7
|
+
gem "jruby-openssl", :platforms => :jruby
|
|
8
|
+
gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
|
|
9
|
+
gem "rails", "4.1.9"
|
|
10
|
+
gem 'sequel'
|
|
11
|
+
gem 'mongo_mapper', '~> 0.13'
|
|
12
|
+
gem 'bson_ext', :platforms => :ruby
|
|
13
|
+
|
|
14
|
+
gemspec :path => "../"
|
data/gemfiles/rails_4.2.gemfile
CHANGED
|
@@ -1,16 +1,12 @@
|
|
|
1
1
|
source "https://rubygems.org"
|
|
2
2
|
|
|
3
3
|
gem "sqlite3", :platforms => :ruby
|
|
4
|
-
gem "coveralls"
|
|
5
4
|
gem 'rubysl', :platforms => :rbx
|
|
6
5
|
gem 'rubinius-developer_tools', :platforms => :rbx
|
|
7
6
|
gem "jruby-openssl", :platforms => :jruby
|
|
8
7
|
gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
|
|
9
8
|
gem "rails", "4.2.0"
|
|
10
|
-
|
|
11
|
-
# mongoid is not yet compatible with Rails >= 4
|
|
12
|
-
# gem 'mongoid' if Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version.create('1.9.3')
|
|
13
|
-
|
|
9
|
+
gem 'mongoid', '~>4.0' if Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version.create('1.9.3')
|
|
14
10
|
gem 'sequel'
|
|
15
11
|
|
|
16
12
|
gemspec :path => "../"
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
source "https://rubygems.org"
|
|
2
|
+
|
|
3
|
+
gem "sqlite3", :platforms => :ruby
|
|
4
|
+
gem "coveralls"
|
|
5
|
+
gem 'rubysl', :platforms => :rbx
|
|
6
|
+
gem 'rubinius-developer_tools', :platforms => :rbx
|
|
7
|
+
gem "jruby-openssl", :platforms => :jruby
|
|
8
|
+
gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
|
|
9
|
+
gem "rails", "4.2.0"
|
|
10
|
+
gem 'sequel'
|
|
11
|
+
gem 'mongo_mapper'
|
|
12
|
+
gem 'bson_ext', :platforms => :ruby
|
|
13
|
+
|
|
14
|
+
gemspec :path => "../"
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
source "https://rubygems.org"
|
|
2
|
+
|
|
3
|
+
gem "sqlite3", :platforms => :ruby
|
|
4
|
+
gem 'rubysl', :platforms => :rbx
|
|
5
|
+
gem 'rubinius-developer_tools', :platforms => :rbx
|
|
6
|
+
gem "jruby-openssl", :platforms => :jruby
|
|
7
|
+
gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
|
|
8
|
+
gem "rails", "4.2.0"
|
|
9
|
+
gem 'mongoid', '~>5.0' if Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version.create('1.9.3')
|
|
10
|
+
gem 'sequel'
|
|
11
|
+
|
|
12
|
+
gemspec :path => "../"
|