activerecord-transactionable 2.0.1 → 2.0.5
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 +5 -5
- data/.github/FUNDING.yml +12 -0
- data/.github/dependabot.yml +8 -0
- data/.github/workflows/eol.yml +37 -0
- data/.github/workflows/style.yml +34 -0
- data/.github/workflows/supported.yml +57 -0
- data/.github/workflows/unsupported.yml +40 -0
- data/.gitignore +2 -1
- data/.rspec +1 -0
- data/.rubocop.yml +121 -0
- data/.rubocop_todo.yml +138 -0
- data/.simplecov +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/CONTRIBUTING.md +26 -0
- data/Gemfile +4 -3
- data/LICENSE +21 -0
- data/README.md +152 -40
- data/Rakefile +22 -3
- data/SECURITY.md +14 -0
- data/activerecord-transactionable.gemspec +42 -15
- data/bin/console +1 -0
- data/lib/activerecord/transactionable/result.rb +32 -4
- data/lib/activerecord/transactionable/version.rb +3 -1
- data/lib/activerecord/transactionable.rb +104 -79
- metadata +211 -39
- data/.coveralls.yml +0 -1
- data/.travis.yml +0 -20
- data/Appraisals +0 -34
- data/gemfiles/rails_4_0.gemfile +0 -9
- data/gemfiles/rails_4_1.gemfile +0 -9
- data/gemfiles/rails_4_2.gemfile +0 -9
- data/gemfiles/rails_5_0.gemfile +0 -9
- data/gemfiles/rails_5_1.gemfile +0 -9
data/README.md
CHANGED
@@ -1,23 +1,16 @@
|
|
1
1
|
# Activerecord::Transactionable
|
2
2
|
|
3
|
-
Provides a method, `transaction_wrapper` at the class and instance levels that can be used instead of `ActiveRecord#transaction`. Enables you to do transactions properly, including with or without locking.
|
4
|
-
|
5
|
-
| Project
|
6
|
-
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
| inline documenation | [](http://inch-ci.org/github/pboling/activerecord-transactionable) |
|
15
|
-
| continuous integration | [](https://travis-ci.org/pboling/activerecord-transactionable) |
|
16
|
-
| test coverage | [](https://coveralls.io/github/pboling/activerecord-transactionable?branch=master) |
|
17
|
-
| homepage | [https://github.com/pboling/activerecord-transactionable][homepage] |
|
18
|
-
| documentation | [http://rdoc.info/github/pboling/activerecord-transactionable/frames][documentation] |
|
19
|
-
| live chat | [](https://gitter.im/pboling/activerecord-transactionable?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) |
|
20
|
-
| Spread ~♡ⓛⓞⓥⓔ♡~ | [on AngelList](https://angel.co/peter-boling), [on Coderwall](http://coderwall.com/pboling) |
|
3
|
+
Provides a method, `transaction_wrapper` at the class and instance levels that can be used instead of `ActiveRecord#transaction`. Enables you to do transactions properly, with custom rescues and retry, including with or without locking.
|
4
|
+
|
5
|
+
| Project | Activerecord::Transactionable |
|
6
|
+
|--------------------------- |--------------------------- |
|
7
|
+
| name, license, docs | [`activerecord-transactionable`][rubygems] [][license-ref] [][documentation] |
|
8
|
+
| version & downloads | [][rubygems] [][rubygems] [][rubygems] [][source] |
|
9
|
+
| dependencies & linting | [][depfu] [][actions] |
|
10
|
+
| unit tests | [][actions] [][actions] |
|
11
|
+
| coverage & maintainability | [][climate_coverage] [][codecov_coverage] [][climate_maintainability] [][maintenancee_policy] |
|
12
|
+
| resources | [][gh_discussions] [](https://www.codementor.io/peterboling?utm_source=github&utm_medium=button&utm_term=peterboling&utm_campaign=github) [][chat] [][blogpage] |
|
13
|
+
| Spread ~♡ⓛⓞⓥⓔ♡~ | [][code_triage] [][liberapay_donate] [][gh_sponsors] [🌏][aboutme] [👼][angelme] [💻][coderme] [🌹][politicme] [![Tweet @ Peter][followme-img]][tweetme] |
|
21
14
|
|
22
15
|
Useful as an example of correct behavior for wrapping transactions.
|
23
16
|
|
@@ -26,13 +19,15 @@ NOTE: Rails' transactions are per-database connection, not per-model, nor per-in
|
|
26
19
|
|
27
20
|
## Upgrading to Version 2
|
28
21
|
|
29
|
-
In version 1 the `transaction_wrapper` returned `true` or `false`. In version 2 it returns an instance of `Activerecord::Transactionable::Result`, which has a `value`, and
|
22
|
+
In version 1 the `transaction_wrapper` returned `true` or `false`. In version 2 it returns an instance of `Activerecord::Transactionable::Result`, which has a `value`, and three methods:
|
30
23
|
```ruby
|
31
|
-
|
24
|
+
args = {}
|
25
|
+
result = transaction_wrapper(**args) do
|
32
26
|
something
|
33
27
|
end
|
34
28
|
result.fail?
|
35
29
|
result.success?
|
30
|
+
result.to_h # => a hash with diagnostic information, particularly useful when things go wrong
|
36
31
|
```
|
37
32
|
Where you used to have:
|
38
33
|
```ruby
|
@@ -52,7 +47,7 @@ end
|
|
52
47
|
Add this line to your application's Gemfile:
|
53
48
|
|
54
49
|
```ruby
|
55
|
-
gem
|
50
|
+
gem "activerecord-transactionable"
|
56
51
|
```
|
57
52
|
|
58
53
|
And then execute:
|
@@ -63,6 +58,19 @@ Or install it yourself as:
|
|
63
58
|
|
64
59
|
$ gem install activerecord-transactionable
|
65
60
|
|
61
|
+
## Compatibility
|
62
|
+
|
63
|
+
Targeted ruby compatibility is non-EOL versions of Ruby, currently 2.6, 2.7, and
|
64
|
+
3.0. Ruby is limited to 2.1+ in the gemspec, and when it changes there will be a major release.
|
65
|
+
The `master` branch currently targets 2.0.x releases.
|
66
|
+
|
67
|
+
| Ruby OAuth Version | Maintenance Branch | Officially Supported Rubies | Unofficially Supported Rubies |
|
68
|
+
|--------------------- | ------------------ | ------------------------------------------- | ----------------------------- |
|
69
|
+
| 3.0.x | N/A | 2.7, 3.0, 3.1 | 2.6 |
|
70
|
+
| 2.0.x | `master` | 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 3.0 | |
|
71
|
+
|
72
|
+
NOTE: 2.0.5 is anticipated as last release of the 2.x series.
|
73
|
+
|
66
74
|
## Usage
|
67
75
|
|
68
76
|
```ruby
|
@@ -117,40 +125,35 @@ If you need to know if the transaction succeeded:
|
|
117
125
|
car = Car.new(name: nil)
|
118
126
|
result = car.transaction_wrapper(lock: true) do # uses ActiveRecord's with_lock
|
119
127
|
car.save!
|
120
|
-
|
128
|
+
end
|
121
129
|
result # => an instance of Activerecord::Transactionable::Result
|
122
130
|
result.success? # => true or false
|
123
131
|
```
|
124
132
|
|
125
|
-
Meanings of `transaction_wrapper` return values:
|
126
|
-
|
127
|
-
* **nil** - ActiveRecord::Rollback was raised, and then caught by the transaction, and not re-raised; the transaction failed.
|
128
|
-
* **false** - An error was raised which was handled by the transaction_wrapper; the transaction failed.
|
129
|
-
* **true** - The transaction was a success.
|
130
|
-
|
131
133
|
## Update Example
|
132
134
|
|
133
135
|
```ruby
|
134
136
|
@client = Client.find(params[:id])
|
135
|
-
transaction_result =
|
137
|
+
transaction_result = @client.transaction_wrapper(lock: true) do
|
136
138
|
@client.assign_attributes(client_params)
|
137
139
|
@client.save!
|
138
|
-
|
140
|
+
end
|
139
141
|
if transaction_result.success?
|
140
142
|
render :show, locals: { client: @client }, status: :ok
|
141
143
|
else
|
142
|
-
# Something prevented update
|
143
|
-
render json: @client.errors, status: :unprocessable_entity
|
144
|
+
# Something prevented update, transaction_result.to_h will have all the available details
|
145
|
+
render json: { record_errors: @client.errors, transaction_result: transaction_result.to_h }, status: :unprocessable_entity
|
144
146
|
end
|
145
147
|
```
|
146
148
|
|
147
149
|
## Find or create
|
148
150
|
|
149
|
-
NOTE: The `is_retry` is passed to the block by the gem, and indicates whether the block is running for the first time or the second time.
|
151
|
+
NOTE: The `is_retry` is passed to the block by the gem, and indicates whether the block is running for the first time or the second, or nth, time.
|
150
152
|
The block will never be retried more than once.
|
151
153
|
|
152
154
|
```ruby
|
153
|
-
Car.transaction_wrapper(outside_retriable_errors: ActivRecord::RecordNotFound) do |is_retry|
|
155
|
+
Car.transaction_wrapper(outside_retriable_errors: ActivRecord::RecordNotFound, outside_num_retry_attempts: 3) do |is_retry|
|
156
|
+
# is_retry will be falsey on first attempt, thereafter will be the integer number of the attempt
|
154
157
|
if is_retry
|
155
158
|
Car.create!(vin: vin)
|
156
159
|
else
|
@@ -166,6 +169,7 @@ The block will never be retried more than once.
|
|
166
169
|
|
167
170
|
```ruby
|
168
171
|
Car.transaction_wrapper(outside_retriable_errors: ActivRecord::RecordNotUnique) do |is_retry|
|
172
|
+
# is_retry will be falsey on first attempt, thereafter will be the integer number of the attempt
|
169
173
|
if is_retry
|
170
174
|
Car.find_by!(vin: vin)
|
171
175
|
else
|
@@ -186,8 +190,8 @@ module SendToRaygun
|
|
186
190
|
begin
|
187
191
|
Raygun.track_exception(args[:error])
|
188
192
|
Rails.logger.debug("Sent Error to Raygun: #{args[:error].class}: #{args[:error].message}")
|
189
|
-
rescue => e
|
190
|
-
Rails.logger.
|
193
|
+
rescue StandardError => e
|
194
|
+
Rails.logger.error("Sending Error #{args[:error].class}: #{args[:error].message} to Raygun Failed with: #{e.class}: #{e.message}")
|
191
195
|
end
|
192
196
|
end
|
193
197
|
end
|
@@ -198,6 +202,17 @@ Activerecord::Transactionable::ClassMethods.class_eval do
|
|
198
202
|
end
|
199
203
|
```
|
200
204
|
|
205
|
+
## More Information
|
206
|
+
|
207
|
+
* RubyDoc Documentation: [][documentation]
|
208
|
+
* GitHub Discussions: [][gh_discussions]
|
209
|
+
* Live Chat on Gitter: [][chat]
|
210
|
+
* Maintainer's Blog: [][blogpage]
|
211
|
+
|
212
|
+
## Code of Conduct
|
213
|
+
|
214
|
+
Everyone interacting with this project's code, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/pboling/activerecord-transactionable/blob/master/CODE_OF_CONDUCT.md).
|
215
|
+
|
201
216
|
## Development
|
202
217
|
|
203
218
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -206,11 +221,108 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
206
221
|
|
207
222
|
## Contributing
|
208
223
|
|
209
|
-
|
224
|
+
See [CONTRIBUTING.md][contributing]
|
225
|
+
|
226
|
+
## Contributors
|
227
|
+
|
228
|
+
[][contributors]
|
229
|
+
|
230
|
+
## Versioning
|
231
|
+
|
232
|
+
This library aims to adhere to [Semantic Versioning 2.0.0][semver].
|
233
|
+
Violations of this scheme should be reported as bugs. Specifically,
|
234
|
+
if a minor or patch version is released that breaks backward
|
235
|
+
compatibility, a new version should be immediately released that
|
236
|
+
restores compatibility. Breaking changes to the public API will
|
237
|
+
only be introduced with new major versions.
|
238
|
+
|
239
|
+
As a result of this policy, you can (and should) specify a
|
240
|
+
dependency on this gem using the [Pessimistic Version Constraint][pvc] with two digits of precision.
|
241
|
+
|
242
|
+
For example:
|
243
|
+
|
244
|
+
```ruby
|
245
|
+
spec.add_dependency "activerecord-transactionable", "~> 2.0"
|
246
|
+
```
|
247
|
+
|
248
|
+
## License
|
249
|
+
|
250
|
+
* Copyright (c) 2016 - 2018, 2021 [Peter H. Boling][peterboling] of [Rails Bling][railsbling]
|
251
|
+
|
252
|
+
[][license-ref]
|
253
|
+
|
254
|
+
[copyright-notice-explainer]: https://opensource.stackexchange.com/questions/5778/why-do-licenses-such-as-the-mit-license-specify-a-single-year
|
255
|
+
|
256
|
+
[license]: https://github.com/pboling/activerecord-transactionable/blob/master/LICENSE
|
210
257
|
|
211
258
|
[semver]: http://semver.org/
|
212
|
-
|
259
|
+
|
260
|
+
[pvc]: http://guides.rubygems.org/patterns/#pessimistic-version-constraint
|
261
|
+
|
213
262
|
[railsbling]: http://www.railsbling.com
|
263
|
+
|
214
264
|
[peterboling]: http://www.peterboling.com
|
215
|
-
|
216
|
-
[
|
265
|
+
|
266
|
+
[issues]: https://github.com/pboling/activerecord-transactionable/issues
|
267
|
+
|
268
|
+
[contributing]: https://github.com/pboling/activerecord-transactionable/blob/master/CONTRIBUTING.md
|
269
|
+
|
270
|
+
[comment]: <> (Following links are used by README, CONTRIBUTING)
|
271
|
+
|
272
|
+
[contributors]: https://github.com/pboling/activerecord-transactionable/graphs/contributors
|
273
|
+
|
274
|
+
[comment]: <> (Following links are used by README, CONTRIBUTING, Homepage)
|
275
|
+
|
276
|
+
[mailinglist]: http://groups.google.com/group/activerecord-transactionable-ruby
|
277
|
+
|
278
|
+
[source]: https://github.com/pboling/activerecord-transactionable/
|
279
|
+
|
280
|
+
[comment]: <> (Following links are used by Homepage)
|
281
|
+
|
282
|
+
[network]: https://github.com/pboling/activerecord-transactionable/network
|
283
|
+
|
284
|
+
[stargazers]: https://github.com/pboling/activerecord-transactionable/stargazers
|
285
|
+
|
286
|
+
[comment]: <> (Following links are used by README, Homepage)
|
287
|
+
|
288
|
+
[rubygems]: https://rubygems.org/gems/activerecord-transactionable
|
289
|
+
|
290
|
+
[depfu]: https://depfu.com/github/pboling/activerecord-transactionable?project_id=2653
|
291
|
+
|
292
|
+
[actions]: https://github.com/pboling/activerecord-transactionable/actions
|
293
|
+
|
294
|
+
[climate_coverage]: https://codeclimate.com/github/pboling/activerecord-transactionable/test_coverage
|
295
|
+
|
296
|
+
[gh_discussions]: https://github.com/pboling/activerecord-transactionable/discussions
|
297
|
+
|
298
|
+
[code_triage]: https://www.codetriage.com/pboling/activerecord-transactionable
|
299
|
+
|
300
|
+
[license-ref]: https://opensource.org/licenses/MIT
|
301
|
+
|
302
|
+
[codecov_coverage]: https://codecov.io/gh/pboling/activerecord-transactionable
|
303
|
+
|
304
|
+
[liberapay_donate]: https://liberapay.com/pboling/donate
|
305
|
+
|
306
|
+
[aboutme]: https://about.me/peter.boling
|
307
|
+
|
308
|
+
[angelme]: https://angel.co/peter-boling
|
309
|
+
|
310
|
+
[coderme]:http://coderwall.com/pboling
|
311
|
+
|
312
|
+
[politicme]: https://nationalprogressiveparty.org
|
313
|
+
|
314
|
+
[followme-img]: https://img.shields.io/twitter/follow/galtzo.svg?style=social&label=Follow
|
315
|
+
|
316
|
+
[tweetme]: http://twitter.com/galtzo
|
317
|
+
|
318
|
+
[documentation]: https://rubydoc.info/github/pboling/activerecord-transactionable
|
319
|
+
|
320
|
+
[climate_maintainability]: https://codeclimate.com/github/pboling/activerecord-transactionable/maintainability
|
321
|
+
|
322
|
+
[chat]: https://gitter.im/pboling/activerecord-transactionable?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge
|
323
|
+
|
324
|
+
[blogpage]: http://www.railsbling.com/tags/activerecord-transactionable/
|
325
|
+
|
326
|
+
[maintenancee_policy]: https://guides.rubyonrails.org/maintenance_policy.html#security-issues
|
327
|
+
|
328
|
+
[gh_sponsors]: https://github.com/sponsors/pboling
|
data/Rakefile
CHANGED
@@ -1,6 +1,25 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
%w[
|
4
|
+
bundler/gem_tasks
|
5
|
+
rake/testtask
|
6
|
+
rspec/core/rake_task
|
7
|
+
fileutils
|
8
|
+
].each { |f| require f }
|
9
|
+
|
10
|
+
Bundler::GemHelper.install_tasks
|
3
11
|
|
4
12
|
RSpec::Core::RakeTask.new(:spec)
|
13
|
+
desc "alias spec => test"
|
14
|
+
task test: :spec
|
15
|
+
|
16
|
+
begin
|
17
|
+
require "rubocop/rake_task"
|
18
|
+
RuboCop::RakeTask.new
|
19
|
+
rescue LoadError
|
20
|
+
task :rubocop do
|
21
|
+
warn "RuboCop is disabled on Ruby #{RUBY_VERSION}"
|
22
|
+
end
|
23
|
+
end
|
5
24
|
|
6
|
-
task :
|
25
|
+
task default: %i[spec rubocop]
|
data/SECURITY.md
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# Security Policy
|
2
|
+
|
3
|
+
## Supported Versions
|
4
|
+
|
5
|
+
| Version | Supported |
|
6
|
+
| ------- | ------------------ |
|
7
|
+
| 3.0.x | :white_check_mark: |
|
8
|
+
| 2.0.x | :white_check_mark: |
|
9
|
+
| 1.0.x | :x: |
|
10
|
+
| 0.0.x | :x: |
|
11
|
+
|
12
|
+
## Reporting a Vulnerability
|
13
|
+
|
14
|
+
Peter Boling is the primary maintainer of this gem. Please find a way to [contact him directly](https://railsbling.com/contact) to report the issue. Include as much relevant information as possible.
|
@@ -1,7 +1,8 @@
|
|
1
|
-
#
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path("lib", __dir__)
|
3
4
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require
|
5
|
+
require "activerecord/transactionable/version"
|
5
6
|
|
6
7
|
Gem::Specification.new do |spec|
|
7
8
|
spec.name = "activerecord-transactionable"
|
@@ -10,24 +11,50 @@ Gem::Specification.new do |spec|
|
|
10
11
|
spec.email = ["peter.boling@gmail.com"]
|
11
12
|
spec.licenses = ["MIT"]
|
12
13
|
|
13
|
-
spec.summary =
|
14
|
-
spec.description =
|
14
|
+
spec.summary = "Do ActiveRecord transactions the right way."
|
15
|
+
spec.description = "Getting transactions right is hard, and this gem makes it easier."
|
15
16
|
spec.homepage = "http://www.railsbling.com/tags/activerecord-transactionable"
|
16
17
|
|
17
18
|
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
19
|
spec.bindir = "exe"
|
19
20
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
21
|
spec.require_paths = ["lib"]
|
21
|
-
spec.required_ruby_version = ">= 2.1.0"
|
22
|
+
spec.required_ruby_version = ">= 2.1.0"
|
23
|
+
|
24
|
+
ruby_version = Gem::Version.new(RUBY_VERSION)
|
25
|
+
minimum_version = ->(version) { ruby_version >= Gem::Version.new(version) && RUBY_ENGINE == "ruby" }
|
26
|
+
linting = minimum_version.call("2.6")
|
27
|
+
coverage = minimum_version.call("2.6")
|
28
|
+
good_sqlite_constraint = minimum_version.call("2.5")
|
29
|
+
debug = minimum_version.call("2.4")
|
22
30
|
|
23
|
-
spec.add_dependency "activemodel"
|
24
|
-
spec.add_dependency "activerecord"
|
31
|
+
spec.add_dependency "activemodel", ">= 4.0.0"
|
32
|
+
spec.add_dependency "activerecord", ">= 4.0.0"
|
25
33
|
|
26
|
-
spec.add_development_dependency "
|
27
|
-
spec.add_development_dependency "
|
28
|
-
spec.add_development_dependency "
|
29
|
-
spec.add_development_dependency "
|
30
|
-
spec.add_development_dependency "
|
31
|
-
spec.add_development_dependency "
|
32
|
-
spec.add_development_dependency "
|
34
|
+
spec.add_development_dependency "byebug", "~> 11.1" if debug
|
35
|
+
spec.add_development_dependency "factory_bot", ">= 4.0"
|
36
|
+
spec.add_development_dependency "rake", ">= 12.0"
|
37
|
+
spec.add_development_dependency "rspec", "~> 3.10"
|
38
|
+
spec.add_development_dependency "rspec-benchmark", "~> 0.6"
|
39
|
+
spec.add_development_dependency "rspec-block_is_expected", "~> 1.0"
|
40
|
+
spec.add_development_dependency "rspec-pending_for", "~> 0.1"
|
41
|
+
if linting
|
42
|
+
spec.add_development_dependency("rubocop", "~> 1.22")
|
43
|
+
spec.add_development_dependency("rubocop-md", "~> 1.0")
|
44
|
+
spec.add_development_dependency("rubocop-minitest", "~> 0.15")
|
45
|
+
spec.add_development_dependency("rubocop-packaging", "~> 0.5")
|
46
|
+
spec.add_development_dependency("rubocop-performance", "~> 1.11")
|
47
|
+
spec.add_development_dependency("rubocop-rake", "~> 0.6")
|
48
|
+
spec.add_development_dependency("rubocop-rspec", "~> 2.5")
|
49
|
+
spec.add_development_dependency("rubocop-thread_safety", "~> 0.4")
|
50
|
+
end
|
51
|
+
if coverage
|
52
|
+
spec.add_development_dependency("simplecov", "~> 0.21")
|
53
|
+
spec.add_development_dependency("simplecov-cobertura", "~> 1.4")
|
54
|
+
end
|
55
|
+
# sqlite version constraint: https://stackoverflow.com/a/54729071/213191
|
56
|
+
# TODO: Relax and update the constraint when dropping support for <= activerecord 5.2.2
|
57
|
+
# NOTE: Ruby 2.5+ will install Rails 6+
|
58
|
+
spec.add_development_dependency "sqlite3", good_sqlite_constraint ? "~> 1" : "~> 1.3.6"
|
59
|
+
spec.add_development_dependency "yard", ">= 0.9.20"
|
33
60
|
end
|
data/bin/console
CHANGED
@@ -1,18 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Activerecord
|
2
4
|
module Transactionable
|
3
5
|
class Result
|
4
|
-
attr_reader :value
|
5
|
-
|
6
|
+
attr_reader :value, :result, :error, :type, :context, :nested, :attempt
|
7
|
+
|
8
|
+
def initialize(value, context:, transaction_open:, attempt:, error: nil, type: nil)
|
6
9
|
@value = value
|
10
|
+
@result = fail? ? "fail" : "success"
|
11
|
+
@context = context
|
12
|
+
@nested = transaction_open ? true : false
|
13
|
+
@attempt = attempt
|
14
|
+
@error = error
|
15
|
+
@type = type
|
7
16
|
end
|
8
|
-
|
17
|
+
|
9
18
|
def fail?
|
10
19
|
value == false
|
11
20
|
end
|
12
|
-
|
21
|
+
|
13
22
|
def success?
|
14
23
|
value == true
|
15
24
|
end
|
25
|
+
|
26
|
+
def to_h(skip_error: nil)
|
27
|
+
diagnostic_data = {
|
28
|
+
result: result,
|
29
|
+
type: type,
|
30
|
+
context: context,
|
31
|
+
nested: nested,
|
32
|
+
attempt: attempt
|
33
|
+
}
|
34
|
+
if !skip_error && error
|
35
|
+
diagnostic_data[:error] = error.class.to_s
|
36
|
+
diagnostic_data[:message] = error.message
|
37
|
+
end
|
38
|
+
diagnostic_data
|
39
|
+
end
|
40
|
+
|
41
|
+
def to_s(skip_error: nil)
|
42
|
+
to_h(skip_error: skip_error).to_s
|
43
|
+
end
|
16
44
|
end
|
17
45
|
end
|
18
46
|
end
|