unique_attributes 0.1.0 → 0.1.2
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 +5 -5
- data/README.md +1 -1
- data/lib/unique_attributes.rb +24 -18
- data/lib/unique_attributes/version.rb +1 -1
- data/spec/config/test_setup_migration.rb +8 -1
- data/spec/postgresql_spec.rb +1 -0
- data/spec/shared/unique_attributes_examples.rb +17 -0
- data/unique_attributes.gemspec +2 -2
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 05ae0b01e29ee3376746119561a42e507acf30c2e0e49ad00bb23202fad8dcb1
|
4
|
+
data.tar.gz: 4e898afbfe1a0668bfebc76b3dac718d9b9879663dc89dde6a4c2fc127dac4b9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 63b6d47cfba4c4d82cf704f31b38e8e4209342af24fd921a92ce09181845a3879a87b3dfb515150ec12519af56c50f615732c50c4a0638b9da70392af50a3eda
|
7
|
+
data.tar.gz: 0c26f90f0323aac83ad3ea09410039c5001bd131ba2b31f3ee258a7270f017a895c74154476496382fed8447fc2724b23d8824ce96d87ce287ba586ee2bb5bff
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
[](https://codeclimate.com/github/panorama-ed/unique_attributes) [](https://codeclimate.com/github/panorama-ed/unique_attributes) [](http://inch-ci.org/github/panorama-ed/unique_attributes) [](https://travis-ci.org/panorama-ed/unique_attributes)
|
1
|
+
[](https://codeclimate.com/github/panorama-ed/unique_attributes) [](https://codeclimate.com/github/panorama-ed/unique_attributes) [](http://inch-ci.org/github/panorama-ed/unique_attributes) [](https://travis-ci.org/panorama-ed/unique_attributes) [](http://badge.fury.io/rb/unique_attributes)
|
2
2
|
|
3
3
|
# UniqueAttributes
|
4
4
|
|
data/lib/unique_attributes.rb
CHANGED
@@ -64,6 +64,8 @@ module UniqueAttributes
|
|
64
64
|
# If we have blank unique attributes.
|
65
65
|
if blank_attrs.size > 0
|
66
66
|
attempts = 0
|
67
|
+
attr_group = "(?<attr>#{blank_attrs.keys.join('|')})"
|
68
|
+
other_fields = "(, [\\w`'\".]+)*"
|
67
69
|
|
68
70
|
# Keep retrying until the save works.
|
69
71
|
while !self.persisted?
|
@@ -77,25 +79,29 @@ module UniqueAttributes
|
|
77
79
|
yield # Perform the save, and see if it works.
|
78
80
|
end
|
79
81
|
rescue ActiveRecord::RecordNotUnique => error
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
82
|
+
if attempts <= SAVE_ATTEMPTS_LIMIT
|
83
|
+
match = [
|
84
|
+
# Postgres
|
85
|
+
/Key \(#{attr_group}#{other_fields}\)=\([\w\s,]*\) already exists/,
|
86
|
+
# SQLite
|
87
|
+
/column(s)? #{attr_group}#{other_fields} (is|are) not unique/,
|
88
|
+
/UNIQUE constraint failed: #{self.class.table_name}\.#{attr_group}#{other_fields}:/
|
89
|
+
].inject(nil) { |m, regex| m || regex.match(error.message) }
|
90
|
+
|
91
|
+
# If we've managed to hit the same unique attribute of a record
|
92
|
+
# already in the database, then we should wipe the attribute and
|
93
|
+
# try again
|
94
|
+
if match
|
95
|
+
attr = match[:attr].to_sym
|
96
|
+
blank_attrs = { attr => self.class.unique_attributes[attr] }
|
97
|
+
write_attribute(attr, nil)
|
98
|
+
next
|
99
|
+
end
|
98
100
|
end
|
101
|
+
|
102
|
+
# If we're already at the attempts limit, or some other attribute
|
103
|
+
# was the problem, let the error propagate.
|
104
|
+
raise error
|
99
105
|
end
|
100
106
|
end
|
101
107
|
else # If the unique values are already set, perform a regular save.
|
@@ -1,4 +1,11 @@
|
|
1
|
-
|
1
|
+
migration_class =
|
2
|
+
if ActiveRecord::VERSION::MAJOR >= 5
|
3
|
+
ActiveRecord::Migration[4.2]
|
4
|
+
else
|
5
|
+
ActiveRecord::Migration
|
6
|
+
end
|
7
|
+
|
8
|
+
class TestSetupMigration < migration_class
|
2
9
|
def up
|
3
10
|
return if ActiveRecord::Base.connection.table_exists? :test_classes
|
4
11
|
|
data/spec/postgresql_spec.rb
CHANGED
@@ -109,5 +109,22 @@ RSpec.shared_examples ".unique_attribute" do
|
|
109
109
|
MultiattributeTestClass.create!
|
110
110
|
MultiattributeTestClass.create!
|
111
111
|
end
|
112
|
+
|
113
|
+
context "when there's more than one conflict" do
|
114
|
+
it "retries all conflicting fields" do
|
115
|
+
# We stub the username/password generation such that the first time both
|
116
|
+
# username and password are generated, there are conflicts. We use
|
117
|
+
# expectations to ensure that both fields are re-generated as needed.
|
118
|
+
expect(MultiattributeTestClass).to receive(:generate_username).
|
119
|
+
exactly(3).times.
|
120
|
+
and_return("1", "1", "2")
|
121
|
+
expect(MultiattributeTestClass).to receive(:generate_password).
|
122
|
+
exactly(3).times.
|
123
|
+
and_return("A", "A", "B")
|
124
|
+
|
125
|
+
MultiattributeTestClass.create!
|
126
|
+
MultiattributeTestClass.create!
|
127
|
+
end
|
128
|
+
end
|
112
129
|
end
|
113
130
|
end
|
data/unique_attributes.gemspec
CHANGED
@@ -19,8 +19,8 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
20
|
spec.require_paths = ["lib"]
|
21
21
|
|
22
|
-
spec.add_dependency "activerecord", "
|
23
|
-
spec.add_dependency "activesupport", "
|
22
|
+
spec.add_dependency "activerecord", ">= 4.0"
|
23
|
+
spec.add_dependency "activesupport", ">= 4.0"
|
24
24
|
|
25
25
|
spec.add_development_dependency "bundler", "~> 1.7"
|
26
26
|
spec.add_development_dependency "codeclimate-test-reporter", "~> 0.4"
|
metadata
CHANGED
@@ -1,41 +1,41 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: unique_attributes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jacob Evelyn
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-01-02 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: '4.0'
|
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: '4.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: activesupport
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - '>='
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '4.0'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - '>='
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '4.0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
@@ -227,7 +227,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
227
227
|
version: '0'
|
228
228
|
requirements: []
|
229
229
|
rubyforge_project:
|
230
|
-
rubygems_version: 2.
|
230
|
+
rubygems_version: 2.7.4
|
231
231
|
signing_key:
|
232
232
|
specification_version: 4
|
233
233
|
summary: Auto-assign unique attributes for ActiveRecord objects.
|