activerecord-rescue_from_duplicate 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- ZDA5ZTEwOWE2NjIxMTQ5MWY0MDI0YTM2NDZmNzFlYTkwZDE4NGQzMw==
5
- data.tar.gz: !binary |-
6
- NThhZTcxOTExYzkyZjZhNTYyYjFiZTJlZjRhODdmZWM2MjEwZDNmNg==
2
+ SHA1:
3
+ metadata.gz: 4d59c5d63f6929f47bcf86422995f8cd95893edf
4
+ data.tar.gz: 547f6c3633d0df084d64cc85c5afbd50a45db1a1
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- NDljN2M4MjcxZmUxMjhhNGRjNTgyZDk3YThkNTBkYzEzODc3MWNjMmNkMTE2
10
- ZWI0ZDQ3YmMzN2EwMTUwNTVjNmEyYmE4MGY5ODQxODJhNDU5ZWJmYTZjYjU4
11
- YjUwZDI0MjUwNjcyYTI0MTJmNjdmMWU0YzQ4ZWRiZDk2OTExNmU=
12
- data.tar.gz: !binary |-
13
- MzRhMDQ1MmM2MTdmZGJiZTRmZGFmNGQ5ZjdhYWY3MDJkNGRkZjI2MzRiMTJi
14
- NmEzMjJhYTIzYjA0MjQ4NWZmMDMyMzZhNThiZjgyMzFjNDIxOWI5MDY4MTU2
15
- Mjg2NjhlZGUzOWQ2Y2EwYTY2MzQ2YzZmZTYzMTBiOGE0YjliOGE=
6
+ metadata.gz: 8195739d842dc67951c013505698d11ac0fb591dd70fa1723eb17cc153c1b68d92b9dbf8ddd750de1043e69e64b4ae6d8b792c4d57e2a4371c62bff52d0012d6
7
+ data.tar.gz: 77fe96c1d679bb986c413713c645575e59539335d1a8d1b5acfc584d35c2762c144a386c8fa9fd7d2c5befa8a32822a3b181e5c0488920bfa01c94005465f1f7
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # ActiveRecord - RescueFromDuplicate
2
2
 
3
+ [![Build Status](https://travis-ci.org/Shopify/activerecord-rescue_from_duplicate.png?branch=master)](https://travis-ci.org/Shopify/activerecord-rescue_from_duplicate)
4
+
3
5
  This gem will rescue from MySQL and Sqlite errors when trying to insert records that fail uniqueness validation.
4
6
  PostgreSQL is not supported at the moment because of the errors raised when using prepared statements.
5
7
 
@@ -4,10 +4,10 @@ module RescueFromDuplicate::ActiveRecord
4
4
  module Extension
5
5
  def create_or_update(*params, &block)
6
6
  super
7
- rescue ActiveRecord::RecordNotUnique => e
8
- validator = exception_validator(e)
7
+ rescue ActiveRecord::RecordNotUnique => exception
8
+ validator = exception_validator(exception)
9
9
 
10
- raise e unless validator
10
+ raise exception unless validator
11
11
 
12
12
  attribute = validator.attributes.first
13
13
  options = validator.options.except(:case_sensitive, :scope).merge(:value => self.send(:read_attribute_for_validation, attribute))
@@ -43,7 +43,7 @@ module RescueFromDuplicate::ActiveRecord
43
43
 
44
44
  def other_exception_columns(exception)
45
45
  indexes = self.class.connection.indexes(self.class.table_name)
46
- indexes.detect{ |i| exception.message.include?(i.name) }.try(:columns)
46
+ indexes.detect{ |i| exception.message.include?(i.name) }.try(:columns) || []
47
47
  end
48
48
 
49
49
  def rescue_with_validator(columns, validator)
@@ -1,5 +1,5 @@
1
1
  module Activerecord
2
2
  module RescueFromDuplicate
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
@@ -12,11 +12,11 @@ shared_examples 'database error rescuing' do
12
12
 
13
13
  describe "#exception_columns" do
14
14
  context "index cannot be found" do
15
- let(:message) { super().gsub(/index_\w+/, "index_toto").gsub(/column .* is/, 'toto') }
15
+ let(:message) { super().gsub(/index_\w+/, "index_toto").gsub(/column .* is/, 'column toto is') }
16
16
  let(:exception) { ActiveRecord::RecordNotUnique.new(message, nil) }
17
17
 
18
- it "returns nil" do
19
- expect(subject.exception_columns(exception)).to be_nil
18
+ it "returns an array" do
19
+ expect(subject.exception_columns(exception)).to be_a Array
20
20
  end
21
21
  end
22
22
 
@@ -53,6 +53,39 @@ shared_examples 'database error rescuing' do
53
53
  expect(subject.exception_validator(uniqueness_exception)).to be_nil
54
54
  end
55
55
  end
56
+
57
+ context "no validator" do
58
+ before {
59
+ Rescuable.stub(:_validators => {})
60
+ }
61
+
62
+ it "returns nil" do
63
+ expect(subject.exception_validator(uniqueness_exception)).to be_nil
64
+ end
65
+ end
66
+
67
+ context "no index on the table" do
68
+ before {
69
+ Rescuable.stub(:index => nil)
70
+ Rescuable.stub(:connection => double(:indexes => []))
71
+ }
72
+
73
+ let(:message) { super().gsub(/column (.*) is/, 'column toto is') }
74
+
75
+ it "returns nil" do
76
+ expect(subject.exception_validator(uniqueness_exception)).to be_nil
77
+ end
78
+ end
79
+
80
+ context "columns part of the index of another table" do
81
+ before {
82
+ subject.stub(:exception_columns => ['foo', 'baz'])
83
+ }
84
+
85
+ it "returns nil" do
86
+ expect(subject.exception_validator(uniqueness_exception)).to be_nil
87
+ end
88
+ end
56
89
  end
57
90
 
58
91
  describe "#create_or_update when the validation fails" do
@@ -8,8 +8,8 @@ AR_4_1 = Gem::Version.new('4.1.0.beta')
8
8
 
9
9
  ActiveRecord::Base.configurations = {
10
10
  'test_sqlite3' => {adapter: 'sqlite3', database: "/tmp/rescue_from_duplicate.db"},
11
- 'test_postgresql' => {adapter: 'postgresql', database: 'gmalette', username: 'gmalette', host: '127.0.0.1'},
12
- 'test_mysql' => {adapter: 'mysql2', database: 'rescue_from_duplicate', username: 'root', host: '127.0.0.1', port: 13306},
11
+ 'test_postgresql' => {adapter: 'postgresql', database: 'rescue_from_duplicate', username: 'postgres'},
12
+ 'test_mysql' => {adapter: 'mysql2', database: 'rescue_from_duplicate', username: 'travis'},
13
13
  }
14
14
 
15
15
  class CreateAllTables < ActiveRecord::Migration
metadata CHANGED
@@ -1,27 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-rescue_from_duplicate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Guillaume Malette
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-26 00:00:00.000000000 Z
11
+ date: 2013-12-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ! '>='
17
+ - - '>='
18
18
  - !ruby/object:Gem::Version
19
19
  version: '3.2'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ! '>='
24
+ - - '>='
25
25
  - !ruby/object:Gem::Version
26
26
  version: '3.2'
27
27
  - !ruby/object:Gem::Dependency
@@ -42,112 +42,112 @@ dependencies:
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ! '>='
45
+ - - '>='
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ! '>='
52
+ - - '>='
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: coveralls
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ! '>='
59
+ - - '>='
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ! '>='
66
+ - - '>='
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: sqlite3
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ! '>='
73
+ - - '>='
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ! '>='
80
+ - - '>='
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: pg
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - ! '>='
87
+ - - '>='
88
88
  - !ruby/object:Gem::Version
89
89
  version: '0'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - ! '>='
94
+ - - '>='
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: mysql2
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - ! '>='
101
+ - - '>='
102
102
  - !ruby/object:Gem::Version
103
103
  version: '0'
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
- - - ! '>='
108
+ - - '>='
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
111
  - !ruby/object:Gem::Dependency
112
112
  name: rspec
113
113
  requirement: !ruby/object:Gem::Requirement
114
114
  requirements:
115
- - - ! '>='
115
+ - - '>='
116
116
  - !ruby/object:Gem::Version
117
117
  version: '0'
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
- - - ! '>='
122
+ - - '>='
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
125
  - !ruby/object:Gem::Dependency
126
126
  name: pry
127
127
  requirement: !ruby/object:Gem::Requirement
128
128
  requirements:
129
- - - ! '>='
129
+ - - '>='
130
130
  - !ruby/object:Gem::Version
131
131
  version: '0'
132
132
  type: :development
133
133
  prerelease: false
134
134
  version_requirements: !ruby/object:Gem::Requirement
135
135
  requirements:
136
- - - ! '>='
136
+ - - '>='
137
137
  - !ruby/object:Gem::Version
138
138
  version: '0'
139
139
  - !ruby/object:Gem::Dependency
140
140
  name: pry-debugger
141
141
  requirement: !ruby/object:Gem::Requirement
142
142
  requirements:
143
- - - ! '>='
143
+ - - '>='
144
144
  - !ruby/object:Gem::Version
145
145
  version: '0'
146
146
  type: :development
147
147
  prerelease: false
148
148
  version_requirements: !ruby/object:Gem::Requirement
149
149
  requirements:
150
- - - ! '>='
150
+ - - '>='
151
151
  - !ruby/object:Gem::Version
152
152
  version: '0'
153
153
  description: Rescue from MySQL and Sqlite duplicate errors
@@ -186,17 +186,17 @@ require_paths:
186
186
  - lib
187
187
  required_ruby_version: !ruby/object:Gem::Requirement
188
188
  requirements:
189
- - - ! '>='
189
+ - - '>='
190
190
  - !ruby/object:Gem::Version
191
191
  version: '0'
192
192
  required_rubygems_version: !ruby/object:Gem::Requirement
193
193
  requirements:
194
- - - ! '>='
194
+ - - '>='
195
195
  - !ruby/object:Gem::Version
196
196
  version: '0'
197
197
  requirements: []
198
198
  rubyforge_project:
199
- rubygems_version: 2.1.9
199
+ rubygems_version: 2.0.14
200
200
  signing_key:
201
201
  specification_version: 4
202
202
  summary: Rescue from MySQL and Sqlite duplicate errors when trying to insert records
@@ -209,4 +209,3 @@ test_files:
209
209
  - spec/spec_helper.rb
210
210
  - spec/support/model.rb
211
211
  - spec/validator_spec.rb
212
- has_rdoc: