tp_record_optimistic 0.2.0 → 0.2.1
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/config/initializers/inflections.rb +1 -0
- data/lib/tp_record_optimistic.rb +16 -6
- data/lib/tp_record_optimistic/version.rb +1 -1
- 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: 0dd5d0fcffaddb10783744f3eaf79d415d6858dc
|
4
|
+
data.tar.gz: f2aad750e85c444a214405fe0acb050d9eb3131e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c78aa6ba5d05359ef3a8bddb85653d959f8e3257961d5d360ad11337a64e3bb21ce25e0da2c9720d7db9d97ab5df7397da697fa7b22c2ac89d23179a7a9efb7c
|
7
|
+
data.tar.gz: 86ffef16fcd8e43e7dc4fc95e267e8bc3d6ce58d469801413a351b54a4a064cb42c789d370cd0957d4bce80d38bfb2c3609fad82099c8fed93c1ce8cade132e6
|
data/lib/tp_record_optimistic.rb
CHANGED
@@ -1,9 +1,21 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
require 'active_record' unless defined? ActiveRecord
|
3
|
-
|
3
|
+
|
4
|
+
module SQLErrorParse
|
5
|
+
def unique_errors(error)
|
6
|
+
table = self.class.table_name
|
7
|
+
split_key = /(INSERT|UPDATE)/
|
8
|
+
especific_key = %w(UNIQUE constraint failed: SQLite3::ConstraintException:)
|
9
|
+
error.split(split_key)[0].split
|
10
|
+
.reject { |e| especific_key.include? e }
|
11
|
+
.map { |e| e.sub("#{table}.", '').delete(':') }
|
12
|
+
.map(&:to_sym)
|
13
|
+
end
|
14
|
+
end
|
4
15
|
|
5
16
|
module TPRecordOptimistic
|
6
17
|
extend ActiveSupport::Concern
|
18
|
+
include SQLErrorParse
|
7
19
|
|
8
20
|
def save(*args)
|
9
21
|
super(*args)
|
@@ -14,7 +26,9 @@ module TPRecordOptimistic
|
|
14
26
|
def save_optimistic(*args)
|
15
27
|
old_save(*args)
|
16
28
|
rescue ActiveRecord::RecordNotUnique => e
|
17
|
-
|
29
|
+
unique_errors(e.to_s).each do |field|
|
30
|
+
errors.add(field, :Unique, message: "UNIQUE constraint failed #{field}")
|
31
|
+
end
|
18
32
|
return false
|
19
33
|
end
|
20
34
|
|
@@ -30,10 +44,6 @@ module TPRecordOptimistic
|
|
30
44
|
errors.add('all', e.to_s)
|
31
45
|
raise ActiveRecord::RecordInvalid, self
|
32
46
|
end
|
33
|
-
|
34
|
-
# def optimistic_unique
|
35
|
-
# alias_method :save, :save_optimistic
|
36
|
-
# end
|
37
47
|
end
|
38
48
|
|
39
49
|
ActiveSupport.on_load(:active_record) do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tp_record_optimistic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Fabricio Oliveira
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-05-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|