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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 18b8156926d15c46a8ffdc916f1b093cf50c9aeb
4
- data.tar.gz: 46322a6464c78088d2aff604926e12d7307f6f7e
3
+ metadata.gz: 0dd5d0fcffaddb10783744f3eaf79d415d6858dc
4
+ data.tar.gz: f2aad750e85c444a214405fe0acb050d9eb3131e
5
5
  SHA512:
6
- metadata.gz: 8e76fbe00a06098a7117b18dc78b1119cbe37ba50baa8ae706e2baf498db5ff958aa6876ad8dcb19c9e1527fa483168afca19f9bebc34308ad5c0618793a4abd
7
- data.tar.gz: e10d80cad8300d6e21836d4ab1b9288cc05bc6b6fe18358749322db748b0bb2953c1f42239ac04b6b7f33545e04c448d3987bd2af0207a9311db02512ac05be3
6
+ metadata.gz: c78aa6ba5d05359ef3a8bddb85653d959f8e3257961d5d360ad11337a64e3bb21ce25e0da2c9720d7db9d97ab5df7397da697fa7b22c2ac89d23179a7a9efb7c
7
+ data.tar.gz: 86ffef16fcd8e43e7dc4fc95e267e8bc3d6ce58d469801413a351b54a4a064cb42c789d370cd0957d4bce80d38bfb2c3609fad82099c8fed93c1ce8cade132e6
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
  ActiveSupport::Inflector.inflections(:en) do |inflect|
3
3
  inflect.acronym 'TP'
4
+ inflect.acronym 'SQL'
4
5
  end
@@ -1,9 +1,21 @@
1
1
  # frozen_string_literal: true
2
2
  require 'active_record' unless defined? ActiveRecord
3
- # require 'active_model' unless defined? ActiveModel
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
- errors.add('all', e.to_s)
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
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module TPRecordOptimistic
3
- VERSION = '0.2.0'
3
+ VERSION = '0.2.1'
4
4
  end
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.0
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-03-16 00:00:00.000000000 Z
11
+ date: 2017-05-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails