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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 70a00c890458b0211c83d2c03761dc25076d3eca
4
- data.tar.gz: fe8105fe343e4eb9b88fe05b49e9d64cdf77526a
2
+ SHA256:
3
+ metadata.gz: 05ae0b01e29ee3376746119561a42e507acf30c2e0e49ad00bb23202fad8dcb1
4
+ data.tar.gz: 4e898afbfe1a0668bfebc76b3dac718d9b9879663dc89dde6a4c2fc127dac4b9
5
5
  SHA512:
6
- metadata.gz: f7ac7936da0e54159abd1ef4e92240b2cdbf449cbe960eb45b830e037e1798c64a14f287020958f78721991e68321bbe02198abb4fa8f054e3d1dae2ae91238a
7
- data.tar.gz: 576ebc540a990b71865ecbbd9bc919802308efe216ae7f910c171b0f4c5f1b19fbebaf32042e10b87c1e028dfd214ae8f31311bc9d351506bbe66485eba302af
6
+ metadata.gz: 63b6d47cfba4c4d82cf704f31b38e8e4209342af24fd921a92ce09181845a3879a87b3dfb515150ec12519af56c50f615732c50c4a0638b9da70392af50a3eda
7
+ data.tar.gz: 0c26f90f0323aac83ad3ea09410039c5001bd131ba2b31f3ee258a7270f017a895c74154476496382fed8447fc2724b23d8824ce96d87ce287ba586ee2bb5bff
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- [![Code Climate](https://codeclimate.com/github/panorama-ed/unique_attributes/badges/gpa.svg)](https://codeclimate.com/github/panorama-ed/unique_attributes) [![Test Coverage](https://codeclimate.com/github/panorama-ed/unique_attributes/badges/coverage.svg)](https://codeclimate.com/github/panorama-ed/unique_attributes) [![Inline docs](http://inch-ci.org/github/panorama-ed/unique_attributes.png)](http://inch-ci.org/github/panorama-ed/unique_attributes) [![Build Status](https://travis-ci.org/panorama-ed/unique_attributes.svg)](https://travis-ci.org/panorama-ed/unique_attributes)
1
+ [![Code Climate](https://codeclimate.com/github/panorama-ed/unique_attributes/badges/gpa.svg)](https://codeclimate.com/github/panorama-ed/unique_attributes) [![Test Coverage](https://codeclimate.com/github/panorama-ed/unique_attributes/badges/coverage.svg)](https://codeclimate.com/github/panorama-ed/unique_attributes) [![Inline docs](http://inch-ci.org/github/panorama-ed/unique_attributes.png)](http://inch-ci.org/github/panorama-ed/unique_attributes) [![Build Status](https://travis-ci.org/panorama-ed/unique_attributes.svg)](https://travis-ci.org/panorama-ed/unique_attributes) [![Gem Version](https://badge.fury.io/rb/unique_attributes.svg)](http://badge.fury.io/rb/unique_attributes)
2
2
 
3
3
  # UniqueAttributes
4
4
 
@@ -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
- attr_group = "(?<attr>#{blank_attrs.keys.join('|')})"
81
- other_fields = "(, [\\w`'\"]+)*"
82
- sqlite3_regex = /column(s)? #{attr_group}#{other_fields} (is|are) not unique/
83
- postgresql_regex = /Key \(#{attr_group}#{other_fields}\)=\([\w\s,]*\) already exists/
84
-
85
- match = sqlite3_regex.match(error.message) ||
86
- postgresql_regex.match(error.message)
87
-
88
- # If we've managed to hit the same unique attribute of a record
89
- # already in the database, then we should wipe the attribute and try
90
- # again, unless we've already done this many times in which case we
91
- # should let the error bubble up.
92
- if match && attempts <= SAVE_ATTEMPTS_LIMIT
93
- attr = match[:attr].to_sym
94
- blank_attrs = { attr => blank_attrs[attr] }
95
- write_attribute(attr, nil)
96
- else
97
- raise error # If something else went wrong, let it propagate.
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,3 +1,3 @@
1
1
  module UniqueAttributes
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -1,4 +1,11 @@
1
- class TestSetupMigration < ActiveRecord::Migration
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
 
@@ -1,5 +1,6 @@
1
1
  require "spec_helper"
2
2
  require "shared/unique_attributes_examples"
3
+ require "config/test_setup_migration"
3
4
 
4
5
  RSpec.describe "PostgreSQL" do
5
6
  before :all do
@@ -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
@@ -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", "~> 4.0"
23
- spec.add_dependency "activesupport", "~> 4.0"
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.0
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: 2015-01-20 00:00:00.000000000 Z
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.2.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.