activerecord-transactionable 1.0.3 → 2.0.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
  SHA1:
3
- metadata.gz: 45ff61a88a97f5e887d2782237e27bf2ea65075c
4
- data.tar.gz: f6706016a55fcb7eb575c4957168d118a39bbc7f
3
+ metadata.gz: 3becf9bed4568fc5073445b3ae7beb01d86c8e99
4
+ data.tar.gz: e771c0221f7ea32ec42a2f4d2628508fd9aa56d2
5
5
  SHA512:
6
- metadata.gz: 21ec04bb084c2e7bbb52b1c68c0861e3bb2feb0f9e1141bbf1c6961990fd2f85d4bdfa1e2573b59519e5b5693b57db6a3f7e8e7962ad8e8f664af0061e56f8b2
7
- data.tar.gz: 02d33ee1345a027f51b75139a4408ffda9c9426a59c0316525cbdfa01089ea37b1174426199216b80c2bffefac91f240ae74c01b2e039cfa39038c098f4f110a
6
+ metadata.gz: '019b592ea646a19664e20914d9c3ac42bdd3766fb6f61b4079f6b8d1c36b74542d057570f7322b985f288a7572e3667ba75672311aa55f7c0842f0ad69fff998'
7
+ data.tar.gz: 1dd3ac5bc3e1a670c380c1ce6241919e4e313d77249d1cdb279e4d8a0fd49d260134b8a6a5eb50ff62634dce3f6ce7fbf544a328890910f12b1f91e496105513
data/README.md CHANGED
@@ -5,7 +5,7 @@ Provides a method, `transaction_wrapper` at the class and instance levels that c
5
5
  | Project | Activerecord::Transactionable |
6
6
  |------------------------ | ----------------- |
7
7
  | gem name | activerecord-transactionable |
8
- | license | MIT |
8
+ | license | [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) |
9
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
10
  | download rank | [![Total Downloads](https://img.shields.io/gem/rt/activerecord-transactionable.svg)](https://rubygems.org/gems/activerecord-transactionable) |
11
11
  | version | [![Gem Version](https://badge.fury.io/rb/activerecord-transactionable.png)](http://badge.fury.io/rb/activerecord-transactionable) |
@@ -24,6 +24,29 @@ Useful as an example of correct behavior for wrapping transactions.
24
24
  NOTE: Rails' transactions are per-database connection, not per-model, nor per-instance,
25
25
  see: http://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html
26
26
 
27
+ ## Upgrading to Version 2
28
+
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:
30
+ ```ruby
31
+ result = transaction_wrapper(...) do
32
+ something
33
+ end
34
+ result.fail?
35
+ result.success?
36
+ ```
37
+ Where you used to have:
38
+ ```ruby
39
+ if result
40
+ # ...
41
+ end
42
+ ```
43
+ You must update to:
44
+ ```ruby
45
+ if result.success?
46
+ # ...
47
+ end
48
+ ```
49
+
27
50
  ## Installation
28
51
 
29
52
  Add this line to your application's Gemfile:
@@ -91,7 +114,8 @@ car = Car.new(name: nil)
91
114
  result = car.transaction_wrapper(lock: true) do # uses ActiveRecord's with_lock
92
115
  car.save!
93
116
  end
94
- result # => true, false or nil
117
+ result # => an instance of Activerecord::Transactionable::Result
118
+ result.success? # => true or false
95
119
  ```
96
120
 
97
121
  Meanings of `transaction_wrapper` return values:
@@ -108,7 +132,7 @@ transaction_result = @client.transaction_wrapper(lock: true) do
108
132
  @client.assign_attributes(client_params)
109
133
  @client.save!
110
134
  end
111
- if transaction_result
135
+ if transaction_result.success?
112
136
  render :show, locals: { client: @client }, status: :ok
113
137
  else
114
138
  # Something prevented update
@@ -24,7 +24,7 @@ Gem::Specification.new do |spec|
24
24
  spec.add_dependency "activerecord"
25
25
 
26
26
  spec.add_development_dependency "bundler", "~> 1.15"
27
- spec.add_development_dependency "rake", "~> 10.5"
27
+ spec.add_development_dependency "rake", "~> 12.2"
28
28
  spec.add_development_dependency "rspec", "~> 3.4"
29
29
  spec.add_development_dependency "appraisal", "~> 2.2"
30
30
  spec.add_development_dependency "wwtd", "~> 1.3"
@@ -1,4 +1,3 @@
1
- require "activerecord/transactionable/version"
2
1
  require "active_model"
3
2
  require "active_record"
4
3
  # apparently needed for Rails 4.0 compatibility with rspec, when
@@ -6,6 +5,9 @@ require "active_record"
6
5
  # keep your Gemfile sorted alphabetically.
7
6
  require "active_record/validations"
8
7
 
8
+ require "activerecord/transactionable/version"
9
+ require "activerecord/transactionable/result"
10
+
9
11
  module Activerecord # Note lowercase "r" in Activerecord (different namespace than rails' module)
10
12
  # SRP: Provides an example of correct behavior for wrapping transactions.
11
13
  # NOTE: Rails' transactions are per-database connection, not per-model, nor per-instance,
@@ -70,15 +72,11 @@ module Activerecord # Note lowercase "r" in Activerecord (different namespace th
70
72
  else
71
73
  logger.debug("[#{self}.transaction_wrapper] Will start a nested transaction.")
72
74
  end
75
+ end
76
+ error_handler_outside_transaction(object: object, transaction_open: transaction_open, **outside_args) do
73
77
  run_inside_transaction_block(transaction_args: transaction_args, inside_args: inside_args, lock: lock, transaction_open: transaction_open, object: object) do
74
78
  yield
75
79
  end
76
- else
77
- error_handler_outside_transaction(object: object, transaction_open: transaction_open, **outside_args) do
78
- run_inside_transaction_block(transaction_args: transaction_args, inside_args: inside_args, lock: lock, transaction_open: transaction_open, object: object) do
79
- yield
80
- end
81
- end
82
80
  end
83
81
  end
84
82
 
@@ -156,11 +154,11 @@ module Activerecord # Note lowercase "r" in Activerecord (different namespace th
156
154
  # If we were already inside a transaction, such that this one is nested,
157
155
  # then the result of the yield is what we want to return, to preserve the innermost result
158
156
  result = yield
159
- # When in the outside context we need to preserve the inside result so it bubles up unmolested with the "meaningful" result of the transaction.
160
- if transaction_open || local_context == OUTSIDE_CONTEXT
161
- result # <= preserve the meaningful return value. Meaning: transaction succeeded, no errors raised
157
+ # When in the outside context we need to preserve the inside result so it bubbles up unmolested with the "meaningful" result of the transaction.
158
+ if result.is_a?(Activerecord::Transactionable::Result)
159
+ result # <= preserve the meaningful return value
162
160
  else
163
- true # <= make the return value meaningful. Meaning: transaction succeeded, no errors raised
161
+ Activerecord::Transactionable::Result.new(true) # <= make the return value meaningful. Meaning: transaction succeeded, no errors raised
164
162
  end
165
163
  rescue *reraisable_errors => error
166
164
  # This has highest precedence because raising is the most critical functionality of a raised error to keep
@@ -175,7 +173,7 @@ module Activerecord # Note lowercase "r" in Activerecord (different namespace th
175
173
  # To avoid the infinite recursion, we track the retry state
176
174
  if re_try
177
175
  transaction_error_logger(object: object, error: error, additional_message: " [#{transaction_open ? 'nested ' : ''}#{local_context} 2nd attempt]")
178
- false # <= make the return value meaningful. Meaning is: transaction failed after two attempts
176
+ Activerecord::Transactionable::Result.new(false) # <= make the return value meaningful. Meaning is: transaction failed after two attempts
179
177
  else
180
178
  re_try = true
181
179
  # Not adding error to base when retrying, because otherwise the added error may
@@ -186,10 +184,10 @@ module Activerecord # Note lowercase "r" in Activerecord (different namespace th
186
184
  rescue *already_been_added_to_self => error
187
185
  # ActiveRecord::RecordInvalid, when done correctly, will have already added the error to object.
188
186
  transaction_error_logger(object: nil, error: error, additional_message: " [#{transaction_open ? 'nested ' : ''}#{local_context}]")
189
- false # <= make the return value meaningful. Meaning is: transaction failed
187
+ Activerecord::Transactionable::Result.new(false) # <= make the return value meaningful. Meaning is: transaction failed
190
188
  rescue *needing_added_to_self => error
191
189
  transaction_error_logger(object: object, error: error, additional_message: " [#{transaction_open ? 'nested ' : ''}#{local_context}]")
192
- false # <= make the return value meaningful. Meaning is: transaction failed
190
+ Activerecord::Transactionable::Result.new(false) # <= make the return value meaningful. Meaning is: transaction failed
193
191
  end
194
192
  end
195
193
 
@@ -0,0 +1,18 @@
1
+ module Activerecord
2
+ module Transactionable
3
+ class Result
4
+ attr_reader :value
5
+ def initialize(value)
6
+ @value = value
7
+ end
8
+
9
+ def fail?
10
+ value == false
11
+ end
12
+
13
+ def success?
14
+ value == true
15
+ end
16
+ end
17
+ end
18
+ end
@@ -1,5 +1,5 @@
1
1
  module Activerecord
2
2
  module Transactionable
3
- VERSION = "1.0.3"
3
+ VERSION = "2.0.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-transactionable
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Boling
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-09-16 00:00:00.000000000 Z
11
+ date: 2017-11-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '10.5'
61
+ version: '12.2'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '10.5'
68
+ version: '12.2'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rspec
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -160,6 +160,7 @@ files:
160
160
  - gemfiles/rails_5_0.gemfile
161
161
  - gemfiles/rails_5_1.gemfile
162
162
  - lib/activerecord/transactionable.rb
163
+ - lib/activerecord/transactionable/result.rb
163
164
  - lib/activerecord/transactionable/version.rb
164
165
  homepage: http://www.railsbling.com/tags/activerecord-transactionable
165
166
  licenses: