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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8ff9786fae36205ed1621cd7801d1a9af216c7aff03f92b528e9ecc89fb1d2bc
4
- data.tar.gz: 63771b5f779b93e902e30edca63c29a407c3856c3b0fd30973babfb63ba40a19
3
+ metadata.gz: e56c6790f8382532f9e3a9ba01dfe2ef29bb2b9c6bf7f4b1059bdf4ba4e432df
4
+ data.tar.gz: 48d102170b5893bb8f5d48691cd0fdb2a85a3f220e3db563c9d470d1b6d09867
5
5
  SHA512:
6
- metadata.gz: b3d79e03b30b90b55f33dd78e8ff16a8a3ac8fb8729482e2607dd09ebdbec22b4ec52647859d2b01ad397ec4e43657b5378832a3aedb0490586f21cdb47de70e
7
- data.tar.gz: 1965d04f498fdc1d34ece26e6c22258c77f269af7554171fee716f923bb2146b6f6146e56086b0ccda1366b00503d0ad84f11eff0d1309e68f3d06aaf4ff7399
6
+ metadata.gz: f7f945cd1a46a33446bf41eb2479732edabf96a85324f3a4285d356aee6d62edc3ce699656b1690126dbdefeeb5bcc5b6ca5252f3fdcff648df05d21682c5862
7
+ data.tar.gz: 885ffb6b352c232c7bae350ef997cd374174783e199448e240644dd33b90087f212beba5b805083581cbb247f0cec21bf7605ffd91e4b7fbc880571aa22eb514
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- has_unique_attribute (0.1.1)
4
+ has_unique_attribute (0.1.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -59,4 +59,4 @@ DEPENDENCIES
59
59
  rubocop-rspec (~> 1.42)
60
60
 
61
61
  BUNDLED WITH
62
- 2.0.2
62
+ 2.2.3
data/README.md CHANGED
@@ -82,12 +82,17 @@ class Membership < Application
82
82
  belongs_to :user
83
83
  belongs_to :club
84
84
 
85
- has_unique_index :user, index: 'index_memberships_on_club_id_and_user_id', message: 'is already a member'
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.
@@ -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)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HasUniqueAttribute
4
- VERSION = '0.1.1'
4
+ VERSION = '0.1.2'
5
5
  end
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.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: 2020-07-15 00:00:00.000000000 Z
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.0.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.