record_not_unique 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +20 -0
- data/lib/record_not_unique.rb +82 -0
- metadata +87 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ebcd2c56191ea42deafd1d38ca5762414a0ce9c4b2ec00fdfcc7e406ea64f707
|
4
|
+
data.tar.gz: 71740396b18e585bbbc49705558ecb580683a15b6afa5db8866194070cea6eb5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ee5d7678af9fcf36f6fe8f1f56cfe1b1e10b3dc7dbf6c4f3972dd53cb9d0fd7cac121e893b6eae51619564be9ec5acf614c7ceec6b3f89d9b634918dda70772e
|
7
|
+
data.tar.gz: 17b7b48b92286afad3ddedb0e87f2d4f72cdc9ef3e3b830c00f1ca8131141b002b0648084ab398d962352128c63e6f670fac8688f1cf4456109d144028a9de7b
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2017 FRESHWORKS, INC.
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@@ -0,0 +1,82 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RecordNotUnique
|
4
|
+
module ClassMethods
|
5
|
+
def handle_record_not_unique(*args)
|
6
|
+
# need to be shared across child classes as indexes would be common
|
7
|
+
class_eval do
|
8
|
+
cattr_accessor :_rnu_indexes, :_rnu_error_messages
|
9
|
+
|
10
|
+
self._rnu_indexes = []
|
11
|
+
self._rnu_error_messages = []
|
12
|
+
_indexes = connection.indexes(table_name)
|
13
|
+
args.each do |arg|
|
14
|
+
raise NotImplementedError unless arg.key?(:field) && arg.key?(:message)
|
15
|
+
raise 'field should be an array' unless arg[:field].is_a?(Array)
|
16
|
+
_index = _indexes.detect { |index| index.columns.eql?(arg[:field]) }
|
17
|
+
raise "Couldn't find unique index for field #{arg[:field].join(',')}" if !_index&.unique
|
18
|
+
self._rnu_indexes << _index.name
|
19
|
+
self._rnu_error_messages << arg[:message].to_a.flatten
|
20
|
+
end
|
21
|
+
prepend InstanceMethods
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
module InstanceMethods
|
27
|
+
# handle update_column for rails3, update_columns for rails4+
|
28
|
+
if ActiveRecord::VERSION::MAJOR < 4
|
29
|
+
def update_column(name, value)
|
30
|
+
handle_custom_unique_constraint {
|
31
|
+
super(name, value)
|
32
|
+
}
|
33
|
+
end
|
34
|
+
else
|
35
|
+
def update_columns(attributes)
|
36
|
+
handle_custom_unique_constraint {
|
37
|
+
super(attributes)
|
38
|
+
}
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
if ActiveRecord::VERSION::MAJOR >=6
|
43
|
+
def save(**options, &block)
|
44
|
+
handle_custom_unique_constraint {
|
45
|
+
super
|
46
|
+
}
|
47
|
+
end
|
48
|
+
elsif ActiveRecord::VERSION::MAJOR >=5
|
49
|
+
def save(*args, &block)
|
50
|
+
handle_custom_unique_constraint {
|
51
|
+
super
|
52
|
+
}
|
53
|
+
end
|
54
|
+
else
|
55
|
+
def save(*)
|
56
|
+
handle_custom_unique_constraint {
|
57
|
+
super
|
58
|
+
}
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
def handle_custom_unique_constraint
|
64
|
+
begin
|
65
|
+
yield
|
66
|
+
rescue ActiveRecord::RecordNotUnique => e
|
67
|
+
self._rnu_indexes.each_with_index { |index_name, i|
|
68
|
+
if e.message.include?(index_name)
|
69
|
+
custom_error = self.class._rnu_error_messages[i]
|
70
|
+
object = custom_error.last
|
71
|
+
custom_error_msg = object.is_a?(Proc) ? object.call(self) : object
|
72
|
+
|
73
|
+
self.errors.add(custom_error.first, custom_error_msg)
|
74
|
+
end
|
75
|
+
}
|
76
|
+
false
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
ActiveRecord::Base.extend(RecordNotUnique::ClassMethods)
|
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: record_not_unique
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ritikesh G
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-07-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: mysql2
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Handle ActiveRecord::RecordNotUnique exceptions gracefully with customisable
|
56
|
+
error messages
|
57
|
+
email: ritikesh.ganpathraj@freshworks.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- LICENSE
|
63
|
+
- lib/record_not_unique.rb
|
64
|
+
homepage: http://rubygems.org/gems/record_not_unique
|
65
|
+
licenses:
|
66
|
+
- MIT
|
67
|
+
metadata: {}
|
68
|
+
post_install_message:
|
69
|
+
rdoc_options: []
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
requirements: []
|
83
|
+
rubygems_version: 3.0.6
|
84
|
+
signing_key:
|
85
|
+
specification_version: 4
|
86
|
+
summary: Handle ActiveRecord::RecordNotUnique exceptions gracefully!
|
87
|
+
test_files: []
|