state_machine_job 3.0.0 → 3.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: 6a7f675ec564eb009278abd23d975cc4a90a8227547f4a6b52d892ff68ea9a09
4
- data.tar.gz: 3edbe769acda3301a5727296f9658d7dfd38733b04e95e66033e0c0f8e8c2db2
3
+ metadata.gz: 1da48595f1508f1bcadd9c3f342893b1b2e902df7768c2bc84cde8047581b150
4
+ data.tar.gz: 46fe342256ddab39e39c233cd8036b60c8bb73cac2f1a9b456e1a92aca3965c7
5
5
  SHA512:
6
- metadata.gz: ae3afe431ed7a0b271ba445a613f57b0bb065c77cc0f8f4c97031ca82d2e9ee44cb1496ecff65accc838d9706445a589f9c5148682f4d3e9a55cfe40aa311c7a
7
- data.tar.gz: 8b4bc3f88b24bee08753119de4aca55e03cee70a7d883365d9d7ebbfeaa2a65476c58c8353ff270bd5fa14eb0c05512b034fdeb73d494df0cfa2c1d32602ce17
6
+ metadata.gz: dce23a6e924e89b36c9a1d745be3f47643a058e0ac27337ac288bd348df876a3d6d51abc99311da82ab35e6e947e2fda88994566f2cfd6a0db967a6e86670bd3
7
+ data.tar.gz: 6cb757643ff20b9a18e29a875ca7f059a9cf6860faa40dea07f69384f2a9107a26638f18e2b02786c7dbde0777bafa9a12a47c0547697c072bf122bfa1a3cf37
@@ -1,14 +1,14 @@
1
1
  # CHANGELOG
2
2
 
3
- ### Version 3.0.0
3
+ ### Version 3.1.0
4
4
 
5
- 2018-07-31
5
+ 2019-07-22
6
6
 
7
- [Compare changes](http://github.com/codevise/state_machine_job/compare/2-x-stable...v3.0.0)
7
+ [Compare changes](http://github.com/codevise/state_machine_job/compare/3-0-stable...v3.1.0)
8
8
 
9
- - Migrate to `state_machines` gem
10
- ([#10](https://github.com/codevise/state_machine_job/pull/10))
9
+ - Ensure error state can be persisted if job leaves record invalid.
10
+ ([#11](https://github.com/codevise/state_machine_job/pull/11))
11
11
 
12
12
  See
13
- [2-x-stable branch](http://github.com/codevise/state_machine_job/blob/2-x-stable/CHANGELOG.md)
13
+ [3-0-stable branch](http://github.com/codevise/state_machine_job/blob/3-0-stable/CHANGELOG.md)
14
14
  for previous changes.
data/README.md CHANGED
@@ -56,9 +56,15 @@ When the `state` attribute changes to `'running'` (either by the `run`
56
56
  event or by manually updateing the attribute), `SomeJob` will
57
57
  automatically be enqueued. If `perform_with_result` returns `:ok`, the
58
58
  state machine transitions to the `'done'` state. You can specify as
59
- many results as you want. Note that any exception raised by
60
- `perform_with_result` leads to a state machine transition as if the
61
- result had been `:error`. The exception is not rescued, though.
59
+ many results as you want.
60
+
61
+ Note that any exception raised by `perform_with_result` leads to a
62
+ state machine transition as if the result had been `:error`. The
63
+ exception is not rescued, though. If `perform_with_result` raises an
64
+ exception and the record is invalid, previous attribute values will be
65
+ restored before invoking the transition. That way the transition to
66
+ the error state can be persisted by rolling back the changes that led
67
+ to the records invalidity during job execution.
62
68
 
63
69
  ### Passing custom Payload
64
70
 
@@ -9,6 +9,7 @@ module StateMachineJob
9
9
  begin
10
10
  result = perform_with_result(record, payload)
11
11
  rescue StandardError
12
+ record.restore_attributes unless record.valid?
12
13
  result = :error
13
14
  raise
14
15
  ensure
@@ -1,3 +1,3 @@
1
1
  module StateMachineJob
2
- VERSION = '3.0.0'.freeze
2
+ VERSION = '3.1.0'.freeze
3
3
  end
@@ -8,6 +8,9 @@ describe StateMachineJob do
8
8
  end
9
9
 
10
10
  class Model
11
+ include ActiveModel::Dirty
12
+ include ActiveModel::Validations
13
+
11
14
  def id
12
15
  3
13
16
  end
@@ -17,6 +20,23 @@ describe StateMachineJob do
17
20
  def test_job_error!; end
18
21
  end
19
22
 
23
+ class ModelRequiringName < Model
24
+ attr_reader :name
25
+
26
+ validates_presence_of :name
27
+ define_attribute_methods :name
28
+
29
+ def initialize(name: nil)
30
+ @name = name
31
+ changes_applied
32
+ end
33
+
34
+ def name=(value)
35
+ name_will_change! unless value == @name
36
+ @name = value
37
+ end
38
+ end
39
+
20
40
  class SomeError < StandardError; end
21
41
 
22
42
  it 'passes record and payload to perform_with_result method' do
@@ -61,4 +81,20 @@ describe StateMachineJob do
61
81
  rescue SomeError # rubocop:disable Lint/HandleExceptions
62
82
  end
63
83
  end
84
+
85
+ it 'restores attributes of invalid record before invoking job result event ' \
86
+ 'if perform_with_result raises to ensure state transition can be persisted' do
87
+ record = ModelRequiringName.new(name: 'Susan')
88
+ job = TestJob.new
89
+
90
+ record.name = ''
91
+ allow(job).to receive(:perform_with_result).and_raise(SomeError)
92
+
93
+ begin
94
+ job.perform(record)
95
+ rescue SomeError # rubocop:disable Lint/HandleExceptions
96
+ ensure
97
+ expect(record).to be_valid
98
+ end
99
+ end
64
100
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: state_machine_job
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Codevise Solutions Ltd.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-07-31 00:00:00.000000000 Z
11
+ date: 2019-07-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: state_machines-activemodel