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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a0812f2e67144280ab20dd446f5bd07018b59ca9
4
- data.tar.gz: e71c43c92c1cd62774801f6d838a3c8596f19ba4
3
+ metadata.gz: fbca33d6b0e92edde3ab4db1a217a382e70cb4fd
4
+ data.tar.gz: 9f64286739ee114ce953d45b04f81ad6bdf33ba5
5
5
  SHA512:
6
- metadata.gz: e68d3cabd0f850cab506af17fe72dfed9d0bf0cc4bcd33624b45a359c2e0b3bf65ac2122e8129843090d8ff25f7e5d06a6208337446f1537294f19ef50b2fede
7
- data.tar.gz: edec40435139d336435431e5838c956032cfd638b2581a450ca29e6662eae6d6219e06fa207210d1f1d10328a83d643791b589d29b35792df177b03a9e3820a9
6
+ metadata.gz: bc03bc554b965bc73bbabae31068efc5ee27088aeda5fe75a8e1f5805c14175f3e8b6e263910b9f606a3f9600d682d55a096b58c6ef7c20fc43907161825bb6e
7
+ data.tar.gz: ea5132f6498f35677a60d5e86ce37bff1d4bfb304320d7db7ae7761ef9b8ee40b2e204d4078e41c6c201faada68f8b4aeae17cd841289811b63c883e0b26fc9c
data/CHANGELOG CHANGED
@@ -1,3 +1,8 @@
1
+ 0.1.3
2
+ - Fix typo in error message
3
+ - Try and be locale independent when determining the violated index' name
4
+ - Clean up and restructure the exception processing code
5
+
1
6
  0.1.2
2
7
  - Drop pg version in gem dependencies
3
8
  - Add CHANGELOG
@@ -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
- e.message.match( UNIQUE_INDEX_VIOLATION_RE ) do |match|
24
- attribute = self.class._uniqueness_error_attribute_for( match[1] )
35
+ klass = self.class
36
+ raise unless klass.respond_to?( :connection )
25
37
 
26
- if !attribute
27
- #TODO: Test this
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
- self.errors.add( attribute, :taken, :value => self.send( attribute ) )
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.2"
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
@@ -9,6 +9,7 @@ gem 'pg'
9
9
 
10
10
  gem 'rspec-rails', :group => :test
11
11
  gem 'database_cleaner', :group => :test
12
+ gem 'byebug', :group => :test
12
13
 
13
14
 
14
15
 
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.2
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.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