validates_duplicity_of 0.1.1 → 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
2
  SHA1:
3
- metadata.gz: 699497aa0d7bc19b03e80501e80cab9be63d5201
4
- data.tar.gz: 039be4d1d69f1703b2d9cb00f9ee56da767aa192
3
+ metadata.gz: f9ca17376355188c72f408f519d8dfb9eb1050bd
4
+ data.tar.gz: 3a898d1fc183f1d076c6513b1884ee37f51d0aba
5
5
  SHA512:
6
- metadata.gz: 0054a842cde9121f92d30b834b932f32633acae28fa187f895898451949ff62d7d4d7ec3ae7cfc3d6d6f5143aba2bd8172304c3666160d487986b82357b41f1f
7
- data.tar.gz: 1d137a0ebb92072bfe9b5014cbe62acd1606db5304f2a56c954d7a85a31e3ff8fd5335550dd60fb2857844e89a3875bf237ffb77574fa4e1a98534f938904e19
6
+ metadata.gz: 38f7571c6b2e4780c996952ed894c9c6921a84ac2a95c600c54f1f8b700c072338cc40b8fbb1016af5f6a776cfcb0ca3badcee15196b2ecb188b11389856611e
7
+ data.tar.gz: 6633e52514694cd093e970a0e50bd3b8113e1745fef0f4fe3e49076260b82d009ff08c0230331795cac3a2815ca5052ae898d879921fc0c2e3b15721ccce6413
Binary file
data.tar.gz.sig CHANGED
Binary file
data/README.md CHANGED
@@ -29,9 +29,9 @@ Or with security:
29
29
 
30
30
  ## Usage
31
31
 
32
- Use method `validates_duplicity_of` with attribute in your Rails model. Scope is optional.
32
+ Use method `validates_duplicity_of` with attribute in your Rails model. Scope is optional. Default callback is `before_save`.
33
33
 
34
- class Post
34
+ class Post < ActiveRecord::Base
35
35
  validates_duplicity_of :name, scope: :user_id
36
36
  end
37
37
 
@@ -42,6 +42,11 @@ Use method `validates_duplicity_of` with attribute in your Rails model. Scope is
42
42
  p = Post.create name: "Foo", user_id: 1
43
43
  p.name
44
44
  => "Foo (1)"
45
+
46
+ class Note < ActiveRecord::Base
47
+ validates_uniqueness_of :name
48
+ validates_duplicity_of :name, callback: :before_validation
49
+ end
45
50
 
46
51
 
47
52
  ## Contributing
@@ -2,9 +2,10 @@ require 'validates_duplicity_of/version'
2
2
  require 'active_record'
3
3
 
4
4
  module ValidatesDuplicityOf
5
- def validates_duplicity_of(attr_name, scope: nil)
6
- before_save do
5
+ def validates_duplicity_of(attr_name, scope: nil, callback: :before_save)
6
+ validates = -> do
7
7
  return unless changed.include? attr_name.to_s
8
+ return unless self[attr_name].present?
8
9
  if self.class.exists?(Hash[*[attr_name, self[attr_name], scope, self[scope]].compact])
9
10
  if /#{Regexp.escape(self[attr_name])} \(\d+\)$/.match changed_attributes[attr_name.to_s]
10
11
  self[attr_name] = changed_attributes[attr_name.to_s]
@@ -17,6 +18,7 @@ module ValidatesDuplicityOf
17
18
  end
18
19
  end
19
20
  end
21
+ send callback, &validates
20
22
  end
21
23
  end
22
24
 
@@ -1,3 +1,3 @@
1
1
  module ValidatesDuplicityOf
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -40,6 +40,12 @@ describe "validates_duplicity_of" do
40
40
  post.update_attributes name: "New name"
41
41
  expect(post.name).to eq "New name (2)"
42
42
  end
43
+
44
+ it "should not update if name is not present" do
45
+ Post.create name: ""
46
+ post = Post.create name: ""
47
+ expect(post.name).to eq ""
48
+ end
43
49
  end
44
50
 
45
51
  context "with scope" do
@@ -67,4 +73,14 @@ describe "validates_duplicity_of" do
67
73
  expect(note.title).to eq "New title (11)"
68
74
  end
69
75
  end
76
+
77
+ context "with before_validation callback" do
78
+ before(:each) { Comment.create name: "New name" }
79
+ after(:each) { Comment.delete_all }
80
+
81
+ it "should update name before validation" do
82
+ comment = Comment.create name: "New name"
83
+ expect(comment.name).to eq "New name (1)"
84
+ end
85
+ end
70
86
  end
@@ -4,4 +4,9 @@ end
4
4
 
5
5
  class Note < ActiveRecord::Base
6
6
  validates_duplicity_of :title, scope: :user_id
7
+ end
8
+
9
+ class Comment < ActiveRecord::Base
10
+ validates_uniqueness_of :name
11
+ validates_duplicity_of :name, callback: :before_validation
7
12
  end
@@ -9,4 +9,8 @@ ActiveRecord::Schema.define do
9
9
  t.string :title
10
10
  t.integer :user_id
11
11
  end
12
+
13
+ create_table :comments, force: true do |t|
14
+ t.string :name
15
+ end
12
16
  end
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.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vincent Durand
@@ -31,7 +31,7 @@ cert_chain:
31
31
  xDr7hfCTJQsNosd7V5q7Uz+SYPK9ALyONt5t3KKYEWS/mBCStEuQXwjIsWG/bqwr
32
32
  vqfQstIS
33
33
  -----END CERTIFICATE-----
34
- date: 2013-10-17 00:00:00.000000000 Z
34
+ date: 2013-10-18 00:00:00.000000000 Z
35
35
  dependencies:
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: bundler
metadata.gz.sig CHANGED
Binary file