validates_duplicity_of 0.1.0 → 0.1.1
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 +4 -4
- checksums.yaml.gz.sig +0 -0
- data/README.md +6 -0
- data/lib/validates_duplicity_of/version.rb +1 -1
- data/lib/validates_duplicity_of.rb +9 -5
- data/spec/lib/validates_duplicity_of_spec.rb +7 -0
- data.tar.gz.sig +0 -0
- metadata +1 -1
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 699497aa0d7bc19b03e80501e80cab9be63d5201
|
4
|
+
data.tar.gz: 039be4d1d69f1703b2d9cb00f9ee56da767aa192
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0054a842cde9121f92d30b834b932f32633acae28fa187f895898451949ff62d7d4d7ec3ae7cfc3d6d6f5143aba2bd8172304c3666160d487986b82357b41f1f
|
7
|
+
data.tar.gz: 1d137a0ebb92072bfe9b5014cbe62acd1606db5304f2a56c954d7a85a31e3ff8fd5335550dd60fb2857844e89a3875bf237ffb77574fa4e1a98534f938904e19
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data/README.md
CHANGED
@@ -21,6 +21,12 @@ Or install it yourself as:
|
|
21
21
|
|
22
22
|
$ gem install validates_duplicity_of
|
23
23
|
|
24
|
+
Or with security:
|
25
|
+
|
26
|
+
$ wget https://gist.github.com/madwork/7026195/raw/6e06ae0754eecdf78cbb091e816597e860022dc1/gem-public_cert.pem -O madwork-public_cert.pem
|
27
|
+
$ gem cert --add madwork-public_cert.pem
|
28
|
+
$ gem install validates_duplicity_of --trust-policy=HighSecurity
|
29
|
+
|
24
30
|
## Usage
|
25
31
|
|
26
32
|
Use method `validates_duplicity_of` with attribute in your Rails model. Scope is optional.
|
@@ -6,11 +6,15 @@ module ValidatesDuplicityOf
|
|
6
6
|
before_save do
|
7
7
|
return unless changed.include? attr_name.to_s
|
8
8
|
if self.class.exists?(Hash[*[attr_name, self[attr_name], scope, self[scope]].compact])
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
9
|
+
if /#{Regexp.escape(self[attr_name])} \(\d+\)$/.match changed_attributes[attr_name.to_s]
|
10
|
+
self[attr_name] = changed_attributes[attr_name.to_s]
|
11
|
+
else
|
12
|
+
relation = scope ? self.class.where(Hash[*[scope, self[scope]]]) : self.class
|
13
|
+
names = relation.where(self.class.arel_table[attr_name].matches("#{self[attr_name]} (%)")).pluck(attr_name)
|
14
|
+
name_ids = names.flat_map{ |name| name.match(/\((\d+)\)$/).captures }.map(&:to_i)
|
15
|
+
index = name_ids.sort.last.to_i + 1
|
16
|
+
self[attr_name].concat " (#{index})"
|
17
|
+
end
|
14
18
|
end
|
15
19
|
end
|
16
20
|
end
|
@@ -33,6 +33,13 @@ describe "validates_duplicity_of" do
|
|
33
33
|
expect(post.name).to eq "New name (1) (#{index + 1})"
|
34
34
|
end
|
35
35
|
end
|
36
|
+
|
37
|
+
it "should keep the same index with the same name" do
|
38
|
+
Post.create name: "New name (1)"
|
39
|
+
post = Post.create name: "New name (2)"
|
40
|
+
post.update_attributes name: "New name"
|
41
|
+
expect(post.name).to eq "New name (2)"
|
42
|
+
end
|
36
43
|
end
|
37
44
|
|
38
45
|
context "with scope" do
|
data.tar.gz.sig
CHANGED
Binary file
|
metadata
CHANGED
metadata.gz.sig
CHANGED
Binary file
|