aasm 4.1.0 → 4.1.1

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: a07996d6cd3f0270a6ec9d010a904cebd462b6a7
4
- data.tar.gz: 57e8efc5d284371e23c50d41771beb3172787bdd
3
+ metadata.gz: e85a2c10a3cf49a548193c9634548eb38eb1578b
4
+ data.tar.gz: 9b87e33663caa939dbf4eeefad57ea6ebde01fac
5
5
  SHA512:
6
- metadata.gz: 0e1f53507bcbcf9deecce7193a5fed9f37ae5fbc91b6325f127820ead35113e19bea7dba6f87af9482affac31462b05e1b6c89a87ef61a47ad93d95c07f19179
7
- data.tar.gz: 9da9eea4b58047190c441dc42c120fed864c318828340cc61505601f70179736d9cc70a0f60166bae13c6fc6aa3f3c8c4569047915a0af72ab152f30ce5422fd
6
+ metadata.gz: 1e2b255e2cae2cfae4a13c14ef8bd89f20c132ae5acf8260e68319f40646008304f05870e5cfbb2e4402b7cedc588552b777407c613b30f7e8b1787b9eac8faf
7
+ data.tar.gz: 41843dcb1cfa5c1147abea4420951ae3217349abd7a7918a1ef0014671218a520957c61d2d7f70ae5634514df5722466e83deb64549b463a5d3d51a323049da3
@@ -2,6 +2,13 @@
2
2
 
3
3
  ## 4.2.0 (not yet released)
4
4
 
5
+ ## 4.1.1
6
+
7
+ * support block notation for `:after_commit` event callbacks (see [issue #224](https://github.com/aasm/aasm/issues/224) for details)
8
+ * 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))
9
+ * `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))
10
+ * 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))
11
+
5
12
  ## 4.1.0
6
13
 
7
14
  * 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 ))
data/Gemfile CHANGED
@@ -1,7 +1,6 @@
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 "jruby-openssl", :platforms => :jruby
7
6
  gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
data/README.md CHANGED
@@ -2,8 +2,8 @@
2
2
 
3
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
+ [![Dependency Status](https://gemnasium.com/aasm/aasm.svg)](https://gemnasium.com/aasm/aasm)
5
6
  [![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
 
@@ -24,10 +24,6 @@ Gem::Specification.new do |s|
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) }
@@ -1,7 +1,6 @@
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
@@ -1,7 +1,6 @@
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
@@ -1,7 +1,6 @@
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
@@ -1,7 +1,6 @@
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
@@ -69,8 +69,10 @@ private
69
69
  )
70
70
 
71
71
  if may_fire_to = event.may_fire?(self, *args)
72
- old_state.fire_callbacks(:before_exit, self)
73
- old_state.fire_callbacks(:exit, self) # TODO: remove for AASM 4?
72
+ old_state.fire_callbacks(:before_exit, self,
73
+ *process_args(event, aasm.current_state, *args))
74
+ old_state.fire_callbacks(:exit, self,
75
+ *process_args(event, aasm.current_state, *args)) # TODO: remove for AASM 4?
74
76
 
75
77
  if new_state_name = event.fire(self, {:may_fire => may_fire_to}, *args)
76
78
  aasm_fired(event, old_state, new_state_name, options, *args, &block)
@@ -90,9 +92,11 @@ private
90
92
 
91
93
  new_state = aasm.state_object_for_name(new_state_name)
92
94
 
93
- new_state.fire_callbacks(:before_enter, self)
95
+ new_state.fire_callbacks(:before_enter, self,
96
+ *process_args(event, aasm.current_state, *args))
94
97
 
95
- new_state.fire_callbacks(:enter, self) # TODO: remove for AASM 4?
98
+ new_state.fire_callbacks(:enter, self,
99
+ *process_args(event, aasm.current_state, *args)) # TODO: remove for AASM 4?
96
100
 
97
101
  persist_successful = true
98
102
  if persist
@@ -107,8 +111,10 @@ private
107
111
  end
108
112
 
109
113
  if persist_successful
110
- old_state.fire_callbacks(:after_exit, self)
111
- new_state.fire_callbacks(:after_enter, self)
114
+ old_state.fire_callbacks(:after_exit, self,
115
+ *process_args(event, aasm.current_state, *args))
116
+ new_state.fire_callbacks(:after_enter, self,
117
+ *process_args(event, aasm.current_state, *args))
112
118
  event.fire_callbacks(
113
119
  :after,
114
120
  self,
@@ -129,7 +135,7 @@ private
129
135
  end
130
136
 
131
137
  if AASM::StateMachine[self.class].config.whiny_transitions
132
- raise AASM::InvalidTransition, "Event '#{event_name}' cannot transition from '#{aasm.current_state}'"
138
+ raise AASM::InvalidTransition.new(self, event_name)
133
139
  else
134
140
  false
135
141
  end
@@ -12,7 +12,7 @@ module AASM::Core
12
12
 
13
13
  # from aasm4
14
14
  @options = options # QUESTION: .dup ?
15
- add_options_from_dsl(@options, [:after, :before, :error, :success], &block) if block
15
+ add_options_from_dsl(@options, [:after, :before, :error, :success, :after_commit], &block) if block
16
16
  end
17
17
 
18
18
  # a neutered version of fire - it doesn't actually fire the event, it just
@@ -28,12 +28,12 @@ module AASM::Core
28
28
  name.to_s
29
29
  end
30
30
 
31
- def fire_callbacks(action, record)
31
+ def fire_callbacks(action, record, *args)
32
32
  action = @options[action]
33
33
  catch :halt_aasm_chain do
34
34
  action.is_a?(Array) ?
35
- action.each {|a| _fire_callbacks(a, record)} :
36
- _fire_callbacks(action, record)
35
+ action.each {|a| _fire_callbacks(a, record, args)} :
36
+ _fire_callbacks(action, record, args)
37
37
  end
38
38
  end
39
39
 
@@ -66,12 +66,14 @@ module AASM::Core
66
66
  self
67
67
  end
68
68
 
69
- def _fire_callbacks(action, record)
69
+ def _fire_callbacks(action, record, args)
70
70
  case action
71
71
  when Symbol, String
72
- record.send(action)
72
+ arity = record.send(:method, action.to_sym).arity
73
+ record.send(action, *(arity < 0 ? args : args[0...arity]))
73
74
  when Proc
74
- action.call(record)
75
+ arity = action.arity
76
+ action.call(record, *(arity < 0 ? args : args[0...arity]))
75
77
  end
76
78
  end
77
79
 
@@ -1,5 +1,16 @@
1
1
  module AASM
2
- class InvalidTransition < RuntimeError; end
2
+
3
+ class InvalidTransition < RuntimeError
4
+ attr_reader :object, :event_name
5
+ def initialize(object, event_name)
6
+ @object, @event_name = object, event_name
7
+ end
8
+
9
+ def message
10
+ "Event '#{event_name}' cannot transition from '#{object.aasm.current_state}'"
11
+ end
12
+ end
13
+
3
14
  class UndefinedState < RuntimeError; end
4
15
  class NoDirectAssignmentError < RuntimeError; end
5
16
  end
@@ -187,7 +187,7 @@ module AASM
187
187
  end
188
188
 
189
189
  def aasm_validate_states
190
- unless AASM::StateMachine[self.class].config.skip_validation_on_save
190
+ unless aasm_skipping_validations
191
191
  if aasm.current_state && !aasm.states.include?(aasm.current_state)
192
192
  self.errors.add(AASM::StateMachine[self.class].config.column , "is invalid")
193
193
  end
@@ -72,6 +72,7 @@ module AASM
72
72
  #
73
73
  def aasm_ensure_initial_state
74
74
  aasm.enter_initial_state if
75
+ (new? || values.key?(self.class.aasm.attribute_name)) &&
75
76
  send(self.class.aasm.attribute_name).to_s.strip.empty?
76
77
  end
77
78
 
@@ -1,3 +1,3 @@
1
1
  module AASM
2
- VERSION = "4.1.0"
2
+ VERSION = "4.1.1"
3
3
  end
@@ -44,15 +44,15 @@ module Callbacks
44
44
 
45
45
  def aasm_write_state(*args); log('aasm_write_state'); true; end
46
46
 
47
- def before_enter_open; log('before_enter_open'); end
48
- def before_exit_open; log('before_exit_open'); end
49
- def after_enter_open; log('after_enter_open'); end
50
- def after_exit_open; log('after_exit_open'); end
51
-
52
- def before_enter_closed; log('before_enter_closed'); end
53
- def before_exit_closed; log('before_enter_closed'); end
54
- def after_enter_closed; log('after_enter_closed'); end
55
- def after_exit_closed; log('after_exit_closed'); end
47
+ def before_enter_open(*args); log("before_enter_open(#{args.map(&:inspect).join(',')})"); end
48
+ def before_exit_open(*args); log("before_exit_open(#{args.map(&:inspect).join(',')})"); end
49
+ def after_enter_open(*args); log("after_enter_open(#{args.map(&:inspect).join(',')})"); end
50
+ def after_exit_open(*args); log("after_exit_open(#{args.map(&:inspect).join(',')})"); end
51
+
52
+ def before_enter_closed(*args); log("before_enter_closed(#{args.map(&:inspect).join(',')})"); end
53
+ def before_exit_closed(*args); log("before_enter_closed(#{args.map(&:inspect).join(',')})"); end
54
+ def after_enter_closed(*args); log("after_enter_closed(#{args.map(&:inspect).join(',')})"); end
55
+ def after_exit_closed(*args); log("after_exit_closed(#{args.map(&:inspect).join(',')})"); end
56
56
 
57
57
  def before(arg1, *args); log("before(#{arg1.inspect},#{args.map(&:inspect).join(',')})"); end
58
58
  def transition_proc(arg1, arg2); log("transition_proc(#{arg1.inspect},#{arg2.inspect})"); end
@@ -12,6 +12,9 @@ class Validator < ActiveRecord::Base
12
12
  transitions :to => :running, :from => :sleeping
13
13
  end
14
14
  event :sleep do
15
+ after_commit do
16
+ change_name_on_sleep
17
+ end
15
18
  transitions :to => :sleeping, :from => :running
16
19
  end
17
20
  event :fail do
@@ -26,6 +29,11 @@ class Validator < ActiveRecord::Base
26
29
  save!
27
30
  end
28
31
 
32
+ def change_name_on_sleep
33
+ self.name = "sleeper"
34
+ save!
35
+ end
36
+
29
37
  def fail
30
38
  raise StandardError.new('failed on purpose')
31
39
  end
@@ -4,9 +4,6 @@ require 'aasm'
4
4
 
5
5
  require 'rspec'
6
6
 
7
- require 'coveralls'
8
- Coveralls.wear!
9
-
10
7
  # require 'ruby-debug'; Debugger.settings[:autoeval] = true; debugger; rubys_debugger = 'annoying'
11
8
  # require 'ruby-debug/completion'
12
9
  # require 'pry'
@@ -145,7 +145,7 @@ describe 'callbacks for the new DSL' do
145
145
 
146
146
  cb.reset_data
147
147
  cb.close!(:arg1, :arg2)
148
- expect(cb.data).to eql 'before(:arg1,:arg2) before_exit_open transition_proc(:arg1,:arg2) before_enter_closed aasm_write_state after_exit_open after_enter_closed after(:arg1,:arg2)'
148
+ expect(cb.data).to eql 'before(:arg1,:arg2) before_exit_open(:arg1,:arg2) transition_proc(:arg1,:arg2) before_enter_closed(:arg1,:arg2) aasm_write_state after_exit_open(:arg1,:arg2) after_enter_closed(:arg1,:arg2) after(:arg1,:arg2)'
149
149
  end
150
150
 
151
151
  it "should call the callbacks given the to-state as argument" do
@@ -431,9 +431,14 @@ describe 'transitions with persistence' do
431
431
  it "should fire :after_commit if transaction was successful" do
432
432
  validator = Validator.create(:name => 'name')
433
433
  expect(validator).to be_sleeping
434
+
434
435
  validator.run!
435
436
  expect(validator).to be_running
436
- expect(validator.name).not_to eq("name")
437
+ expect(validator.name).to eq("name changed")
438
+
439
+ validator.sleep!
440
+ expect(validator).to be_sleeping
441
+ expect(validator.name).to eq("sleeper")
437
442
  end
438
443
 
439
444
  it "should not fire :after_commit if transaction failed" do
@@ -44,6 +44,11 @@ describe 'sequel' do
44
44
  expect(model.aasm.current_state).to eq(:alpha)
45
45
  end
46
46
 
47
+ it "should save the initial state" do
48
+ model.save
49
+ expect(model.status).to eq("alpha")
50
+ end
51
+
47
52
  it "should return the aasm column when new and the aasm field is not nil" do
48
53
  model.status = "beta"
49
54
  expect(model.aasm.current_state).to eq(:beta)
@@ -61,6 +66,14 @@ describe 'sequel' do
61
66
  expect(model.aasm.current_state).to be_nil
62
67
  end
63
68
 
69
+ it "should not change the state if state is not loaded" do
70
+ model.release
71
+ model.save
72
+ model.class.select(:id).first.save
73
+ model.reload
74
+ expect(model.aasm.current_state).to eq(:beta)
75
+ end
76
+
64
77
  it "should call aasm_ensure_initial_state on validation before create" do
65
78
  expect(model).to receive(:aasm_ensure_initial_state).and_return(true)
66
79
  model.valid?
@@ -4,7 +4,12 @@ describe 'transitions' do
4
4
 
5
5
  it 'should raise an exception when whiny' do
6
6
  process = ProcessWithNewDsl.new
7
- expect { process.stop! }.to raise_error(AASM::InvalidTransition)
7
+ expect { process.stop! }.to raise_error do |err|
8
+ expect(err.class).to eql(AASM::InvalidTransition)
9
+ expect(err.message).to eql("Event 'stop' cannot transition from 'sleeping'")
10
+ expect(err.object).to eql(process)
11
+ expect(err.event_name).to eql(:stop)
12
+ end
8
13
  expect(process).to be_sleeping
9
14
  end
10
15
 
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.1.0
4
+ version: 4.1.1
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-02-24 00:00:00.000000000 Z
13
+ date: 2015-05-13 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake