activerecord-transactionable 2.0.1 → 2.0.5

Sign up to get free protection for your applications and to get access to all the features.
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 | Activerecord::Transactionable |
6
- |------------------------ | ----------------- |
7
- | gem name | activerecord-transactionable |
8
- | license | [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) |
9
- | expert support | [![Get help on Codementor](https://cdn.codementor.io/badges/get_help_github.svg)](https://www.codementor.io/peterboling?utm_source=github&utm_medium=button&utm_term=peterboling&utm_campaign=github) |
10
- | download rank | [![Total Downloads](https://img.shields.io/gem/rt/activerecord-transactionable.svg)](https://rubygems.org/gems/activerecord-transactionable) |
11
- | version | [![Gem Version](https://badge.fury.io/rb/activerecord-transactionable.png)](http://badge.fury.io/rb/activerecord-transactionable) |
12
- | dependencies | [![Dependency Status](https://gemnasium.com/pboling/activerecord-transactionable.png)](https://gemnasium.com/pboling/activerecord-transactionable) |
13
- | code quality | [![Code Climate](https://codeclimate.com/github/pboling/activerecord-transactionable.png)](https://codeclimate.com/github/pboling/activerecord-transactionable) |
14
- | inline documenation | [![Inline docs](http://inch-ci.org/github/pboling/activerecord-transactionable.png)](http://inch-ci.org/github/pboling/activerecord-transactionable) |
15
- | continuous integration | [![Build Status](https://secure.travis-ci.org/pboling/activerecord-transactionable.png?branch=master)](https://travis-ci.org/pboling/activerecord-transactionable) |
16
- | test coverage | [![Coverage Status](https://coveralls.io/repos/github/pboling/activerecord-transactionable/badge.svg?branch=master)](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 | [![Join the chat at https://gitter.im/pboling/activerecord-transactionable](https://badges.gitter.im/Join%20Chat.svg)](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: MIT](https://img.shields.io/badge/License-MIT-green.svg)][license-ref] [![RubyDoc.info](https://img.shields.io/badge/documentation-rubydoc-brightgreen.svg?style=flat)][documentation] |
8
+ | version & downloads | [![Version](https://img.shields.io/gem/v/activerecord-transactionable.svg)][rubygems] [![Total Downloads](https://img.shields.io/gem/dt/activerecord-transactionable.svg)][rubygems] [![Downloads Today](https://img.shields.io/gem/rd/activerecord-transactionable.svg)][rubygems] [![Homepage](https://img.shields.io/badge/source-github-brightgreen.svg?style=flat)][source] |
9
+ | dependencies & linting | [![Depfu](https://badges.depfu.com/badges/d570491bac0ad3b0b65deb3c82028327/count.svg)][depfu] [![lint status](https://github.com/pboling/activerecord-transactionable/actions/workflows/style.yml/badge.svg)][actions] |
10
+ | unit tests | [![supported rubies](https://github.com/pboling/activerecord-transactionable/actions/workflows/supported.yml/badge.svg)][actions] [![unsupported status](https://github.com/pboling/activerecord-transactionable/actions/workflows/unsupported.yml/badge.svg)][actions] |
11
+ | coverage & maintainability | [![Test Coverage](https://api.codeclimate.com/v1/badges/41fa99881cfe6d45e7e5/test_coverage)][climate_coverage] [![codecov](https://codecov.io/gh/pboling/activerecord-transactionable/branch/master/graph/badge.svg?token=4ZNAWNxrf9)][codecov_coverage] [![Maintainability](https://api.codeclimate.com/v1/badges/41fa99881cfe6d45e7e5/maintainability)][climate_maintainability] [![Maintenance Policy](https://img.shields.io/badge/maintenance-policy-brightgreen.svg?style=flat)][maintenancee_policy] |
12
+ | resources | [![Discussion](https://img.shields.io/badge/discussions-github-brightgreen.svg?style=flat)][gh_discussions] [![Get help on Codementor](https://cdn.codementor.io/badges/get_help_github.svg)](https://www.codementor.io/peterboling?utm_source=github&utm_medium=button&utm_term=peterboling&utm_campaign=github) [![Join the chat at https://gitter.im/pboling/activerecord-transactionable](https://badges.gitter.im/Join%20Chat.svg)][chat] [![Blog](https://img.shields.io/badge/blog-railsbling-brightgreen.svg?style=flat)][blogpage] |
13
+ | Spread ~♡ⓛⓞⓥⓔ♡~ | [![Open Source Helpers](https://www.codetriage.com/pboling/activerecord-transactionable/badges/users.svg)][code_triage] [![Liberapay Patrons](https://img.shields.io/liberapay/patrons/pboling.svg?logo=liberapay)][liberapay_donate] [![Sponsor Me](https://img.shields.io/badge/sponsor-pboling.svg?style=social&logo=github)][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 two methods:
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
- result = transaction_wrapper(...) do
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 'activerecord-transactionable'
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
- end
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 = @client.transaction_wrapper(lock: true) do
137
+ transaction_result = @client.transaction_wrapper(lock: true) do
136
138
  @client.assign_attributes(client_params)
137
139
  @client.save!
138
- end
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.debug("Sending Error #{args[:error].class}: #{args[:error].message} to Raygun Failed with: #{e.class}: #{e.message}")
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: [![RubyDoc.info](https://img.shields.io/badge/documentation-rubydoc-brightgreen.svg?style=flat)][documentation]
208
+ * GitHub Discussions: [![Discussion](https://img.shields.io/badge/discussions-github-brightgreen.svg?style=flat)][gh_discussions]
209
+ * Live Chat on Gitter: [![Join the chat at https://gitter.im/pboling/activerecord-transactionable](https://badges.gitter.im/Join%20Chat.svg)][chat]
210
+ * Maintainer's Blog: [![Blog](https://img.shields.io/badge/blog-railsbling-brightgreen.svg?style=flat)][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
- Bug reports and pull requests are welcome on GitHub at https://github.com/pboling/activerecord-transactionable.
224
+ See [CONTRIBUTING.md][contributing]
225
+
226
+ ## Contributors
227
+
228
+ [![Contributors](https://contrib.rocks/image?repo=pboling/activerecord-transactionable)][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: MIT](https://img.shields.io/badge/License-MIT-green.svg)][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
- [pvc]: http://docs.rubygems.org/read/chapter/16#page74
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
- [documentation]: http://rdoc.info/github/pboling/activerecord-transactionable/frames
216
- [homepage]: https://github.com/pboling/activerecord-transactionable
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
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
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 :default => :spec
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
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
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 'activerecord/transactionable/version'
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 = %q{Do ActiveRecord transactions the right way.}
14
- spec.description = %q{Getting transactions right is hard, and this gem makes it easier.}
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" # Uses named required parameters
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 "bundler", "~> 1.15"
27
- spec.add_development_dependency "rake", "~> 12.2"
28
- spec.add_development_dependency "rspec", "~> 3.4"
29
- spec.add_development_dependency "appraisal", "~> 2.2"
30
- spec.add_development_dependency "wwtd", "~> 1.3"
31
- spec.add_development_dependency "coveralls", "~> 0.8"
32
- spec.add_development_dependency "thor", "~> 0.19.1"
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,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
4
  require "bundler/setup"
4
5
  require "activerecord/transactionable"
@@ -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
- def initialize(value)
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
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Activerecord
2
4
  module Transactionable
3
- VERSION = "2.0.1"
5
+ VERSION = "2.0.5"
4
6
  end
5
7
  end