aasm 5.0.6 → 5.0.7

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: 51dfd1ad2a78034c409ddc556625e765260dd067
4
- data.tar.gz: f934e43d4a35fc75a06d161190a8794e36066c02
3
+ metadata.gz: aa5a74637dafaa4c06da6c03e96342997692ad3f
4
+ data.tar.gz: e46be5420174440acc2dea63d9e66958d7dc6efc
5
5
  SHA512:
6
- metadata.gz: 983b08a4a0977c37dd73b77c5d80d014a05025d7f2841c26788518c82a8e738825ab9fa5fc8a36577e9c6a23fd33cf341c6fefc954630836e213ab40ee12c909
7
- data.tar.gz: b091bca286362fb33a31f1b9705840f664ebd0a948c6b2dffbaa2436144c5931a04950080b18e16c1a7d9e4e1b8f517b7ea6c2c133259a3860905928fda32b90
6
+ metadata.gz: 359caf35d39d769ec0cc3d805cface2f89946692e62b1dd984fc42830be53305c3abfb300163d6e2f46317673fbd3a1c9e8ea366328feebdedab5806b85f1fd0
7
+ data.tar.gz: a1621683151f4ff00e1e83f4d7f3926bc1d5d4e0d945e2a38ed4f6355267c0165e2aabbeca553cdf17a8c9e4237130a0a63cc9715b0b0e8bb5801c71bd2bb053
@@ -14,6 +14,8 @@ before_install:
14
14
  rvm:
15
15
  - 2.3.0
16
16
  - 2.5.0
17
+ - 2.6.5
18
+ - 2.7.0
17
19
  - jruby-9.1.12.0
18
20
 
19
21
  services:
@@ -56,12 +58,28 @@ matrix:
56
58
  gemfile: gemfiles/rails_5.1.gemfile
57
59
  - rvm: 2.3.0
58
60
  gemfile: gemfiles/rails_5.2.gemfile
61
+ - rvm: 2.7.0
62
+ gemfile: gemfiles/rails_5.2.gemfile
63
+ - rvm: 2.6.5
64
+ gemfile: gemfiles/rails_5.2.gemfile
59
65
  - rvm: 2.5.0
60
66
  gemfile: gemfiles/rails_3.2.gemfile
67
+ - rvm: 2.6.5
68
+ gemfile: gemfiles/rails_3.2.gemfile
69
+ - rvm: 2.7.0
70
+ gemfile: gemfiles/rails_3.2.gemfile
61
71
  - rvm: 2.5.0
62
72
  gemfile: gemfiles/rails_4.2.gemfile
73
+ - rvm: 2.6.5
74
+ gemfile: gemfiles/rails_4.2.gemfile
75
+ - rvm: 2.7.0
76
+ gemfile: gemfiles/rails_4.2.gemfile
63
77
  - rvm: 2.5.0
64
78
  gemfile: gemfiles/rails_4.2_mongoid_5.gemfile
79
+ - rvm: 2.6.5
80
+ gemfile: gemfiles/rails_4.2_mongoid_5.gemfile
81
+ - rvm: 2.7.0
82
+ gemfile: gemfiles/rails_4.2_mongoid_5.gemfile
65
83
  # - rvm: 2.5.0
66
84
  # gemfile: gemfiles/rails_4.2_nobrainer.gemfile
67
85
  - rvm: jruby-9.1.12.0
@@ -2,6 +2,13 @@
2
2
 
3
3
  ## unreleased
4
4
 
5
+ ## 5.0.7
6
+
7
+ * Fix :after_commit within nested transaction [#666](https://github.com/aasm/aasm/pull/666), thanks to [stokarenko](https://github.com/stokarenko)
8
+ * Add permitted_transitions to group permitted event with state [#664](https://github.com/aasm/aasm/pull/664), thanks to [dnamsons](https://github.com/dnamsons)
9
+ * Add Ruby 2.7.0 & 2.6.5 to Travis CI Test Matrix [#661](https://github.com/aasm/aasm/pull/661), thanks to [the-spectator](https://github.com/the-spectator)
10
+ * Handle InvalidTransition in transition_from matcher [#653](https://github.com/aasm/aasm/pull/653), thanks to [ryanwood](https://github.com/ryanwood)
11
+
5
12
  ## 5.0.6
6
13
 
7
14
  * Fix no_direct_assignment, couldn't be turned off pragmatically [#636](https://github.com/aasm/aasm/issues/636)
data/README.md CHANGED
@@ -436,12 +436,12 @@ job.aasm.current_state # stage3
436
436
  ### Multiple state machines per class
437
437
 
438
438
  Multiple state machines per class are supported. Be aware though that _AASM_ has been
439
- built with one state machine per class in mind. Nonetheless, here's how to do it:
439
+ built with one state machine per class in mind. Nonetheless, here's how to do it (see below). Please note that you will need to specify database columns for where your pertinent states will be stored - we have specified two columns `move_state` and `work_state` in the example below. See the [Column name & migration](https://github.com/aasm/aasm#column-name--migration) section for further info.
440
440
 
441
441
  ```ruby
442
442
  class SimpleMultipleExample
443
443
  include AASM
444
- aasm(:move) do
444
+ aasm(:move, column: 'move_state') do
445
445
  state :standing, initial: true
446
446
  state :walking
447
447
  state :running
@@ -457,7 +457,7 @@ class SimpleMultipleExample
457
457
  end
458
458
  end
459
459
 
460
- aasm(:work) do
460
+ aasm(:work, column: 'work_state') do
461
461
  state :sleeping, initial: true
462
462
  state :processing
463
463
 
@@ -1065,11 +1065,11 @@ this by defining your favorite column name, using `:column` like this:
1065
1065
  class Job < ActiveRecord::Base
1066
1066
  include AASM
1067
1067
 
1068
- aasm column: 'my_state' do
1068
+ aasm column: :my_state do
1069
1069
  ...
1070
1070
  end
1071
1071
 
1072
- aasm :another_state_machine, column: 'second_state' do
1072
+ aasm :another_state_machine, column: :second_state do
1073
1073
  ...
1074
1074
  end
1075
1075
  end
@@ -1141,6 +1141,10 @@ job = Job.new
1141
1141
  job.aasm.states(permitted: true).map(&:name)
1142
1142
  #=> [:running]
1143
1143
 
1144
+ # List all the permitted transitions(event and state pairs) from initial state
1145
+ job.aasm.permitted_transitions
1146
+ #=> [{ :event => :run, :state => :running }]
1147
+
1144
1148
  job.run
1145
1149
  job.aasm.states(permitted: true).map(&:name)
1146
1150
  #=> [:sleeping]
@@ -17,6 +17,7 @@ Gem::Specification.new do |s|
17
17
  s.required_ruby_version = '>= 1.9.3'
18
18
 
19
19
  s.add_dependency 'concurrent-ruby', '~> 1.0'
20
+ s.add_dependency 'after_commit_action', '~> 1.0'
20
21
 
21
22
  s.add_development_dependency 'rake'
22
23
  s.add_development_dependency 'sdoc'
@@ -2,7 +2,7 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "sqlite3", "~> 1.3.5", platforms: :ruby
5
+ gem "sqlite3", "~> 1.3", ">= 1.3.5", platforms: :ruby
6
6
  gem "rails", install_if: false
7
7
  gem "sequel"
8
8
  gem "redis-objects"
@@ -78,6 +78,17 @@ module AASM
78
78
  events
79
79
  end
80
80
 
81
+ def permitted_transitions
82
+ events(permitted: true).flat_map do |event|
83
+ available_transitions = event.transitions_from_state(current_state)
84
+ allowed_transitions = available_transitions.select { |t| t.allowed?(@instance) }
85
+
86
+ allowed_transitions.map do |transition|
87
+ { event: event.name, state: transition.to }
88
+ end
89
+ end
90
+ end
91
+
81
92
  def state_object_for_name(name)
82
93
  obj = @instance.class.aasm(@name).states.find {|s| s.name == name}
83
94
  raise AASM::UndefinedState, "State :#{name} doesn't exist" if obj.nil?
@@ -1,3 +1,4 @@
1
+ require 'after_commit_action'
1
2
  require 'aasm/persistence/orm'
2
3
  module AASM
3
4
  module Persistence
@@ -28,6 +29,7 @@ module AASM
28
29
  # end
29
30
  #
30
31
  def self.included(base)
32
+ base.send(:include, ::AfterCommitAction) unless base.include?(::AfterCommitAction)
31
33
  base.send(:include, AASM::Persistence::Base)
32
34
  base.send(:include, AASM::Persistence::ORM)
33
35
  base.send(:include, AASM::Persistence::ActiveRecordPersistence::InstanceMethods)
@@ -88,6 +90,12 @@ module AASM
88
90
  end
89
91
  end
90
92
 
93
+ def aasm_execute_after_commit
94
+ execute_after_commit do
95
+ yield
96
+ end
97
+ end
98
+
91
99
  def aasm_enum(name=:default)
92
100
  case AASM::StateMachineStore.fetch(self.class, true).machine(name).config.enum
93
101
  when false then nil
@@ -81,6 +81,10 @@ module AASM
81
81
  true
82
82
  end
83
83
 
84
+ def aasm_execute_after_commit
85
+ yield
86
+ end
87
+
84
88
  def aasm_write_state_attribute(state, name=:default)
85
89
  aasm_write_attribute(self.class.aasm(name).attribute_name, aasm_raw_attribute_value(state, name))
86
90
  end
@@ -116,32 +120,32 @@ module AASM
116
120
 
117
121
  # Returns true if event was fired successfully and transaction completed.
118
122
  def aasm_fire_event(state_machine_name, name, options, *args, &block)
119
- if aasm_supports_transactions? && options[:persist]
120
- event = self.class.aasm(state_machine_name).state_machine.events[name]
121
- event.fire_callbacks(:before_transaction, self, *args)
122
- event.fire_global_callbacks(:before_all_transactions, self, *args)
123
-
124
- begin
125
- success = if options[:persist] && use_transactions?(state_machine_name)
126
- aasm_transaction(requires_new?(state_machine_name), requires_lock?(state_machine_name)) do
127
- super
128
- end
129
- else
123
+ return super unless aasm_supports_transactions? && options[:persist]
124
+
125
+ event = self.class.aasm(state_machine_name).state_machine.events[name]
126
+ event.fire_callbacks(:before_transaction, self, *args)
127
+ event.fire_global_callbacks(:before_all_transactions, self, *args)
128
+
129
+ begin
130
+ success = if options[:persist] && use_transactions?(state_machine_name)
131
+ aasm_transaction(requires_new?(state_machine_name), requires_lock?(state_machine_name)) do
130
132
  super
131
133
  end
134
+ else
135
+ super
136
+ end
132
137
 
133
- if success
138
+ if success
139
+ aasm_execute_after_commit do
134
140
  event.fire_callbacks(:after_commit, self, *args)
135
141
  event.fire_global_callbacks(:after_all_commits, self, *args)
136
142
  end
137
-
138
- success
139
- ensure
140
- event.fire_callbacks(:after_transaction, self, *args)
141
- event.fire_global_callbacks(:after_all_transactions, self, *args)
142
143
  end
143
- else
144
- super
144
+
145
+ success
146
+ ensure
147
+ event.fire_callbacks(:after_transaction, self, *args)
148
+ event.fire_global_callbacks(:after_all_transactions, self, *args)
145
149
  end
146
150
  end
147
151
 
@@ -2,7 +2,11 @@ RSpec::Matchers.define :transition_from do |from_state|
2
2
  match do |obj|
3
3
  @state_machine_name ||= :default
4
4
  obj.aasm(@state_machine_name).current_state = from_state.to_sym
5
- obj.send(@event, *@args) && obj.aasm(@state_machine_name).current_state == @to_state.to_sym
5
+ begin
6
+ obj.send(@event, *@args) && obj.aasm(@state_machine_name).current_state == @to_state.to_sym
7
+ rescue AASM::InvalidTransition
8
+ false
9
+ end
6
10
  end
7
11
 
8
12
  chain :on do |state_machine_name|
@@ -1,3 +1,3 @@
1
1
  module AASM
2
- VERSION = "5.0.6"
2
+ VERSION = "5.0.7"
3
3
  end
@@ -3,11 +3,17 @@ class SimpleExample
3
3
  aasm do
4
4
  state :initialised, :initial => true
5
5
  state :filled_out
6
+ state :denied
6
7
  state :authorised
7
8
 
8
9
  event :fill_out do
9
10
  transitions :from => :initialised, :to => :filled_out
10
11
  end
12
+
13
+ event :deny do
14
+ transitions from: :initialised, to: :denied
15
+ end
16
+
11
17
  event :authorise do
12
18
  transitions :from => :filled_out, :to => :authorised
13
19
  end
@@ -613,6 +613,38 @@ if defined?(ActiveRecord)
613
613
  expect(validator).to be_running
614
614
  expect(validator.name).to eq("name")
615
615
  end
616
+
617
+ context "nested transaction" do
618
+ it "should fire :after_commit if root transaction was successful" do
619
+ validator = Validator.create(:name => 'name')
620
+ expect(validator).to be_sleeping
621
+
622
+ validator.transaction do
623
+ validator.run!
624
+ expect(validator.name).to eq("name")
625
+ expect(validator).to be_running
626
+ end
627
+
628
+ expect(validator.name).to eq("name changed")
629
+ expect(validator.reload).to be_running
630
+ end
631
+ end
632
+
633
+ it "should not fire :after_commit if root transaction failed" do
634
+ validator = Validator.create(:name => 'name')
635
+ expect(validator).to be_sleeping
636
+
637
+ validator.transaction do
638
+ validator.run!
639
+ expect(validator.name).to eq("name")
640
+ expect(validator).to be_running
641
+
642
+ raise ActiveRecord::Rollback, "failed on purpose"
643
+ end
644
+
645
+ expect(validator.name).to eq("name")
646
+ expect(validator.reload).to be_sleeping
647
+ end
616
648
  end
617
649
 
618
650
  describe 'before and after transaction callbacks' do
@@ -8,14 +8,17 @@ describe 'state machine' do
8
8
  it "works for simple state machines" do
9
9
  expect(simple).to transition_from(:initialised).to(:filled_out).on_event(:fill_out)
10
10
  expect(simple).to_not transition_from(:initialised).to(:authorised).on_event(:fill_out)
11
+ expect(simple).to_not transition_from(:authorised).to(:filled_out).on_event(:fill_out)
11
12
  end
12
13
 
13
14
  it "works for multiple state machines" do
14
15
  expect(multiple).to transition_from(:standing).to(:walking).on_event(:walk).on(:move)
15
16
  expect(multiple).to_not transition_from(:standing).to(:running).on_event(:walk).on(:move)
17
+ expect(multiple).to_not transition_from(:running).to(:walking).on_event(:walk).on(:move)
16
18
 
17
19
  expect(multiple).to transition_from(:sleeping).to(:processing).on_event(:start).on(:work)
18
20
  expect(multiple).to_not transition_from(:sleeping).to(:sleeping).on_event(:start).on(:work)
21
+ expect(multiple).to_not transition_from(:processing).to(:sleeping).on_event(:start).on(:work)
19
22
  end
20
23
  end
21
24
 
@@ -23,6 +23,21 @@ describe 'state machine' do
23
23
  expect(simple).to be_authorised
24
24
  end
25
25
 
26
+ it 'shows the permitted transitions' do
27
+ expect(simple.aasm.permitted_transitions).to eq(
28
+ [
29
+ { event: :fill_out, state: :filled_out },
30
+ { event: :deny, state: :denied }
31
+ ]
32
+ )
33
+
34
+ simple.fill_out!
35
+ expect(simple.aasm.permitted_transitions).to eq([{ event: :authorise, state: :authorised }])
36
+
37
+ simple.authorise
38
+ expect(simple.aasm.permitted_transitions).to eq([])
39
+ end
40
+
26
41
  it 'denies transitions to other states' do
27
42
  expect {simple.authorise}.to raise_error(AASM::InvalidTransition)
28
43
  expect {simple.authorise!}.to raise_error(AASM::InvalidTransition)
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: 5.0.6
4
+ version: 5.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thorsten Boettger
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2019-09-04 00:00:00.000000000 Z
12
+ date: 2020-03-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: concurrent-ruby
@@ -25,6 +25,20 @@ dependencies:
25
25
  - - "~>"
26
26
  - !ruby/object:Gem::Version
27
27
  version: '1.0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: after_commit_action
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '1.0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '1.0'
28
42
  - !ruby/object:Gem::Dependency
29
43
  name: rake
30
44
  requirement: !ruby/object:Gem::Requirement