dino_utils 0.1.2 → 0.1.3
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 +4 -4
- data/lib/dino/upsert.rb +42 -0
- data/lib/dino_utils/version.rb +1 -1
- data/lib/dino_utils.rb +1 -40
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4fe82553f09d16773a01d4b2541dac1066447df4
|
4
|
+
data.tar.gz: 5c40e59e63341f818a9401fb16b5515ea918428e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 976a2baff2e86bfc86cc85f272fd1ade2b69bd688bdbf1b5e595d17b324a2793bdef6524dda3b72aac8882889cadf9cf54aeed9581d8ef88c682959b0245eba3
|
7
|
+
data.tar.gz: 3a1ffceb478d8a018262d8039b197647469c58083137fe8037abcd45c7eaa150bcbf2a4e90ca89a70bcbb9c2494be708acb0e395f15973864f9e173aa6a0a831
|
data/lib/dino/upsert.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'pg'
|
3
|
+
|
4
|
+
module Dino
|
5
|
+
# Provides a simple way to do an create or update of an ActiveRecord object.
|
6
|
+
module Upsert
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
# User.upsert
|
9
|
+
module ClassMethods
|
10
|
+
def upsert(*args)
|
11
|
+
Dino::Upsert.upsert(self, *args)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
# klass: The ActiveRecord class we are upserting against.
|
16
|
+
# conditions: what should match the upsert
|
17
|
+
# options: what we are updating/inserting
|
18
|
+
# If provided, the block gets the object before it's saved, in case
|
19
|
+
# there's special init options necessary for it.
|
20
|
+
# rubocop:disable MethodLength
|
21
|
+
def self.upsert(klass, conditions, options = {}, &block)
|
22
|
+
retry_count = 0
|
23
|
+
begin
|
24
|
+
klass.transaction(requires_new: true) do
|
25
|
+
klass.where(conditions).first_or_initialize(&block).tap { |t| t.update!(options) }
|
26
|
+
end
|
27
|
+
rescue PG::UniqueViolation, ActiveRecord::RecordNotUnique
|
28
|
+
# If there's a unique violation, retry this. But only a certain amount
|
29
|
+
# of times or we'll get into an infinite loop if something's messed up.
|
30
|
+
# (like an incorrect unique index or something)
|
31
|
+
if retry_count < 10
|
32
|
+
retry_count += 1
|
33
|
+
retry
|
34
|
+
else
|
35
|
+
raise
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
ActiveRecord::Base.send(:include, Dino::Upsert)
|
data/lib/dino_utils/version.rb
CHANGED
data/lib/dino_utils.rb
CHANGED
@@ -1,41 +1,2 @@
|
|
1
1
|
require "dino_utils/version"
|
2
|
-
|
3
|
-
|
4
|
-
module Dino
|
5
|
-
# Provides a simple way to do an create or update of an ActiveRecord object.
|
6
|
-
module Upsert
|
7
|
-
extend ActiveSupport::Concern
|
8
|
-
# User.upsert
|
9
|
-
module ClassMethods
|
10
|
-
def upsert(*args)
|
11
|
-
Dino::Upsert.upsert(self, *args)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
# klass: The ActiveRecord class we are upserting against.
|
16
|
-
# conditions: what should match the upsert
|
17
|
-
# options: what we are updating/inserting
|
18
|
-
# If provided, the block gets the object before it's saved, in case
|
19
|
-
# there's special init options necessary for it.
|
20
|
-
# rubocop:disable MethodLength
|
21
|
-
def self.upsert(klass, conditions, options = {}, &block)
|
22
|
-
retry_count = 0
|
23
|
-
begin
|
24
|
-
klass.transaction(requires_new: true) do
|
25
|
-
klass.where(conditions).first_or_initialize(&block).tap { |t| t.update!(options) }
|
26
|
-
end
|
27
|
-
rescue PG::UniqueViolation, ActiveRecord::RecordNotUnique
|
28
|
-
# If there's a unique violation, retry this. But only a certain amount
|
29
|
-
# of times or we'll get into an infinite loop if something's messed up.
|
30
|
-
# (like an incorrect unique index or something)
|
31
|
-
if retry_count < 10
|
32
|
-
retry_count += 1 && retry
|
33
|
-
else
|
34
|
-
raise
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
ActiveRecord::Base.send(:include, Dino::Upsert)
|
2
|
+
require_relative 'dino/upsert'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dino_utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joe Van Dyk
|
@@ -86,6 +86,7 @@ files:
|
|
86
86
|
- bin/rake
|
87
87
|
- bin/setup
|
88
88
|
- dino_utils.gemspec
|
89
|
+
- lib/dino/upsert.rb
|
89
90
|
- lib/dino_utils.rb
|
90
91
|
- lib/dino_utils/version.rb
|
91
92
|
homepage: https://github.com/tanga/dino_utils
|
@@ -113,4 +114,3 @@ signing_key:
|
|
113
114
|
specification_version: 4
|
114
115
|
summary: Dino stuff
|
115
116
|
test_files: []
|
116
|
-
has_rdoc:
|