roqua-support 0.1.4 → 0.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f8db8a86e0ea4abd9792e5cf25865306e7b0cba1
4
- data.tar.gz: 6d2bc6551743ff667987df984710536a4185a7f1
3
+ metadata.gz: 62b713d3e5154df4114e38724df34637620903af
4
+ data.tar.gz: 9248b7cc7833517983955066fe280e5fda61a673
5
5
  SHA512:
6
- metadata.gz: 8e717a9ebca1e2e98436d258567ee3a5c27e569f020bc72d0fb4c8debcbacb373051fa74f0c470da4c11aaab247a228f2579192f0579f17f0792d4baa2b76c62
7
- data.tar.gz: e8655d8edb9832d7d0c892666c893ede37593c2ba7f48d800db990de8a5e08ff01fa829df3e1cda33adc45f5e8762531123dce7d747ef36804a7771189715244
6
+ metadata.gz: a34115a92fdc6f8b30dbd835b68e1921eb9c519e80122890df104428b7ea9f91f1decb9d1aff568cefd23ba8d7a2d3c55cb4d1effebd28f1cf39d014844114f1
7
+ data.tar.gz: 037d2ca3d79ae8d2cd722e5abfd88dde320fdb8698662af6dbec7cea04b7ed27c790306802243568e5dd9a16eae2a8a3a4f5654b626de5af3a49f6ba3812c9e4
@@ -1,3 +1,7 @@
1
+ ## 0.1.5 / 2014-03-10
2
+
3
+ * Make sure to raise when uniq_find_or_create cannot find a record
4
+
1
5
  ## 0.1.4 / 2014-02-25
2
6
 
3
7
  * Add ActiveRecord \#uniq\_find\_or\_create\_by! method to find or create records with uniqueness constraints enforced by the database.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- roqua-support (0.1.4)
4
+ roqua-support (0.1.5)
5
5
  activesupport (>= 3.2, < 5.0)
6
6
 
7
7
  GEM
@@ -44,7 +44,7 @@ GEM
44
44
  lumberjack (1.0.4)
45
45
  method_source (0.8.2)
46
46
  minitest (4.7.5)
47
- multi_json (1.8.4)
47
+ multi_json (1.9.0)
48
48
  nio4r (1.0.0)
49
49
  pry (0.9.12.6)
50
50
  coderay (~> 1.0)
@@ -1,5 +1,5 @@
1
1
  module Roqua
2
2
  module Support
3
- VERSION = "0.1.4"
3
+ VERSION = "0.1.5"
4
4
  end
5
5
  end
@@ -9,16 +9,16 @@ module ActiveRecord
9
9
  # containing the validation errors is returned instead.
10
10
  def self.uniq_find_or_create_by(attributes, &block)
11
11
  record = find_or_create_by(attributes, &block)
12
- rescue ActiveRecord::RecordNotUnique
12
+ rescue ActiveRecord::RecordNotUnique => exception
13
13
  ensure
14
- return find_by(attributes) || record
14
+ return find_by(attributes) || record || raise(exception)
15
15
  end
16
16
 
17
17
  # Use this method if you want an exception to be raised when creating a new record fails due to some validation
18
18
  # error other than uniqueness.
19
19
  def self.uniq_find_or_create_by!(attributes, &block)
20
20
  find_or_create_by!(attributes, &block)
21
- rescue ActiveRecord::RecordNotUnique
21
+ rescue ActiveRecord::RecordNotUnique => exception
22
22
  rescue ActiveRecord::RecordInvalid => exception
23
23
  ensure
24
24
  return find_by(attributes) || raise(exception)
@@ -20,7 +20,7 @@ describe ActiveRecord::Base do
20
20
 
21
21
  describe '#uniq_find_or_create_by' do
22
22
  it 'tries to find or create a record by the attributes provided' do
23
- expect(ActiveRecord::Base).to receive(:find_or_create_by).with(attributes, &block)
23
+ expect(ActiveRecord::Base).to receive(:find_or_create_by).with(attributes, &block).and_return record
24
24
  ActiveRecord::Base.uniq_find_or_create_by attributes, &block
25
25
  end
26
26
 
@@ -36,6 +36,15 @@ describe ActiveRecord::Base do
36
36
  expect(ActiveRecord::Base.uniq_find_or_create_by attributes, &block).to eq(record)
37
37
  end
38
38
 
39
+ it 'raises when a concurrent record is detected by the database but could not be queried for unknown reasons' do
40
+ allow(ActiveRecord::Base).to receive(:find_by).with(attributes).and_return nil
41
+ allow(ActiveRecord::Base).to receive(:find_or_create_by).with(attributes, &block)
42
+ .and_raise ActiveRecord::RecordNotUnique
43
+ expect do
44
+ ActiveRecord::Base.uniq_find_or_create_by attributes, &block
45
+ end.to raise_error ActiveRecord::RecordNotUnique
46
+ end
47
+
39
48
  it 'returns the created record for inspection when validation fails' do
40
49
  allow(ActiveRecord::Base).to receive(:find_or_create_by).with(attributes, &block).and_return record
41
50
  expect(ActiveRecord::Base.uniq_find_or_create_by attributes, &block).to eq(record)
@@ -61,6 +70,15 @@ describe ActiveRecord::Base do
61
70
  expect(ActiveRecord::Base.uniq_find_or_create_by! attributes, &block).to eq(record)
62
71
  end
63
72
 
73
+ it 'raises when a concurrent record is detected by the database but could not be queried for unknown reasons' do
74
+ allow(ActiveRecord::Base).to receive(:find_by).with(attributes).and_return nil
75
+ allow(ActiveRecord::Base).to receive(:find_or_create_by!).with(attributes, &block)
76
+ .and_raise ActiveRecord::RecordNotUnique
77
+ expect do
78
+ ActiveRecord::Base.uniq_find_or_create_by! attributes, &block
79
+ end.to raise_error ActiveRecord::RecordNotUnique
80
+ end
81
+
64
82
  it 'raises when creating a new record causes validation failures not due to concurrency' do
65
83
  exception = ActiveRecord::RecordInvalid.new 'some_validation_error'
66
84
  allow(ActiveRecord::Base).to receive(:find_or_create_by!).with(attributes, &block).and_raise exception
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: roqua-support
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marten Veldthuis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-25 00:00:00.000000000 Z
11
+ date: 2014-03-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport