has_unique_attribute 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +2 -2
- data/README.md +6 -1
- data/lib/has_unique_attribute.rb +2 -2
- data/lib/has_unique_attribute/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e56c6790f8382532f9e3a9ba01dfe2ef29bb2b9c6bf7f4b1059bdf4ba4e432df
|
4
|
+
data.tar.gz: 48d102170b5893bb8f5d48691cd0fdb2a85a3f220e3db563c9d470d1b6d09867
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f7f945cd1a46a33446bf41eb2479732edabf96a85324f3a4285d356aee6d62edc3ce699656b1690126dbdefeeb5bcc5b6ca5252f3fdcff648df05d21682c5862
|
7
|
+
data.tar.gz: 885ffb6b352c232c7bae350ef997cd374174783e199448e240644dd33b90087f212beba5b805083581cbb247f0cec21bf7605ffd91e4b7fbc880571aa22eb514
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -82,12 +82,17 @@ class Membership < Application
|
|
82
82
|
belongs_to :user
|
83
83
|
belongs_to :club
|
84
84
|
|
85
|
-
|
85
|
+
has_unique_attribute :user, index: 'index_memberships_on_club_id_and_user_id', message: 'is already a member'
|
86
86
|
end
|
87
87
|
```
|
88
88
|
|
89
89
|
The error is only applied to the specified attribute.
|
90
90
|
|
91
|
+
### Caveats
|
92
|
+
|
93
|
+
* Errors triggering the `has_unique_attribute` behaviour happen further into the save process than standard validations (after validation and `before_save` callbacks are fired). Behaviours that should not be executed if a uniqueness constraint fails should be placed `after_save` or `after_commit`, or be transactional (and rolled-back as part of the transaction).
|
94
|
+
* The `create_or_find_by` methods require that an `ActiveRecord::RecordNotUnique` error be raised, which `has_unique_attribute` consumes and does not rethrow.
|
95
|
+
|
91
96
|
## Development
|
92
97
|
|
93
98
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/has_unique_attribute.rb
CHANGED
@@ -38,8 +38,8 @@ private
|
|
38
38
|
def handle_unique_attribute_on_save(attribute_name, index_name, message)
|
39
39
|
existing_method = instance_method(:save)
|
40
40
|
|
41
|
-
define_method(:save) do |*args|
|
42
|
-
existing_method.bind(self).call(*args)
|
41
|
+
define_method(:save) do |*args, **kwargs|
|
42
|
+
existing_method.bind(self).call(*args, **kwargs)
|
43
43
|
rescue ActiveRecord::RecordNotUnique => error
|
44
44
|
if error.message.include?(index_name)
|
45
45
|
errors.add(attribute_name, message)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: has_unique_attribute
|
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
|
- Mihail-K
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-01-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -137,7 +137,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
137
137
|
- !ruby/object:Gem::Version
|
138
138
|
version: '0'
|
139
139
|
requirements: []
|
140
|
-
rubygems_version: 3.
|
140
|
+
rubygems_version: 3.2.3
|
141
141
|
signing_key:
|
142
142
|
specification_version: 4
|
143
143
|
summary: Handle unique attributes safely using the database.
|