aasm 4.3.0 → 4.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1b51607dcaee12f000a23ee83033925bf03dcee7
4
- data.tar.gz: c367dd718de8264432f332ff626fc8aff05581e3
3
+ metadata.gz: 7796dc2427b68fdbe78f6a79597c483b04f0cf70
4
+ data.tar.gz: 5b787a0ef49492a02f6beb7e5fffe6ab44c7724d
5
5
  SHA512:
6
- metadata.gz: 4a3750ab5b7758679ed9ca66fc35f04cf69347a09bc03070fd514ccbd3c111f129f907e6c4e387cf253441daf7e8e79a1045ea64da43296803434711c56a2b84
7
- data.tar.gz: d82ff5d11b0ebae4112277283706e3bd9774517e6f4a7bece806360deda054c811ccebccfefd59c05244dd05e5351f21672932ba8071a8c2626077ba76bc585d
6
+ metadata.gz: 2d429dd03690c213d5fed037d1d65b5fcc31f6ef83ff6a54ddd6092f46958f5bebe753d429bd8ecbd70e83ba210265895e83028fdbb5e5b8aab9031cea532a4f
7
+ data.tar.gz: 0b9fe7da0c4755fabb564fc05beb553d8fd666d6cc885079bef5eb6c97f2a08e81b8721bdac39b0453118b3a7835387eb2c4881855a19c9e72310b241d680c76
@@ -18,11 +18,12 @@ services: mongodb
18
18
  gemfile:
19
19
  - gemfiles/rails_3.2.gemfile
20
20
  - gemfiles/rails_4.0.gemfile
21
- - gemfiles/rails_4.0_bson1.gemfile
21
+ - gemfiles/rails_4.0_mongo_mapper.gemfile
22
22
  - gemfiles/rails_4.1.gemfile
23
- - gemfiles/rails_4.1_bson1.gemfile
23
+ - gemfiles/rails_4.1_mongo_mapper.gemfile
24
24
  - gemfiles/rails_4.2.gemfile
25
- - gemfiles/rails_4.2_bson1.gemfile
25
+ - gemfiles/rails_4.2_mongoid_5.gemfile
26
+ - gemfiles/rails_4.2_mongo_mapper.gemfile
26
27
 
27
28
  matrix:
28
29
  allow_failures:
@@ -1,5 +1,10 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 4.4.0
4
+
5
+ * 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)
6
+ * 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)
7
+
3
8
  ## 4.3.0
4
9
 
5
10
  * 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)
@@ -4,24 +4,8 @@
4
4
 
5
5
  * drop support for aasm_column ?
6
6
 
7
+
7
8
  # Currently working on
8
9
 
9
10
 
10
11
  # Changes so far
11
-
12
- ## version 4.3
13
-
14
- * add support for multiple state machines per class
15
- * class- and instance-level `aasm` methods accept a state machine selector
16
- (aka the state machine _name_)
17
- * if no selector/name is provided, `:default` will be used
18
- * duplicate definitions of states and events will issue warnings
19
- * check all tests
20
- * _ActiveRecord_
21
- * _Mongoid_
22
- * _MongoMapper_
23
- * _Sequel_
24
- * what happen's if someone accesses `aasm`, but has defined a
25
- state machine for `aasm(:my_name)`?
26
- * documentation
27
- * drop support for find_in_state, count_in_state, calculate_in_state, with_state_scope
data/README.md CHANGED
@@ -98,6 +98,8 @@ 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
104
  before do
103
105
  log('Preparing to run')
@@ -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) you can access the
176
- originating state (the from-state) and the target state (the to state), like this:
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)
@@ -313,9 +321,9 @@ job.aasm.current_state # stage3
313
321
  ```
314
322
 
315
323
 
316
- ### Multiple state machine per class
324
+ ### Multiple state machines per class
317
325
 
318
- Multiple state machines per class are supported. Be aware though, that _AASM_ has been
326
+ Multiple state machines per class are supported. Be aware though that _AASM_ has been
319
327
  built with one state machine per class in mind. Nonetheless, here's how to do it:
320
328
 
321
329
  ```ruby
@@ -365,9 +373,9 @@ simple.aasm(:work).current
365
373
 
366
374
  ```
367
375
 
368
- _AASM_ doesn't prohibit to define the same event in both state machines. The
369
- latest definition "wins" and overrides previous definitions. A warning is issued:
370
- `SimpleMultipleExample: The event name run is already used!`.
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!`.
371
379
 
372
380
  All _AASM_ class- and instance-level `aasm` methods accept a state machine selector.
373
381
  So, for example, to use inspection on a class level, you have to use
@@ -378,7 +386,7 @@ SimpleMultipleExample.aasm(:work).states
378
386
  ```
379
387
 
380
388
  *Final note*: Support for multiple state machines per class is a pretty new feature
381
- (since version `4.3`), so please bear with us in case it doesn't as expected.
389
+ (since version `4.3`), so please bear with us in case it doesn't work as expected.
382
390
 
383
391
 
384
392
 
@@ -417,7 +425,7 @@ job.run! # saved
417
425
 
418
426
  Saving includes running all validations on the `Job` class. If you want make sure
419
427
  the state gets saved without running validations (and thereby maybe persisting an
420
- invalid object state), simply tell AASM to skip the validations. Be aware, that
428
+ invalid object state), simply tell AASM to skip the validations. Be aware that
421
429
  when skipping validations, only the state column will be updated in the database
422
430
  (just like ActiveRecord `change_column` is working).
423
431
 
@@ -704,6 +712,10 @@ job.aasm.states(:permitted => true).map(&:name)
704
712
  # show all possible (triggerable) events (allowed by transitions)
705
713
  job.aasm.events.map(&:name)
706
714
  => [:sleep]
715
+
716
+ # list states for select
717
+ Job.aasm.states_for_select
718
+ => [["Sleeping", "sleeping"], ["Running", "running"], ["Cleaning", "cleaning"]]
707
719
  ```
708
720
 
709
721
 
@@ -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 => "../"
@@ -67,7 +67,7 @@ module AASM
67
67
  @state_machine.add_state(name, @klass, options)
68
68
 
69
69
  if @klass.instance_methods.include?("#{name}?")
70
- warn "#{@klass.name}: The state name #{name} is already used!"
70
+ warn "#{@klass.name}: The aasm state name #{name} is already used!"
71
71
  end
72
72
 
73
73
  @klass.class_eval <<-EORUBY, __FILE__, __LINE__ + 1
@@ -86,7 +86,7 @@ module AASM
86
86
  @state_machine.add_event(name, options, &block)
87
87
 
88
88
  if @klass.instance_methods.include?("may_#{name}?".to_sym)
89
- warn "#{@klass.name}: The event name #{name} is already used!"
89
+ warn "#{@klass.name}: The aasm event name #{name} is already used!"
90
90
  end
91
91
 
92
92
  # an addition over standard aasm so that, before firing an event, you can ask
@@ -109,6 +109,10 @@ module AASM
109
109
  EORUBY
110
110
  end
111
111
 
112
+ def after_all_transitions(*callbacks, &block)
113
+ @state_machine.add_global_callbacks(:after_all_transitions, *callbacks, &block)
114
+ end
115
+
112
116
  def states
113
117
  @state_machine.states
114
118
  end
@@ -30,6 +30,7 @@ module AASM::Core
30
30
  end
31
31
 
32
32
  def execute(obj, *args)
33
+ invoke_callbacks_compatible_with_guard(event.state_machine.global_callbacks[:after_all_transitions], obj, args)
33
34
  invoke_callbacks_compatible_with_guard(@after, obj, args)
34
35
  end
35
36
 
@@ -59,7 +59,7 @@ module AASM
59
59
  end
60
60
 
61
61
  def state_object_for_name(name)
62
- obj = @instance.class.aasm(@name).states.find {|s| s == name}
62
+ obj = @instance.class.aasm(@name).states.find {|s| s.name == name}
63
63
  raise AASM::UndefinedState, "State :#{name} doesn't exist" if obj.nil?
64
64
  obj
65
65
  end
@@ -56,8 +56,8 @@ module AASM
56
56
  def state_with_scope(name, *args)
57
57
  state_without_scope(name, *args)
58
58
  if @state_machine.config.create_scopes && !@klass.respond_to?(name)
59
- if @klass.ancestors.map {|klass| klass.to_s}.include?("ActiveRecord::Base")
60
59
 
60
+ if @klass.ancestors.map {|klass| klass.to_s}.include?("ActiveRecord::Base")
61
61
  conditions = {"#{@klass.table_name}.#{@klass.aasm(@name).attribute_name}" => name.to_s}
62
62
  if ActiveRecord::VERSION::MAJOR >= 3
63
63
  @klass.class_eval do
@@ -69,12 +69,17 @@ module AASM
69
69
  end
70
70
  end
71
71
  elsif @klass.ancestors.map {|klass| klass.to_s}.include?("Mongoid::Document")
72
- scope_options = lambda { @klass.send(:where, {@klass.aasm(@name).attribute_name.to_sym => name.to_s}) }
72
+ klass = @klass
73
+ state_machine_name = @name
74
+ scope_options = lambda {
75
+ klass.send(:where, {klass.aasm(state_machine_name).attribute_name.to_sym => name.to_s})
76
+ }
73
77
  @klass.send(:scope, name, scope_options)
74
78
  elsif @klass.ancestors.map {|klass| klass.to_s}.include?("MongoMapper::Document")
75
79
  conditions = { @klass.aasm(@name).attribute_name.to_sym => name.to_s }
76
80
  @klass.scope(name, lambda { @klass.where(conditions) })
77
81
  end
82
+
78
83
  end
79
84
  end
80
85
  alias_method :state_without_scope, :state
@@ -10,12 +10,13 @@ module AASM
10
10
  (@machines ||= {})[klass.to_s] = machine
11
11
  end
12
12
 
13
- attr_accessor :states, :events, :initial_state, :config, :name
13
+ attr_accessor :states, :events, :initial_state, :config, :name, :global_callbacks
14
14
 
15
15
  def initialize(name)
16
16
  @initial_state = nil
17
17
  @states = []
18
18
  @events = {}
19
+ @global_callbacks = {}
19
20
  @config = AASM::Configuration.new
20
21
  @name = name
21
22
  end
@@ -40,6 +41,14 @@ module AASM
40
41
  @events[name] = AASM::Core::Event.new(name, self, options, &block)
41
42
  end
42
43
 
44
+ def add_global_callbacks(name, *callbacks, &block)
45
+ @global_callbacks[name] ||= []
46
+ callbacks.each do |callback|
47
+ @global_callbacks[name] << callback
48
+ end
49
+ @global_callbacks[name] << block if block
50
+ end
51
+
43
52
  private
44
53
 
45
54
  def set_initial_state(name, options)
@@ -1,3 +1,3 @@
1
1
  module AASM
2
- VERSION = "4.3.0"
2
+ VERSION = "4.4.0"
3
3
  end
@@ -1,5 +1,5 @@
1
1
  ActiveRecord::Migration.suppress_messages do
2
- %w{gates multiple_gates readers writers transients simples no_scopes multiple_no_scopes no_direct_assignments multiple_no_direct_assignments thieves multiple_thieves localizer_test_models persisted_states provided_and_persisted_states with_enums with_true_enums with_false_enums false_states multiple_with_enums multiple_with_true_enums multiple_with_false_enums multiple_false_states}.each do |table_name|
2
+ %w{gates multiple_gates readers writers transients simples no_scopes multiple_no_scopes no_direct_assignments multiple_no_direct_assignments thieves multiple_thieves localizer_test_models persisted_states provided_and_persisted_states with_enums with_true_enums with_false_enums false_states multiple_with_enums multiple_with_true_enums multiple_with_false_enums multiple_false_states readme_jobs}.each do |table_name|
3
3
  ActiveRecord::Migration.create_table table_name, :force => true do |t|
4
4
  t.string "aasm_state"
5
5
  end
@@ -0,0 +1,21 @@
1
+ class ReadmeJob < ActiveRecord::Base
2
+ include AASM
3
+
4
+ aasm do
5
+ state :sleeping, :initial => true
6
+ state :running
7
+ state :cleaning
8
+
9
+ event :run do
10
+ transitions :from => :sleeping, :to => :running
11
+ end
12
+
13
+ event :clean do
14
+ transitions :from => :running, :to => :cleaning
15
+ end
16
+
17
+ event :sleep do
18
+ transitions :from => [:running, :cleaning], :to => :sleeping
19
+ end
20
+ end
21
+ end
@@ -18,6 +18,8 @@ module Callbacks
18
18
  end
19
19
 
20
20
  aasm do
21
+ after_all_transitions :after_all_transitions
22
+
21
23
  state :open, :initial => true,
22
24
  :before_enter => :before_enter_open,
23
25
  :enter => :enter_open,
@@ -68,6 +70,7 @@ module Callbacks
68
70
  def transition_guard; log('transition_guard'); !@fail_transition_guard; end
69
71
 
70
72
  def after_transition; log('after_transition'); end
73
+ def after_all_transitions;log('after_all_transitions');end
71
74
 
72
75
  def before_event; log('before_event'); end
73
76
  def after_event; log('after_event'); end
@@ -4,7 +4,7 @@ module Callbacks
4
4
  include AASM
5
5
 
6
6
  aasm do
7
- state :open, :inital => true
7
+ state :open, :initial => true
8
8
  state :closed
9
9
  state :out_to_lunch
10
10
 
@@ -4,7 +4,7 @@ module Callbacks
4
4
  include AASM
5
5
 
6
6
  aasm(:left) do
7
- state :open, :inital => true
7
+ state :open, :initial => true
8
8
  state :closed
9
9
  state :out_to_lunch
10
10
 
@@ -17,6 +17,7 @@ describe 'callbacks for the new DSL' do
17
17
  expect(callback).to receive(:exit_open).once.ordered
18
18
  # expect(callback).to receive(:event_guard).once.ordered.and_return(true)
19
19
  # expect(callback).to receive(:transition_guard).once.ordered.and_return(true)
20
+ expect(callback).to receive(:after_all_transitions).once.ordered
20
21
  expect(callback).to receive(:after_transition).once.ordered
21
22
  expect(callback).to receive(:before_enter_closed).once.ordered
22
23
  expect(callback).to receive(:enter_closed).once.ordered
@@ -521,3 +521,20 @@ describe 'basic example with two state machines' do
521
521
  expect(example.aasm(:sync).current_state).to eql :unsynced
522
522
  end
523
523
  end
524
+
525
+ describe 'testing the README examples' do
526
+ it 'Usage' do
527
+ job = ReadmeJob.new
528
+
529
+ expect(job.sleeping?).to eql true
530
+ expect(job.may_run?).to eql true
531
+
532
+ job.run
533
+
534
+ expect(job.running?).to eql true
535
+ expect(job.sleeping?).to eql false
536
+ expect(job.may_run?).to eql false
537
+
538
+ expect { job.run }.to raise_error(AASM::InvalidTransition)
539
+ end
540
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'testing the README examples' do
4
+
5
+ it 'Usage' do
6
+ class Job
7
+ include AASM
8
+
9
+ aasm do
10
+ state :sleeping, :initial => true
11
+ state :running
12
+ state :cleaning
13
+
14
+ event :run do
15
+ transitions :from => :sleeping, :to => :running
16
+ end
17
+
18
+ event :clean do
19
+ transitions :from => :running, :to => :cleaning
20
+ end
21
+
22
+ event :sleep do
23
+ transitions :from => [:running, :cleaning], :to => :sleeping
24
+ end
25
+ end
26
+ end
27
+
28
+ job = Job.new
29
+
30
+ expect(job.sleeping?).to eql true
31
+ expect(job.may_run?).to eql true
32
+
33
+ job.run
34
+
35
+ expect(job.running?).to eql true
36
+ expect(job.sleeping?).to eql false
37
+ expect(job.may_run?).to eql false
38
+
39
+ expect { job.run }.to raise_error(AASM::InvalidTransition)
40
+ end
41
+
42
+ 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: 4.3.0
4
+ version: 4.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Scott Barron
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2015-09-10 00:00:00.000000000 Z
13
+ date: 2015-10-27 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake
@@ -92,11 +92,12 @@ files:
92
92
  - callbacks.txt
93
93
  - gemfiles/rails_3.2.gemfile
94
94
  - gemfiles/rails_4.0.gemfile
95
- - gemfiles/rails_4.0_bson1.gemfile
95
+ - gemfiles/rails_4.0_mongo_mapper.gemfile
96
96
  - gemfiles/rails_4.1.gemfile
97
- - gemfiles/rails_4.1_bson1.gemfile
97
+ - gemfiles/rails_4.1_mongo_mapper.gemfile
98
98
  - gemfiles/rails_4.2.gemfile
99
- - gemfiles/rails_4.2_bson1.gemfile
99
+ - gemfiles/rails_4.2_mongo_mapper.gemfile
100
+ - gemfiles/rails_4.2_mongoid_5.gemfile
100
101
  - lib/aasm.rb
101
102
  - lib/aasm/aasm.rb
102
103
  - lib/aasm/base.rb
@@ -132,6 +133,7 @@ files:
132
133
  - spec/models/active_record/persisted_state.rb
133
134
  - spec/models/active_record/provided_and_persisted_state.rb
134
135
  - spec/models/active_record/reader.rb
136
+ - spec/models/active_record/readme_job.rb
135
137
  - spec/models/active_record/simple_new_dsl.rb
136
138
  - spec/models/active_record/thief.rb
137
139
  - spec/models/active_record/transient.rb
@@ -220,6 +222,7 @@ files:
220
222
  - spec/unit/persistence/mongoid_persistence_spec.rb
221
223
  - spec/unit/persistence/sequel_persistence_multiple_spec.rb
222
224
  - spec/unit/persistence/sequel_persistence_spec.rb
225
+ - spec/unit/readme_spec.rb
223
226
  - spec/unit/reloading_spec.rb
224
227
  - spec/unit/simple_example_spec.rb
225
228
  - spec/unit/simple_multiple_example_spec.rb
@@ -247,7 +250,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
247
250
  version: '0'
248
251
  requirements: []
249
252
  rubyforge_project:
250
- rubygems_version: 2.2.2
253
+ rubygems_version: 2.4.5
251
254
  signing_key:
252
255
  specification_version: 4
253
256
  summary: State machine mixin for Ruby objects
@@ -267,6 +270,7 @@ test_files:
267
270
  - spec/models/active_record/persisted_state.rb
268
271
  - spec/models/active_record/provided_and_persisted_state.rb
269
272
  - spec/models/active_record/reader.rb
273
+ - spec/models/active_record/readme_job.rb
270
274
  - spec/models/active_record/simple_new_dsl.rb
271
275
  - spec/models/active_record/thief.rb
272
276
  - spec/models/active_record/transient.rb
@@ -355,6 +359,7 @@ test_files:
355
359
  - spec/unit/persistence/mongoid_persistence_spec.rb
356
360
  - spec/unit/persistence/sequel_persistence_multiple_spec.rb
357
361
  - spec/unit/persistence/sequel_persistence_spec.rb
362
+ - spec/unit/readme_spec.rb
358
363
  - spec/unit/reloading_spec.rb
359
364
  - spec/unit/simple_example_spec.rb
360
365
  - spec/unit/simple_multiple_example_spec.rb