has_state_machine 1.0.0 → 1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7aad95d7b489183fa7a7c49af178871cb5cc169dbc3a9ae6bd4a93f7ce48ebea
4
- data.tar.gz: 180dbc281d868326a908955013b17fcc935e54de43dbb367212f017e642b994d
3
+ metadata.gz: 902498121bd57b97de373beaa68557c3591de8a221d524af5b9033be1cca8a73
4
+ data.tar.gz: fb4806d204772d488c4fb2bc69360cd989edf4c8a5312508b37868d961adf059
5
5
  SHA512:
6
- metadata.gz: 1eca09471018292e2cc4f41a6a53ec2cf12bc087de32af80595901099a4b589131b529bbf53891de89eacc86c5a9a3c16ca7102f31faec63443003c697c45a7e
7
- data.tar.gz: 1b5a1e0a93e9f0648deac92b4fcba653ffeec037e9b3959092e71b5f9b391c93118e352ced5e7c1f44f385ceed1130ab62f1fde11dc4282ea2fb17097e66d47a
6
+ metadata.gz: 1baeee9d74f64325427b52e2e867cab05db7848f384316b8175cbb5d49a2c4030dcf905020ac8091c495f6cfa20f8dcae657bcffd78b86987e9c867665f5cf42
7
+ data.tar.gz: 7e998865b342452f802266b3408bd673b26fb622672ed48ff18cb384259c34a7ec1f591a9ed99efb12f33364e5a1c9bff33d9b95bd4fbac3794ff9d3f2ccdd25
data/README.md CHANGED
@@ -96,6 +96,13 @@ module Workflow
96
96
  # after_transition callbacks as well.
97
97
  Rails.logger.info "== Transitioned from #{previous_state} ==\n"
98
98
  end
99
+
100
+ # after_transition_commit runs only once the transition has been
101
+ # committed: after the record is saved for normal transitions, and
102
+ # outside the transaction for transactional transitions (see below).
103
+ after_transition_commit do
104
+ MyJob.perform_later(object)
105
+ end
99
106
  end
100
107
  end
101
108
  ```
@@ -206,10 +213,20 @@ module Workflow
206
213
  rollback_transition unless notified_watchers?
207
214
  end
208
215
 
216
+ after_transition_commit do
217
+ enqueue_external_work
218
+ end
219
+
209
220
  private
210
221
 
222
+ def enqueue_external_work
223
+ # Any work you want to happen only after the transition is committed.
224
+ # Enqueuing a job, calling an external API, sending a webhook, etc.
225
+ end
226
+
211
227
  def notified_watchers?
212
- #...
228
+ # Any dependent work that you want to run that should play a part in determining
229
+ # whether the transition was successful or not and needs to be rolled back.
213
230
  end
214
231
  end
215
232
  end
@@ -15,6 +15,11 @@ module HasStateMachine
15
15
  # for use on a HasStateMachine::State instance.
16
16
  define_model_callbacks :transition, only: %i[before after]
17
17
 
18
+ ##
19
+ # Defines the after_transition_commit callback. It runs only after a
20
+ # transition has committed.
21
+ define_model_callbacks :transition_commit, only: %i[after]
22
+
18
23
  ##
19
24
  # possible_transitions - Retrieves the next available transitions for a given state.
20
25
  # transactional? - Determines whether or not the transition should happen with a transactional block.
@@ -80,25 +85,34 @@ module HasStateMachine
80
85
 
81
86
  ##
82
87
  # Makes the actual transition from one state to the next and
83
- # runs the before and after transition callbacks.
88
+ # runs the before and after transition callbacks. The
89
+ # after_transition_commit callbacks run after the update completes
90
+ # and only when it succeeds.
84
91
  def perform_transition!
85
- run_callbacks :transition do
86
- object.update("#{object.state_attribute}": state)
92
+ run_callbacks :transition_commit do
93
+ run_callbacks :transition do
94
+ object.update("#{object.state_attribute}": state)
95
+ end
87
96
  end
88
97
  end
89
98
 
90
99
  ##
91
100
  # Makes the actual transition from one state to the next and
92
101
  # runs the before and after transition callbacks in a transaction
93
- # to allow for roll backs.
102
+ # to allow for roll backs. The after_transition_commit callbacks run
103
+ # outside the transaction and only when it commits (not on rollback).
94
104
  def perform_transactional_transition!
95
- ActiveRecord::Base.transaction(requires_new: true, joinable: false) do
96
- run_callbacks :transition do
97
- rollback_transition unless object.update("#{object.state_attribute}": state)
105
+ run_callbacks :transition_commit do
106
+ ActiveRecord::Base.transaction(requires_new: true, joinable: false) do
107
+ run_callbacks :transition do
108
+ rollback_transition unless object.update("#{object.state_attribute}": state)
109
+ end
98
110
  end
99
- end
100
111
 
101
- object.reload.public_send(object.state_attribute) == state
112
+ @previous_state = previous_state
113
+
114
+ object.reload.public_send(object.state_attribute) == state
115
+ end
102
116
  end
103
117
 
104
118
  private
@@ -112,7 +126,7 @@ module HasStateMachine
112
126
  # it has been transitioned to the new state. Useful in
113
127
  # after_transition blocks
114
128
  def previous_state
115
- object.previous_changes[object.state_attribute]&.first
129
+ @previous_state.presence || object.previous_changes[object.state_attribute]&.first
116
130
  end
117
131
 
118
132
  def state_instance(desired_state, transient_values)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HasStateMachine
4
- VERSION = "1.0.0"
4
+ VERSION = "1.1.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_state_machine
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benjamin Hargett
@@ -94,6 +94,20 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: minitest
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '5.1'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '5.1'
97
111
  description: HasStateMachine uses ruby classes to make creating a finite state machine
98
112
  in your ActiveRecord models a breeze.
99
113
  email: