roqua-support 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +42 -1
- data/Guardfile +8 -0
- data/lib/roqua-support/version.rb +1 -1
- data/lib/roqua/core_ext/activerecord/uniq_find_or_create.rb +15 -0
- data/spec/roqua/core_ext/activerecord/uniq_find_or_create_spec.rb +34 -5
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f8db8a86e0ea4abd9792e5cf25865306e7b0cba1
|
4
|
+
data.tar.gz: 6d2bc6551743ff667987df984710536a4185a7f1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8e717a9ebca1e2e98436d258567ee3a5c27e569f020bc72d0fb4c8debcbacb373051fa74f0c470da4c11aaab247a228f2579192f0579f17f0792d4baa2b76c62
|
7
|
+
data.tar.gz: e8655d8edb9832d7d0c892666c893ede37593c2ba7f48d800db990de8a5e08ff01fa829df3e1cda33adc45f5e8762531123dce7d747ef36804a7771189715244
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
+
## 0.1.4 / 2014-02-25
|
2
|
+
|
3
|
+
* Add ActiveRecord \#uniq\_find\_or\_create\_by! method to find or create records with uniqueness constraints enforced by the database.
|
4
|
+
* Add some clarifying comments to the ActiveRecord extensions.
|
5
|
+
|
1
6
|
## 0.1.3 / 2014-02-24
|
2
7
|
|
3
8
|
* Add ActiveRecord \#uniq\_find\_or\_create\_by method to find or create records with uniqueness constraints enforced by the database.
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
roqua-support (0.1.
|
4
|
+
roqua-support (0.1.4)
|
5
5
|
activesupport (>= 3.2, < 5.0)
|
6
6
|
|
7
7
|
GEM
|
@@ -14,11 +14,46 @@ GEM
|
|
14
14
|
thread_safe (~> 0.1)
|
15
15
|
tzinfo (~> 0.3.37)
|
16
16
|
atomic (1.1.14)
|
17
|
+
celluloid (0.15.2)
|
18
|
+
timers (~> 1.1.0)
|
19
|
+
celluloid-io (0.15.0)
|
20
|
+
celluloid (>= 0.15.0)
|
21
|
+
nio4r (>= 0.5.0)
|
22
|
+
coderay (1.1.0)
|
17
23
|
diff-lcs (1.2.5)
|
24
|
+
ffi (1.9.3)
|
25
|
+
formatador (0.2.4)
|
26
|
+
fuubar (1.3.2)
|
27
|
+
rspec (>= 2.14.0, < 3.1.0)
|
28
|
+
ruby-progressbar (~> 1.3)
|
29
|
+
guard (2.4.0)
|
30
|
+
formatador (>= 0.2.4)
|
31
|
+
listen (~> 2.1)
|
32
|
+
lumberjack (~> 1.0)
|
33
|
+
pry (>= 0.9.12)
|
34
|
+
thor (>= 0.18.1)
|
35
|
+
guard-rspec (4.2.6)
|
36
|
+
guard (~> 2.1)
|
37
|
+
rspec (>= 2.14, < 4.0)
|
18
38
|
i18n (0.6.9)
|
39
|
+
listen (2.5.0)
|
40
|
+
celluloid (>= 0.15.2)
|
41
|
+
celluloid-io (>= 0.15.0)
|
42
|
+
rb-fsevent (>= 0.9.3)
|
43
|
+
rb-inotify (>= 0.9)
|
44
|
+
lumberjack (1.0.4)
|
45
|
+
method_source (0.8.2)
|
19
46
|
minitest (4.7.5)
|
20
47
|
multi_json (1.8.4)
|
48
|
+
nio4r (1.0.0)
|
49
|
+
pry (0.9.12.6)
|
50
|
+
coderay (~> 1.0)
|
51
|
+
method_source (~> 0.8)
|
52
|
+
slop (~> 3.4)
|
21
53
|
rake (10.1.1)
|
54
|
+
rb-fsevent (0.9.4)
|
55
|
+
rb-inotify (0.9.3)
|
56
|
+
ffi (>= 0.5.0)
|
22
57
|
rspec (2.14.1)
|
23
58
|
rspec-core (~> 2.14.0)
|
24
59
|
rspec-expectations (~> 2.14.0)
|
@@ -27,8 +62,12 @@ GEM
|
|
27
62
|
rspec-expectations (2.14.5)
|
28
63
|
diff-lcs (>= 1.1.3, < 2.0)
|
29
64
|
rspec-mocks (2.14.6)
|
65
|
+
ruby-progressbar (1.4.1)
|
66
|
+
slop (3.4.7)
|
67
|
+
thor (0.18.1)
|
30
68
|
thread_safe (0.1.3)
|
31
69
|
atomic
|
70
|
+
timers (1.1.0)
|
32
71
|
tzinfo (0.3.38)
|
33
72
|
|
34
73
|
PLATFORMS
|
@@ -36,6 +75,8 @@ PLATFORMS
|
|
36
75
|
|
37
76
|
DEPENDENCIES
|
38
77
|
bundler (~> 1.0)
|
78
|
+
fuubar (~> 1.3.2)
|
79
|
+
guard-rspec (~> 4.2.6)
|
39
80
|
rake
|
40
81
|
roqua-support!
|
41
82
|
rspec (>= 2.12.0, < 4.0)
|
data/Guardfile
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
guard :rspec, cmd: 'bundle exec rspec --format Fuubar --colour' do
|
5
|
+
watch(%r{^spec/.+_spec\.rb$})
|
6
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
7
|
+
watch('spec/spec_helper.rb') { "spec" }
|
8
|
+
end
|
@@ -2,11 +2,26 @@ require 'active_support'
|
|
2
2
|
|
3
3
|
module ActiveRecord
|
4
4
|
class Base
|
5
|
+
# Use this method to find or create records that have uniqueness constraints enforced by the database.
|
6
|
+
# After calling the AR find_or_create_by method it queries the preexisting or created record by the attributes
|
7
|
+
# provided, thereby ensuring that a concurrently created record is returned when a AR RecordNotUnique error is
|
8
|
+
# raised. When no record can be found, because for instance validations fail on create, the created object
|
9
|
+
# containing the validation errors is returned instead.
|
5
10
|
def self.uniq_find_or_create_by(attributes, &block)
|
6
11
|
record = find_or_create_by(attributes, &block)
|
7
12
|
rescue ActiveRecord::RecordNotUnique
|
8
13
|
ensure
|
9
14
|
return find_by(attributes) || record
|
10
15
|
end
|
16
|
+
|
17
|
+
# Use this method if you want an exception to be raised when creating a new record fails due to some validation
|
18
|
+
# error other than uniqueness.
|
19
|
+
def self.uniq_find_or_create_by!(attributes, &block)
|
20
|
+
find_or_create_by!(attributes, &block)
|
21
|
+
rescue ActiveRecord::RecordNotUnique
|
22
|
+
rescue ActiveRecord::RecordInvalid => exception
|
23
|
+
ensure
|
24
|
+
return find_by(attributes) || raise(exception)
|
25
|
+
end
|
11
26
|
end
|
12
27
|
end
|
@@ -8,14 +8,17 @@ module ActiveRecord
|
|
8
8
|
|
9
9
|
class RecordNotUnique < StandardError
|
10
10
|
end
|
11
|
+
|
12
|
+
class RecordInvalid < StandardError
|
13
|
+
end
|
11
14
|
end
|
12
15
|
|
13
16
|
describe ActiveRecord::Base do
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
let(:record) { double('record') }
|
17
|
+
let(:attributes) { double('attributes') }
|
18
|
+
let(:block) { -> {} }
|
19
|
+
let(:record) { double('record') }
|
18
20
|
|
21
|
+
describe '#uniq_find_or_create_by' do
|
19
22
|
it 'tries to find or create a record by the attributes provided' do
|
20
23
|
expect(ActiveRecord::Base).to receive(:find_or_create_by).with(attributes, &block)
|
21
24
|
ActiveRecord::Base.uniq_find_or_create_by attributes, &block
|
@@ -33,9 +36,35 @@ describe ActiveRecord::Base do
|
|
33
36
|
expect(ActiveRecord::Base.uniq_find_or_create_by attributes, &block).to eq(record)
|
34
37
|
end
|
35
38
|
|
36
|
-
it 'returns
|
39
|
+
it 'returns the created record for inspection when validation fails' do
|
37
40
|
allow(ActiveRecord::Base).to receive(:find_or_create_by).with(attributes, &block).and_return record
|
38
41
|
expect(ActiveRecord::Base.uniq_find_or_create_by attributes, &block).to eq(record)
|
39
42
|
end
|
40
43
|
end
|
44
|
+
|
45
|
+
describe '#uniq_find_or_create_by!' do
|
46
|
+
it 'tries to find or create a record by the attributes provided' do
|
47
|
+
allow(ActiveRecord::Base).to receive(:find_by).with(attributes).and_return record
|
48
|
+
expect(ActiveRecord::Base).to receive(:find_or_create_by!).with(attributes, &block)
|
49
|
+
ActiveRecord::Base.uniq_find_or_create_by! attributes, &block
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'returns a preexisting or created record by querying it' do
|
53
|
+
allow(ActiveRecord::Base).to receive(:find_by).with(attributes).and_return record
|
54
|
+
expect(ActiveRecord::Base.uniq_find_or_create_by! attributes, &block).to eq(record)
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'returns a concurrenlty created record' do
|
58
|
+
allow(ActiveRecord::Base).to receive(:find_by).with(attributes).and_return record
|
59
|
+
allow(ActiveRecord::Base).to receive(:find_or_create_by!).with(attributes, &block)
|
60
|
+
.and_raise ActiveRecord::RecordNotUnique
|
61
|
+
expect(ActiveRecord::Base.uniq_find_or_create_by! attributes, &block).to eq(record)
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'raises when creating a new record causes validation failures not due to concurrency' do
|
65
|
+
exception = ActiveRecord::RecordInvalid.new 'some_validation_error'
|
66
|
+
allow(ActiveRecord::Base).to receive(:find_or_create_by!).with(attributes, &block).and_raise exception
|
67
|
+
expect { ActiveRecord::Base.uniq_find_or_create_by! attributes, &block }.to raise_error(exception)
|
68
|
+
end
|
69
|
+
end
|
41
70
|
end
|
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
|
+
version: 0.1.4
|
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-
|
11
|
+
date: 2014-02-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -90,6 +90,7 @@ files:
|
|
90
90
|
- CHANGELOG.md
|
91
91
|
- Gemfile
|
92
92
|
- Gemfile.lock
|
93
|
+
- Guardfile
|
93
94
|
- LICENSE.txt
|
94
95
|
- README.md
|
95
96
|
- Rakefile
|