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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 01d0732a53d7733346abb9126487f5e2cf13b478
4
- data.tar.gz: c672292cedde949a2a10e449f087f1b5bc282c4e
3
+ metadata.gz: 699497aa0d7bc19b03e80501e80cab9be63d5201
4
+ data.tar.gz: 039be4d1d69f1703b2d9cb00f9ee56da767aa192
5
5
  SHA512:
6
- metadata.gz: c05fd1101e362a516e674764c37efe36e3fb4b10ffcedf16b08b91e9f3c464eecd199caac12eaecdcde56afd18f925cd047511d186671319acfd35867194f642
7
- data.tar.gz: 918bba5b2e531a1aa1ab74dba6e1ef5dd258a003b2d5280f8f51187f4cf81ca0742d7108f5362ff44a46ae460b8f35dbeae5fd2ffc6da50852f77c4c485154b9
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.
@@ -1,3 +1,3 @@
1
1
  module ValidatesDuplicityOf
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -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
- realtion = scope ? self.class.where(Hash[*[scope, self[scope]]]) : self.class
10
- names = realtion.where(self.class.arel_table[attr_name].matches("#{self[attr_name]} (%)")).pluck(attr_name)
11
- name_ids = names.map{ |name| name.match(/\((\d+)\)$/)[1].to_i }
12
- index = name_ids.sort.last.to_i + 1
13
- self[attr_name].concat " (#{index})"
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
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validates_duplicity_of
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vincent Durand
metadata.gz.sig CHANGED
Binary file