aasm 4.0.3 → 4.1.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.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +10 -11
  3. data/CHANGELOG.md +28 -1
  4. data/Gemfile +6 -2
  5. data/README.md +31 -12
  6. data/README_FROM_VERSION_3_TO_4.md +1 -1
  7. data/aasm.gemspec +9 -10
  8. data/gemfiles/rails_3.2.gemfile +3 -1
  9. data/gemfiles/rails_4.0.gemfile +2 -5
  10. data/gemfiles/rails_4.0_bson1.gemfile +14 -0
  11. data/gemfiles/rails_4.1.gemfile +2 -5
  12. data/gemfiles/rails_4.1_bson1.gemfile +14 -0
  13. data/gemfiles/rails_4.2.gemfile +13 -0
  14. data/gemfiles/rails_4.2_bson1.gemfile +14 -0
  15. data/lib/aasm/aasm.rb +1 -19
  16. data/lib/aasm/base.rb +8 -8
  17. data/lib/aasm/{event.rb → core/event.rb} +13 -5
  18. data/lib/aasm/{state.rb → core/state.rb} +1 -1
  19. data/lib/aasm/{transition.rb → core/transition.rb} +1 -3
  20. data/lib/aasm/instance_base.rb +1 -1
  21. data/lib/aasm/persistence/active_record_persistence.rb +14 -13
  22. data/lib/aasm/persistence/base.rb +4 -1
  23. data/lib/aasm/persistence/mongo_mapper_persistence.rb +174 -0
  24. data/lib/aasm/persistence/mongoid_persistence.rb +5 -7
  25. data/lib/aasm/persistence/plain_persistence.rb +24 -0
  26. data/lib/aasm/persistence/sequel_persistence.rb +2 -0
  27. data/lib/aasm/persistence.rb +19 -11
  28. data/lib/aasm/state_machine.rb +1 -1
  29. data/lib/aasm/version.rb +1 -1
  30. data/lib/aasm.rb +13 -15
  31. data/spec/database.rb +5 -1
  32. data/spec/models/callbacks/basic.rb +75 -0
  33. data/spec/models/callbacks/guard_within_block.rb +66 -0
  34. data/spec/models/callbacks/private_method.rb +44 -0
  35. data/spec/models/callbacks/with_args.rb +61 -0
  36. data/spec/models/callbacks/with_state_args.rb +26 -0
  37. data/spec/models/mongo_mapper/no_scope_mongo_mapper.rb +10 -0
  38. data/spec/models/mongo_mapper/simple_mongo_mapper.rb +11 -0
  39. data/spec/models/mongo_mapper/simple_new_dsl_mongo_mapper.rb +12 -0
  40. data/spec/models/mongoid/simple_new_dsl_mongoid.rb +1 -1
  41. data/spec/models/persistence.rb +79 -1
  42. data/spec/spec_helper.rb +2 -1
  43. data/spec/unit/callbacks_spec.rb +101 -62
  44. data/spec/unit/complex_example_spec.rb +9 -9
  45. data/spec/unit/event_spec.rb +12 -12
  46. data/spec/unit/inspection_spec.rb +2 -2
  47. data/spec/unit/memory_leak_spec.rb +12 -12
  48. data/spec/unit/persistence/active_record_persistence_spec.rb +58 -72
  49. data/spec/unit/persistence/mongo_mapper_persistance_spec.rb +135 -0
  50. data/spec/unit/persistence/mongoid_persistance_spec.rb +4 -10
  51. data/spec/unit/persistence/sequel_persistence_spec.rb +2 -1
  52. data/spec/unit/state_spec.rb +2 -2
  53. data/spec/unit/subclassing_spec.rb +1 -1
  54. data/spec/unit/transition_spec.rb +33 -33
  55. metadata +32 -40
  56. data/spec/models/callback_new_dsl.rb +0 -129
  57. data/spec/models/guard_within_block.rb +0 -64
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8d198d23fad17dc052b4135e5ef02470edd8e146
4
- data.tar.gz: 85019079c1b7cfb236cf529b833561ba35030d51
3
+ metadata.gz: a07996d6cd3f0270a6ec9d010a904cebd462b6a7
4
+ data.tar.gz: 57e8efc5d284371e23c50d41771beb3172787bdd
5
5
  SHA512:
6
- metadata.gz: c1a3a8f5df97b012148fa50b79150d218f0dad130a4014e68502a2982b98ab3a6b79148832dac285321901366ab5ec976af11a5f431966ac528d60d5c62dcc5c
7
- data.tar.gz: 9e09e2ec97235c8761fd34a4c884cb6d9579e9960acf02815f472e0068586b8c4d2ff50b6d08d71f6571f31d81aa9cf345b94784d7cde1a066b1ddf0f44c466b
6
+ metadata.gz: 0e1f53507bcbcf9deecce7193a5fed9f37ae5fbc91b6325f127820ead35113e19bea7dba6f87af9482affac31462b05e1b6c89a87ef61a47ad93d95c07f19179
7
+ data.tar.gz: 9da9eea4b58047190c441dc42c120fed864c318828340cc61505601f70179736d9cc70a0f60166bae13c6fc6aa3f3c8c4569047915a0af72ab152f30ce5422fd
data/.travis.yml CHANGED
@@ -1,13 +1,16 @@
1
+ sudo: false
1
2
  language: ruby
3
+ cache: bundler
2
4
 
3
5
  rvm:
4
6
  # - 1.8.7
5
- - 1.9.2
7
+ # - 1.9.2
6
8
  - 1.9.3
7
9
  - 2.0.0
8
10
  - 2.1
11
+ - 2.2
9
12
  # - jruby-18mode # JRuby in 1.8 mode
10
- - jruby-19mode # JRuby in 1.9 mode
13
+ - jruby-1.7 # JRuby in 1.9 mode
11
14
  - rbx-2.2.1
12
15
 
13
16
  services: mongodb
@@ -15,21 +18,17 @@ services: mongodb
15
18
  gemfile:
16
19
  - gemfiles/rails_3.2.gemfile
17
20
  - gemfiles/rails_4.0.gemfile
21
+ - gemfiles/rails_4.0_bson1.gemfile
18
22
  - gemfiles/rails_4.1.gemfile
23
+ - gemfiles/rails_4.1_bson1.gemfile
24
+ - gemfiles/rails_4.2.gemfile
25
+ - gemfiles/rails_4.2_bson1.gemfile
19
26
 
20
27
  matrix:
21
28
  allow_failures:
22
29
  - rvm: rbx-2.2.1
23
- - rvm: jruby-19mode
30
+ - rvm: jruby-1.7
24
31
  exclude:
25
- - rvm: 1.8.7
26
- gemfile: gemfiles/rails_4.0.gemfile
27
- - rvm: 1.8.7
28
- gemfile: gemfiles/rails_4.1.gemfile
29
- - rvm: 1.9.2
30
- gemfile: gemfiles/rails_4.0.gemfile
31
- - rvm: 1.9.2
32
- gemfile: gemfiles/rails_4.1.gemfile
33
32
  - rvm: 1.9.3
34
33
  gemfile: gemfiles/rails_4.1.gemfile
35
34
  - rvm: jruby-19mode
data/CHANGELOG.md CHANGED
@@ -1,10 +1,37 @@
1
1
  # CHANGELOG
2
2
 
3
- ## 4.1.0 (not yet released)
3
+ ## 4.2.0 (not yet released)
4
4
 
5
+ ## 4.1.0
6
+
7
+ * 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 ))
8
+ * added support for mongomapper ORM (see [issue #203](https://github.com/aasm/aasm/issues/203), thanks to [@reidmorrison ](https://github.com/reidmorrison ))
5
9
  * `aasm_column` has been removed. Use `aasm.attribute_name` instead
6
10
  * `aasm_human_event_name` has been removed. Use `aasm.human_event_name` instead
7
11
 
12
+ ## 4.0.8
13
+
14
+ * bugfix: may_event_name? should return true or false only (see [issue #200](https://github.com/aasm/aasm/issues/200) for details)
15
+
16
+ ## 4.0.7
17
+
18
+ * bugfix: take private methods into account when checking for callbacks (see [issue #197](https://github.com/aasm/aasm/issues/197) for details)
19
+
20
+ ## 4.0.6
21
+
22
+ * bugfix: `false` is treated as uninitialised state (same as `nil`) (see [issue #195](https://github.com/aasm/aasm/issues/195) for details)
23
+ * bugfix: an event's `:error` callback now retrieves all arguments passed to the event (see [issue #196](https://github.com/aasm/aasm/issues/196) for details)
24
+
25
+ ## 4.0.5
26
+
27
+ * bugfix: initialize the aasm state column after initialization of the _ActiveRecord_ instance only if the attribute has been loaded (see [issue #193](https://github.com/aasm/aasm/issues/193) for details)
28
+
29
+ ## 4.0.4
30
+
31
+ * corrected callback order in README
32
+ * bugfix: initialize the aasm state column after initialization of the _ActiveRecord_ instance (see [issue #191](https://github.com/aasm/aasm/issues/191) for details)
33
+ * bugfix: avoid Rails autoloading conflicts (see [issue #137](https://github.com/aasm/aasm/issues/137) and [issue #139](https://github.com/aasm/aasm/issues/139) for details)
34
+
8
35
  ## 4.0.3
9
36
 
10
37
  * bugfix: fire guards only once per transition, part 2 (see [issue #187](https://github.com/aasm/aasm/issues/187) for details)
data/Gemfile CHANGED
@@ -1,12 +1,16 @@
1
1
  source "https://rubygems.org"
2
2
 
3
3
  gem "sqlite3", :platforms => :ruby
4
- gem "coveralls", :platforms => :ruby
4
+ gem "coveralls"
5
5
  gem 'rubysl', :platforms => :rbx
6
6
  gem "jruby-openssl", :platforms => :jruby
7
7
  gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
8
8
  gem "rails", "~>4.1"
9
- gem 'mongoid' if Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version.create('1.9.3')
9
+ gem 'mongoid', '~>4.0' if Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version.create('1.9.3')
10
10
  gem 'sequel'
11
+ # Since mongoid V4 requires incompatible bson V2, cannot have mongoid (V4 or greater)
12
+ # and mongo_mapper ( or mongo ) in the same application
13
+ #gem 'mongo_mapper', '~> 0.13'
14
+ #gem 'bson_ext', :platforms => :ruby
11
15
 
12
16
  gemspec
data/README.md CHANGED
@@ -1,16 +1,16 @@
1
1
  # AASM - Ruby state machines
2
2
 
3
- <a href="http://badge.fury.io/rb/aasm"><img src="https://badge.fury.io/rb/aasm@2x.png" alt="Gem Version" height="18"></a>
3
+ [![Gem Version](https://badge.fury.io/rb/aasm.svg)](http://badge.fury.io/rb/aasm)
4
4
  [![Build Status](https://travis-ci.org/aasm/aasm.svg?branch=master)](https://travis-ci.org/aasm/aasm)
5
- [![Code Climate](https://codeclimate.com/github/aasm/aasm.png)](https://codeclimate.com/github/aasm/aasm)
6
- [![Coverage Status](https://coveralls.io/repos/aasm/aasm/badge.png?branch=master)](https://coveralls.io/r/aasm/aasm)
5
+ [![Code Climate](https://codeclimate.com/github/aasm/aasm/badges/gpa.svg)](https://codeclimate.com/github/aasm/aasm)
6
+ [![Coverage Status](https://img.shields.io/coveralls/aasm/aasm.svg)](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) and
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
@@ -145,11 +145,11 @@ begin
145
145
  transition guards
146
146
  old_state before_exit
147
147
  old_state exit
148
+ transition after
148
149
  new_state before_enter
149
150
  new_state enter
150
- ...update state...
151
- transition after
152
- event success # if persist successful
151
+ ...update state...
152
+ event success # if persist successful
153
153
  old_state after_exit
154
154
  new_state after_enter
155
155
  event after
@@ -172,7 +172,7 @@ Note that when passing arguments to a state transition, the first argument must
172
172
  In case of an error during the event processing the error is rescued and passed to `:error`
173
173
  callback, which can handle it or re-raise it for further propagation.
174
174
 
175
- During the `:on_transition` callback (and reliably only then) you can access the
175
+ During the transition's `:after` callback (and reliably only then) you can access the
176
176
  originating state (the from-state) and the target state (the to state), like this:
177
177
 
178
178
  ```ruby
@@ -347,7 +347,9 @@ job.run! # saved
347
347
 
348
348
  Saving includes running all validations on the `Job` class. If you want make sure
349
349
  the state gets saved without running validations (and thereby maybe persisting an
350
- invalid object state), simply tell AASM to skip the validations:
350
+ invalid object state), simply tell AASM to skip the validations. Be aware, that
351
+ when skipping validations, only the state column will be updated in the database
352
+ (just like ActiveRecord `change_column` is working).
351
353
 
352
354
  ```ruby
353
355
  class Job < ActiveRecord::Base
@@ -433,7 +435,7 @@ to ```false```.
433
435
 
434
436
  ### Sequel
435
437
 
436
- AASM also supports [Sequel](http://sequel.jeremyevans.net/) besides _ActiveRecord_ and _Mongoid_.
438
+ AASM also supports [Sequel](http://sequel.jeremyevans.net/) besides _ActiveRecord_, _Mongoid_, and _MongoMapper_.
437
439
 
438
440
  ```ruby
439
441
  class Job < Sequel::Model
@@ -464,6 +466,23 @@ class Job
464
466
  end
465
467
  ```
466
468
 
469
+ ### MongoMapper
470
+
471
+ AASM also supports persistence to Mongodb if you're using MongoMapper. Make sure
472
+ to include MongoMapper::Document before you include AASM.
473
+
474
+ ```ruby
475
+ class Job
476
+ include MongoMapper::Document
477
+ include AASM
478
+
479
+ key :aasm_state, Symbol
480
+ aasm do
481
+ ...
482
+ end
483
+ end
484
+ ```
485
+
467
486
  ### Automatic Scopes
468
487
 
469
488
  AASM will automatically create scope methods for each state in the model.
@@ -669,7 +688,7 @@ purpose.
669
688
 
670
689
  ## License ##
671
690
 
672
- Copyright (c) 2006-2014 Scott Barron
691
+ Copyright (c) 2006-2015 Scott Barron
673
692
 
674
693
  Permission is hereby granted, free of charge, to any person obtaining
675
694
  a copy of this software and associated documentation files (the
@@ -139,7 +139,7 @@ to
139
139
  ```ruby
140
140
  job = Job.new
141
141
 
142
- job.aasm.events(:permitted => true)
142
+ job.aasm.events(:permitted => true).map(&:name)
143
143
  # => [:run]
144
144
  ```
145
145
 
data/aasm.gemspec CHANGED
@@ -9,24 +9,23 @@ Gem::Specification.new do |s|
9
9
  s.email = %q{scott@elitists.net, ttilley@gmail.com, aasm@mt7.de}
10
10
  s.homepage = %q{https://github.com/aasm/aasm}
11
11
  s.summary = %q{State machine mixin for Ruby objects}
12
- s.description = %q{AASM is a continuation of the acts as state machine rails plugin, built for plain Ruby objects.}
12
+ s.description = %q{AASM is a continuation of the acts-as-state-machine rails plugin, built for plain Ruby objects.}
13
13
  s.date = Time.now
14
14
  s.licenses = ["MIT"]
15
15
 
16
- # s.add_development_dependency 'activerecord', '3.2.15'
17
- # s.add_development_dependency 'activerecord', '4.0.1'
16
+ s.platform = Gem::Platform::RUBY
17
+ s.required_ruby_version = '>= 1.9.3'
18
18
 
19
- # s.add_development_dependency 'mongoid' if Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version.create('1.9.3')
20
19
  s.add_development_dependency 'rake'
21
20
  s.add_development_dependency 'sdoc'
22
- s.add_development_dependency 'rspec', '~> 2.14'
23
- s.add_development_dependency 'rr'
24
- # s.add_development_dependency 'sqlite3'
25
- s.add_development_dependency 'minitest'
21
+ s.add_development_dependency 'rspec'
22
+
23
+ # debugging
26
24
  # s.add_development_dependency 'debugger'
27
- # s.add_development_dependency 'pry'
25
+ s.add_development_dependency 'pry'
28
26
 
29
- s.add_development_dependency 'mime-types', '~> 1.25' # needed by coveralls (>= 2.0 needs Ruby >=1.9.2)
27
+ # test coverage
28
+ # s.add_development_dependency 'mime-types', '~> 1.25' # needed by coveralls (>= 2.0 needs Ruby >=1.9.2)
30
29
  # s.add_development_dependency 'coveralls'
31
30
 
32
31
  s.files = `git ls-files`.split("\n")
@@ -7,7 +7,9 @@ gem 'rubinius-developer_tools', :platforms => :rbx
7
7
  gem "jruby-openssl", :platforms => :jruby
8
8
  gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
9
9
  gem "rails", "3.2.21"
10
- gem 'mongoid' if Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version.create('1.9.3')
10
+ gem 'mongoid', '~>3.1' if Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version.create('1.9.3')
11
11
  gem 'sequel'
12
+ gem 'mongo_mapper', '~>0.13'
13
+ gem 'bson_ext', :platforms => :ruby
12
14
 
13
15
  gemspec :path => "../"
@@ -6,11 +6,8 @@ gem 'rubysl', :platforms => :rbx
6
6
  gem 'rubinius-developer_tools', :platforms => :rbx
7
7
  gem "jruby-openssl", :platforms => :jruby
8
8
  gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
9
- gem "rails", "4.0.12"
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 "rails", "4.0.13"
10
+ gem 'mongoid', '~>4.0' if Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version.create('1.9.3')
14
11
  gem 'sequel'
15
12
 
16
13
  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 => "../"
@@ -6,11 +6,8 @@ gem 'rubysl', :platforms => :rbx
6
6
  gem 'rubinius-developer_tools', :platforms => :rbx
7
7
  gem "jruby-openssl", :platforms => :jruby
8
8
  gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
9
- gem "rails", "4.1.8"
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 "rails", "4.1.9"
10
+ gem 'mongoid', '~>4.0' if Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version.create('1.9.3')
14
11
  gem 'sequel'
15
12
 
16
13
  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 => "../"
@@ -0,0 +1,13 @@
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 'mongoid', '~>4.0' if Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version.create('1.9.3')
11
+ gem 'sequel'
12
+
13
+ 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 => "../"
data/lib/aasm/aasm.rb CHANGED
@@ -40,24 +40,6 @@ module AASM
40
40
  @aasm ||= AASM::InstanceBase.new(self)
41
41
  end
42
42
 
43
- # may be overwritten by persistence mixins
44
- def aasm_read_state
45
- # all the following lines behave like @current_state ||= aasm.enter_initial_state
46
- current = aasm.instance_variable_get("@current_state")
47
- return current if current
48
- aasm.instance_variable_set("@current_state", aasm.enter_initial_state)
49
- end
50
-
51
- # may be overwritten by persistence mixins
52
- def aasm_write_state(new_state)
53
- true
54
- end
55
-
56
- # may be overwritten by persistence mixins
57
- def aasm_write_state_without_persistence(new_state)
58
- true
59
- end
60
-
61
43
  private
62
44
 
63
45
  # Takes args and a from state and removes the first
@@ -99,7 +81,7 @@ private
99
81
  aasm_failed(event_name, old_state)
100
82
  end
101
83
  rescue StandardError => e
102
- event.fire_callbacks(:error, self, e) || raise(e)
84
+ event.fire_callbacks(:error, self, e, *process_args(event, aasm.current_state, *args)) || raise(e)
103
85
  end
104
86
  end
105
87
 
data/lib/aasm/base.rb CHANGED
@@ -56,32 +56,32 @@ module AASM
56
56
  def state(name, options={})
57
57
  @state_machine.add_state(name, @klass, options)
58
58
 
59
- @klass.send(:define_method, "#{name.to_s}?") do
59
+ @klass.send(:define_method, "#{name}?") do
60
60
  aasm.current_state == name
61
61
  end
62
62
 
63
- unless @klass.const_defined?("STATE_#{name.to_s.upcase}")
64
- @klass.const_set("STATE_#{name.to_s.upcase}", name)
63
+ unless @klass.const_defined?("STATE_#{name.upcase}")
64
+ @klass.const_set("STATE_#{name.upcase}", name)
65
65
  end
66
66
  end
67
67
 
68
68
  # define an event
69
69
  def event(name, options={}, &block)
70
- @state_machine.events[name] = AASM::Event.new(name, options, &block)
70
+ @state_machine.events[name] = AASM::Core::Event.new(name, options, &block)
71
71
 
72
72
  # an addition over standard aasm so that, before firing an event, you can ask
73
73
  # may_event? and get back a boolean that tells you whether the guard method
74
74
  # on the transition will let this happen.
75
- @klass.send(:define_method, "may_#{name.to_s}?") do |*args|
75
+ @klass.send(:define_method, "may_#{name}?") do |*args|
76
76
  aasm.may_fire_event?(name, *args)
77
77
  end
78
78
 
79
- @klass.send(:define_method, "#{name.to_s}!") do |*args, &block|
80
- aasm.current_event = "#{name.to_s}!".to_sym
79
+ @klass.send(:define_method, "#{name}!") do |*args, &block|
80
+ aasm.current_event = "#{name}!".to_sym
81
81
  aasm_fire_event(name, {:persist => true}, *args, &block)
82
82
  end
83
83
 
84
- @klass.send(:define_method, "#{name.to_s}") do |*args, &block|
84
+ @klass.send(:define_method, "#{name}") do |*args, &block|
85
85
  aasm.current_event = name.to_sym
86
86
  aasm_fire_event(name, {:persist => false}, *args, &block)
87
87
  end
@@ -1,4 +1,4 @@
1
- module AASM
1
+ module AASM::Core
2
2
  class Event
3
3
  include DslHelper
4
4
 
@@ -61,11 +61,11 @@ module AASM
61
61
  if definitions # define new transitions
62
62
  # Create a separate transition for each from-state to the given state
63
63
  Array(definitions[:from]).each do |s|
64
- @transitions << AASM::Transition.new(attach_event_guards(definitions.merge(:from => s.to_sym)), &block)
64
+ @transitions << AASM::Core::Transition.new(attach_event_guards(definitions.merge(:from => s.to_sym)), &block)
65
65
  end
66
66
  # Create a transition if :to is specified without :from (transitions from ANY state)
67
67
  if @transitions.empty? && definitions[:to]
68
- @transitions << AASM::Transition.new(attach_event_guards(definitions), &block)
68
+ @transitions << AASM::Core::Transition.new(attach_event_guards(definitions), &block)
69
69
  end
70
70
  end
71
71
  @transitions
@@ -124,14 +124,22 @@ module AASM
124
124
  def invoke_callbacks(code, record, args)
125
125
  case code
126
126
  when Symbol, String
127
- record.send(code, *args)
127
+ unless record.respond_to?(code, true)
128
+ raise NoMethodError.new("NoMethodError: undefined method `#{code}' for #{record.inspect}:#{record.class}")
129
+ end
130
+ arity = record.send(:method, code.to_sym).arity
131
+ record.send(code, *(arity < 0 ? args : args[0...arity]))
128
132
  true
133
+
129
134
  when Proc
130
- record.instance_exec(*args, &code)
135
+ arity = code.arity
136
+ record.instance_exec(*(arity < 0 ? args : args[0...arity]), &code)
131
137
  true
138
+
132
139
  when Array
133
140
  code.each {|a| invoke_callbacks(a, record, args)}
134
141
  true
142
+
135
143
  else
136
144
  false
137
145
  end
@@ -1,4 +1,4 @@
1
- module AASM
1
+ module AASM::Core
2
2
  class State
3
3
  attr_reader :name, :options
4
4
 
@@ -1,4 +1,4 @@
1
- module AASM
1
+ module AASM::Core
2
2
  class Transition
3
3
  include DslHelper
4
4
 
@@ -50,11 +50,9 @@ module AASM
50
50
 
51
51
  case code
52
52
  when Symbol, String
53
- # QUESTION : record.send(code, *args) ?
54
53
  arity = record.send(:method, code.to_sym).arity
55
54
  arity == 0 ? record.send(code) : record.send(code, *args)
56
55
  when Proc
57
- # QUESTION : record.instance_exec(*args, &code) ?
58
56
  code.arity == 0 ? record.instance_exec(&code) : record.instance_exec(*args, &code)
59
57
  when Array
60
58
  if options[:guard]
@@ -76,7 +76,7 @@ module AASM
76
76
 
77
77
  def may_fire_event?(name, *args)
78
78
  if event = @instance.class.aasm.state_machine.events[name]
79
- event.may_fire?(@instance, *args)
79
+ !!event.may_fire?(@instance, *args)
80
80
  else
81
81
  false # unknown event
82
82
  end
@@ -1,3 +1,5 @@
1
+ require_relative 'base'
2
+
1
3
  module AASM
2
4
  module Persistence
3
5
  module ActiveRecordPersistence
@@ -8,7 +10,7 @@ module AASM
8
10
  #
9
11
  # Adds
10
12
  #
11
- # before_validation :aasm_ensure_initial_state, :on => :create
13
+ # after_initialize :aasm_ensure_initial_state
12
14
  #
13
15
  # As a result, it doesn't matter when you define your methods - the following 2 are equivalent
14
16
  #
@@ -31,15 +33,10 @@ module AASM
31
33
  base.extend AASM::Persistence::ActiveRecordPersistence::ClassMethods
32
34
  base.send(:include, AASM::Persistence::ActiveRecordPersistence::InstanceMethods)
33
35
 
34
- if ActiveRecord::VERSION::MAJOR >= 3
35
- base.before_validation(:aasm_ensure_initial_state, :on => :create)
36
- else
37
- base.before_validation_on_create(:aasm_ensure_initial_state)
36
+ base.after_initialize do
37
+ aasm_ensure_initial_state
38
38
  end
39
39
 
40
- # ensure initial aasm state even when validations are skipped
41
- base.before_create(:aasm_ensure_initial_state)
42
-
43
40
  # ensure state is in the list of states
44
41
  base.validate :aasm_validate_states
45
42
  end
@@ -145,9 +142,9 @@ module AASM
145
142
 
146
143
  def aasm_raw_attribute_value(state)
147
144
  if aasm_enum
148
- value = self.class.send(aasm_enum)[state]
145
+ self.class.send(aasm_enum)[state]
149
146
  else
150
- value = state.to_s
147
+ state.to_s
151
148
  end
152
149
  end
153
150
 
@@ -167,7 +164,11 @@ module AASM
167
164
  # foo.aasm_state # => nil
168
165
  #
169
166
  def aasm_ensure_initial_state
170
- aasm.enter_initial_state if send(self.class.aasm.attribute_name).blank?
167
+ # checking via respond_to? does not work in Rails <= 3
168
+ # if respond_to?(self.class.aasm.attribute_name) && send(self.class.aasm.attribute_name).blank? # Rails 4
169
+ if attribute_names.include?(self.class.aasm.attribute_name.to_s) && send(self.class.aasm.attribute_name).blank?
170
+ aasm.enter_initial_state
171
+ end
171
172
  end
172
173
 
173
174
  def aasm_fire_event(name, options, *args, &block)
@@ -195,5 +196,5 @@ module AASM
195
196
  end # InstanceMethods
196
197
 
197
198
  end
198
- end
199
- end
199
+ end # Persistence
200
+ end # AASM
@@ -37,7 +37,7 @@ module AASM
37
37
  if new_record?
38
38
  state.blank? ? aasm.determine_state_name(self.class.aasm.initial_state) : state.to_sym
39
39
  else
40
- state.nil? ? nil : state.to_sym
40
+ state.blank? ? nil : state.to_sym
41
41
  end
42
42
  end
43
43
 
@@ -71,6 +71,9 @@ module AASM
71
71
  elsif @klass.ancestors.map {|klass| klass.to_s}.include?("Mongoid::Document")
72
72
  scope_options = lambda { @klass.send(:where, {@klass.aasm.attribute_name.to_sym => name.to_s}) }
73
73
  @klass.send(:scope, name, scope_options)
74
+ elsif @klass.ancestors.map {|klass| klass.to_s}.include?("MongoMapper::Document")
75
+ conditions = { @klass.aasm.attribute_name.to_sym => name.to_s }
76
+ @klass.scope(name, lambda { @klass.where(conditions) })
74
77
  end
75
78
  end
76
79
  end