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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 95ffd1c815f0296026de14cf92106766488a5472
4
- data.tar.gz: 2f54109b6dbb3330942a64ce0644f95529e2431a
3
+ metadata.gz: 99a2e09bb6ac6774ef2e6e452cc1be0a1db87799
4
+ data.tar.gz: 1ae17a3c73dde0a7aa54c13f86d2a6d3b140c7f1
5
5
  SHA512:
6
- metadata.gz: 76a7947095f99a17df0607fa139e45949ba4649f6bed2e9e1ee7efe2992863e697c9f0a285060c8003bc8c9282fb21478833859fd583f1fb02a8c54425a0dda4
7
- data.tar.gz: 6e9d39085c3614245854a19023cdd4cd06d02ac8e20a7eee7f6ac2119b2b4fc78a92cef7f40f9b564b202af3cf4954c9b02f5bb1b68720e4aec0d664e721bf69
6
+ metadata.gz: 2d49bfe997a680e977e134344045aaf453afaf5da46a437522140e0dd50a3c5eb0ec72e6bcb03ceb6a8ee57136509abb7cde77daa8e9f0c4d8a94cfd3810f521
7
+ data.tar.gz: 4686cbe3e99568fb77f75bd6cede972053a49ad4b1cb64520a64ee4d793f3754921ee1e89d64ea3a33fc1ed524731aa836bf4e8d931ea3686a64741daf89b957
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ /gemfiles/*.lock
@@ -1,12 +1,14 @@
1
+ sudo: false
1
2
  rvm:
2
- - 1.9.3
3
- - 2.0.0
4
- - 2.1.2
3
+ - 2.0
4
+ - 2.1
5
+ - 2.2
5
6
  gemfile:
6
- - spec/gemfiles/Gemfile.ar-3.2
7
- - spec/gemfiles/Gemfile.ar-4.0
8
- - spec/gemfiles/Gemfile.ar-4.1
9
- - spec/gemfiles/Gemfile.ar-edge
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 "bundler/gem_tasks"
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
- %w(3.2 4.0 4.1 edge).each do |ar_version|
11
- system(
12
- {
13
- "BUNDLE_GEMFILE" => "spec/gemfiles/Gemfile.ar-#{ar_version}",
14
- "MYSQL" => "1"
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
@@ -6,5 +6,5 @@ gem 'rake'
6
6
  gem 'rspec'
7
7
  gem 'sqlite3'
8
8
  gem 'pg', '~> 0.11'
9
- gem 'mysql2'
9
+ gem 'mysql2', '~> 0.3.20'
10
10
  gem 'simplecov', require: false
@@ -6,5 +6,5 @@ gem 'rake'
6
6
  gem 'rspec'
7
7
  gem 'sqlite3'
8
8
  gem 'pg', '~> 0.11'
9
- gem 'mysql2'
9
+ gem 'mysql2', '~> 0.3.20'
10
10
  gem 'simplecov', require: false
@@ -6,5 +6,5 @@ gem 'rake'
6
6
  gem 'rspec'
7
7
  gem 'sqlite3'
8
8
  gem 'pg', '~> 0.11'
9
- gem 'mysql2'
9
+ gem 'mysql2', '~> 0.3.20'
10
10
  gem 'simplecov', require: false
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'activerecord', '~> 4.2.0'
4
+ gem 'bundler', '~> 1.3'
5
+ gem 'rake'
6
+ gem 'rspec'
7
+ gem 'sqlite3'
8
+ gem 'pg', '~> 0.11'
9
+ gem 'mysql2', '~> 0.3.20'
10
+ gem 'simplecov', require: false
@@ -8,5 +8,5 @@ gem 'rake'
8
8
  gem 'rspec'
9
9
  gem 'sqlite3'
10
10
  gem 'pg', '~> 0.11'
11
- gem 'mysql2'
11
+ gem 'mysql2', '~> 0.3.20'
12
12
  gem 'simplecov', require: false
@@ -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
- handler = exception_handler(exception)
27
+ rescue ActiveRecord::RecordNotUnique, ActiveRecord::StatementInvalid => exception
28
+ raise unless handle_unicity_error(exception)
29
+ false
30
+ end
31
+
32
+ private
29
33
 
30
- raise exception unless handler
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
- false
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
- case
51
- when exception.message =~ /SQLite3::ConstraintException/
56
+ if exception.message =~ /SQLite3::ConstraintException/
52
57
  sqlite3_exception_columns(exception)
53
- when exception.message =~ /PG::UniqueViolation/
58
+ elsif exception.message =~ /PG::UniqueViolation/
54
59
  postgresql_exception_columns(exception)
55
60
  else
56
61
  other_exception_columns(exception)
57
- end.sort
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(&:strip)
76
+ columns_string.split(",").map { |column| column.split('.').last.strip }
71
77
  end
72
78
 
73
79
  def other_exception_columns(exception)
@@ -1,5 +1,5 @@
1
1
  module Activerecord
2
2
  module RescueFromDuplicate
3
- VERSION = "0.0.7"
3
+ VERSION = '0.1.0'
4
4
  end
5
5
  end
@@ -15,8 +15,6 @@ begin
15
15
  rescue LoadError
16
16
  end
17
17
 
18
-
19
-
20
18
  module RescueFromDuplicate
21
19
  class Base
22
20
  cattr_accessor :exception
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.7
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: 2014-10-15 00:00:00.000000000 Z
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.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