proper_active_record_uniqueness_validations 0.1.2 → 0.1.3
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/CHANGELOG +5 -0
- data/lib/proper_uniqueness_validation.rb +24 -10
- data/proper_active_record_uniqueness_validations.gemspec +1 -1
- data/test/Gemfile +1 -0
- data/test/Gemfile.lock +6 -0
- 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: fbca33d6b0e92edde3ab4db1a217a382e70cb4fd
|
4
|
+
data.tar.gz: 9f64286739ee114ce953d45b04f81ad6bdf33ba5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bc03bc554b965bc73bbabae31068efc5ee27088aeda5fe75a8e1f5805c14175f3e8b6e263910b9f606a3f9600d682d55a096b58c6ef7c20fc43907161825bb6e
|
7
|
+
data.tar.gz: ea5132f6498f35677a60d5e86ce37bff1d4bfb304320d7db7ae7761ef9b8ee40b2e204d4078e41c6c201faada68f8b4aeae17cd841289811b63c883e0b26fc9c
|
data/CHANGELOG
CHANGED
@@ -1,10 +1,22 @@
|
|
1
1
|
module ProperUniquenessValidation
|
2
|
-
UNIQUE_INDEX_VIOLATION_RE = %r{duplicate key value violates unique constraint "([^"]+)"}
|
3
|
-
|
4
2
|
def self.included( base )
|
5
3
|
base.extend ClassMethods
|
6
4
|
end
|
7
5
|
|
6
|
+
def self.extract_index_name_from_exception( connection, exception )
|
7
|
+
case connection
|
8
|
+
when ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
|
9
|
+
extract_index_name_from_postgres_exception( exception )
|
10
|
+
|
11
|
+
else raise exception
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.extract_index_name_from_postgres_exception( exception )
|
16
|
+
match = exception.original_exception.result.error_field( PGresult::PG_DIAG_MESSAGE_PRIMARY ).match(/"([^"]+)"/ )
|
17
|
+
match[1]
|
18
|
+
end
|
19
|
+
|
8
20
|
module ClassMethods
|
9
21
|
def uniqueness_error_attribute_for( index_name, attribute_name )
|
10
22
|
@_uniqueness_index_attribute_mapping ||= {}
|
@@ -20,18 +32,20 @@ module ProperUniquenessValidation
|
|
20
32
|
def create_or_update
|
21
33
|
super
|
22
34
|
rescue ActiveRecord::RecordNotUnique => e
|
23
|
-
|
24
|
-
|
35
|
+
klass = self.class
|
36
|
+
raise unless klass.respond_to?( :connection )
|
25
37
|
|
26
|
-
|
27
|
-
|
28
|
-
logger.warn "Caught uniqueness exceptions but index '#{match[1]}' was not registered\nAdd a \"uniquness_error_attribute_for 'index_name', :error_attribute_name\"\nclause to your model '#{self.class}'!"
|
29
|
-
raise
|
30
|
-
end
|
38
|
+
index_name = ProperUniquenessValidation.extract_index_name_from_exception( klass.connection, e )
|
39
|
+
attribute = klass._uniqueness_error_attribute_for( index_name )
|
31
40
|
|
32
|
-
|
41
|
+
if !attribute
|
42
|
+
#TODO: Test this
|
43
|
+
logger.warn "Caught uniqueness exceptions but index '#{index_name}' was not registered\nAdd a \"uniqueness_error_attribute_for 'index_name', :error_attribute_name\"\nclause to your model '#{klass}'!"
|
44
|
+
raise
|
33
45
|
end
|
34
46
|
|
47
|
+
self.errors.add( attribute, :taken, :value => self.send( attribute ) )
|
48
|
+
|
35
49
|
return false
|
36
50
|
end
|
37
51
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = "proper_active_record_uniqueness_validations"
|
3
|
-
s.version = "0.1.
|
3
|
+
s.version = "0.1.3"
|
4
4
|
|
5
5
|
s.required_ruby_version = ">= 1.9.3"
|
6
6
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
data/test/Gemfile
CHANGED
data/test/Gemfile.lock
CHANGED
@@ -30,7 +30,12 @@ GEM
|
|
30
30
|
multi_json (~> 1.0)
|
31
31
|
arel (3.0.2)
|
32
32
|
builder (3.0.4)
|
33
|
+
byebug (1.6.1)
|
34
|
+
columnize (~> 0.3.6)
|
35
|
+
debugger-linecache (~> 1.2.0)
|
36
|
+
columnize (0.3.6)
|
33
37
|
database_cleaner (0.9.1)
|
38
|
+
debugger-linecache (1.2.0)
|
34
39
|
diff-lcs (1.2.1)
|
35
40
|
erubis (2.7.0)
|
36
41
|
hike (1.2.3)
|
@@ -96,6 +101,7 @@ PLATFORMS
|
|
96
101
|
ruby
|
97
102
|
|
98
103
|
DEPENDENCIES
|
104
|
+
byebug
|
99
105
|
database_cleaner
|
100
106
|
pg
|
101
107
|
rails (= 3.2.13)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: proper_active_record_uniqueness_validations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sven Riedel
|
@@ -102,7 +102,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
102
102
|
version: '0'
|
103
103
|
requirements: []
|
104
104
|
rubyforge_project:
|
105
|
-
rubygems_version: 2.0.
|
105
|
+
rubygems_version: 2.0.5
|
106
106
|
signing_key:
|
107
107
|
specification_version: 4
|
108
108
|
summary: Uses unique indexes on the database level to see and deal with uniqueness
|