activerecord-rescue_from_duplicate 0.0.7 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.travis.yml +9 -7
- data/Rakefile +6 -9
- data/{spec/gemfiles → gemfiles}/Gemfile.ar-3.2 +1 -1
- data/{spec/gemfiles → gemfiles}/Gemfile.ar-4.0 +1 -1
- data/{spec/gemfiles → gemfiles}/Gemfile.ar-4.1 +1 -1
- data/gemfiles/Gemfile.ar-4.2 +10 -0
- data/{spec/gemfiles → gemfiles}/Gemfile.ar-edge +1 -1
- data/lib/rescue_from_duplicate/active_record/extension.rb +18 -12
- data/lib/rescue_from_duplicate/active_record/version.rb +1 -1
- data/spec/spec_helper.rb +0 -2
- metadata +8 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 99a2e09bb6ac6774ef2e6e452cc1be0a1db87799
|
4
|
+
data.tar.gz: 1ae17a3c73dde0a7aa54c13f86d2a6d3b140c7f1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2d49bfe997a680e977e134344045aaf453afaf5da46a437522140e0dd50a3c5eb0ec72e6bcb03ceb6a8ee57136509abb7cde77daa8e9f0c4d8a94cfd3810f521
|
7
|
+
data.tar.gz: 4686cbe3e99568fb77f75bd6cede972053a49ad4b1cb64520a64ee4d793f3754921ee1e89d64ea3a33fc1ed524731aa836bf4e8d931ea3686a64741daf89b957
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
@@ -1,12 +1,14 @@
|
|
1
|
+
sudo: false
|
1
2
|
rvm:
|
2
|
-
-
|
3
|
-
- 2.
|
4
|
-
- 2.
|
3
|
+
- 2.0
|
4
|
+
- 2.1
|
5
|
+
- 2.2
|
5
6
|
gemfile:
|
6
|
-
-
|
7
|
-
-
|
8
|
-
-
|
9
|
-
-
|
7
|
+
- gemfiles/Gemfile.ar-3.2
|
8
|
+
- gemfiles/Gemfile.ar-4.0
|
9
|
+
- gemfiles/Gemfile.ar-4.1
|
10
|
+
- gemfiles/Gemfile.ar-4.2
|
11
|
+
- gemfiles/Gemfile.ar-edge
|
10
12
|
|
11
13
|
before_script:
|
12
14
|
- mysql -e 'create database rescue_from_duplicate;'
|
data/Rakefile
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require 'bundler/gem_tasks'
|
2
2
|
require 'rspec/core/rake_task'
|
3
3
|
|
4
4
|
RSpec::Core::RakeTask.new(:spec)
|
@@ -7,14 +7,11 @@ task :default => :spec
|
|
7
7
|
|
8
8
|
namespace :spec do
|
9
9
|
task :all do
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
},
|
16
|
-
"rspec"
|
17
|
-
)
|
10
|
+
Dir[File.expand_path('../gemfiles/*.lock', __FILE__)].each { |f| File.delete(f) }
|
11
|
+
Dir[File.expand_path('../gemfiles/*', __FILE__)].each do |gemfile|
|
12
|
+
env = {'BUNDLE_GEMFILE' => gemfile, 'MYSQL' => '1'}
|
13
|
+
system(env, 'bundle install')
|
14
|
+
system(env, 'bundle exec rspec')
|
18
15
|
end
|
19
16
|
end
|
20
17
|
end
|
@@ -24,22 +24,28 @@ module RescueFromDuplicate::ActiveRecord
|
|
24
24
|
|
25
25
|
def create_or_update(*params, &block)
|
26
26
|
super
|
27
|
-
rescue ActiveRecord::RecordNotUnique => exception
|
28
|
-
|
27
|
+
rescue ActiveRecord::RecordNotUnique, ActiveRecord::StatementInvalid => exception
|
28
|
+
raise unless handle_unicity_error(exception)
|
29
|
+
false
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
29
33
|
|
30
|
-
|
34
|
+
def handle_unicity_error(exception)
|
35
|
+
handler = exception_handler(exception)
|
36
|
+
return false unless handler
|
31
37
|
|
32
38
|
attribute = handler.attributes.first
|
33
39
|
options = handler.options.except(:case_sensitive, :scope).merge(value: self.send(:read_attribute_for_validation, attribute))
|
34
40
|
|
35
41
|
self.errors.add(attribute, :taken, options)
|
36
|
-
|
42
|
+
true
|
37
43
|
end
|
38
44
|
|
39
|
-
private
|
40
|
-
|
41
45
|
def exception_handler(exception)
|
42
46
|
columns = exception_columns(exception)
|
47
|
+
return unless columns
|
48
|
+
columns.sort!
|
43
49
|
|
44
50
|
self.class._rescue_from_duplicate_handlers.detect do |handler|
|
45
51
|
handler.rescue? && columns == handler.columns
|
@@ -47,14 +53,13 @@ module RescueFromDuplicate::ActiveRecord
|
|
47
53
|
end
|
48
54
|
|
49
55
|
def exception_columns(exception)
|
50
|
-
|
51
|
-
when exception.message =~ /SQLite3::ConstraintException/
|
56
|
+
if exception.message =~ /SQLite3::ConstraintException/
|
52
57
|
sqlite3_exception_columns(exception)
|
53
|
-
|
58
|
+
elsif exception.message =~ /PG::UniqueViolation/
|
54
59
|
postgresql_exception_columns(exception)
|
55
60
|
else
|
56
61
|
other_exception_columns(exception)
|
57
|
-
end
|
62
|
+
end
|
58
63
|
end
|
59
64
|
|
60
65
|
def postgresql_exception_columns(exception)
|
@@ -62,12 +67,13 @@ module RescueFromDuplicate::ActiveRecord
|
|
62
67
|
end
|
63
68
|
|
64
69
|
def sqlite3_exception_columns(exception)
|
65
|
-
extract_columns(exception.message[/columns? (.*) (?:is|are) not unique/, 1])
|
70
|
+
extract_columns(exception.message[/columns? (.*) (?:is|are) not unique/, 1]) ||
|
71
|
+
extract_columns(exception.message[/UNIQUE constraint failed: ([^:]*)\:/, 1])
|
66
72
|
end
|
67
73
|
|
68
74
|
def extract_columns(columns_string)
|
69
75
|
return unless columns_string
|
70
|
-
columns_string.split(",").map(
|
76
|
+
columns_string.split(",").map { |column| column.split('.').last.strip }
|
71
77
|
end
|
72
78
|
|
73
79
|
def other_exception_columns(exception)
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord-rescue_from_duplicate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Guillaume Malette
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-09-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -151,6 +151,11 @@ files:
|
|
151
151
|
- README.md
|
152
152
|
- Rakefile
|
153
153
|
- activerecord-rescue_from_duplicate.gemspec
|
154
|
+
- gemfiles/Gemfile.ar-3.2
|
155
|
+
- gemfiles/Gemfile.ar-4.0
|
156
|
+
- gemfiles/Gemfile.ar-4.1
|
157
|
+
- gemfiles/Gemfile.ar-4.2
|
158
|
+
- gemfiles/Gemfile.ar-edge
|
154
159
|
- lib/activerecord-rescue_from_duplicate.rb
|
155
160
|
- lib/rescue_from_duplicate/active_record.rb
|
156
161
|
- lib/rescue_from_duplicate/active_record/extension.rb
|
@@ -159,10 +164,6 @@ files:
|
|
159
164
|
- lib/rescue_from_duplicate/rescuer.rb
|
160
165
|
- lib/rescue_from_duplicate/uniqueness_rescuer.rb
|
161
166
|
- shipit.rubygems.yml
|
162
|
-
- spec/gemfiles/Gemfile.ar-3.2
|
163
|
-
- spec/gemfiles/Gemfile.ar-4.0
|
164
|
-
- spec/gemfiles/Gemfile.ar-4.1
|
165
|
-
- spec/gemfiles/Gemfile.ar-edge
|
166
167
|
- spec/rescue_from_duplicate_spec.rb
|
167
168
|
- spec/rescuer_spec.rb
|
168
169
|
- spec/spec_helper.rb
|
@@ -189,16 +190,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
189
190
|
version: '0'
|
190
191
|
requirements: []
|
191
192
|
rubyforge_project:
|
192
|
-
rubygems_version: 2.2.
|
193
|
+
rubygems_version: 2.2.3
|
193
194
|
signing_key:
|
194
195
|
specification_version: 4
|
195
196
|
summary: Rescue from MySQL and Sqlite duplicate errors when trying to insert records
|
196
197
|
that fail uniqueness validation
|
197
198
|
test_files:
|
198
|
-
- spec/gemfiles/Gemfile.ar-3.2
|
199
|
-
- spec/gemfiles/Gemfile.ar-4.0
|
200
|
-
- spec/gemfiles/Gemfile.ar-4.1
|
201
|
-
- spec/gemfiles/Gemfile.ar-edge
|
202
199
|
- spec/rescue_from_duplicate_spec.rb
|
203
200
|
- spec/rescuer_spec.rb
|
204
201
|
- spec/spec_helper.rb
|