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 +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/README.md +7 -2
- data/lib/validates_duplicity_of.rb +4 -2
- data/lib/validates_duplicity_of/version.rb +1 -1
- data/spec/lib/validates_duplicity_of_spec.rb +16 -0
- data/spec/support/models.rb +5 -0
- data/spec/support/schema.rb +4 -0
- metadata +2 -2
- 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: f9ca17376355188c72f408f519d8dfb9eb1050bd
|
4
|
+
data.tar.gz: 3a898d1fc183f1d076c6513b1884ee37f51d0aba
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 38f7571c6b2e4780c996952ed894c9c6921a84ac2a95c600c54f1f8b700c072338cc40b8fbb1016af5f6a776cfcb0ca3badcee15196b2ecb188b11389856611e
|
7
|
+
data.tar.gz: 6633e52514694cd093e970a0e50bd3b8113e1745fef0f4fe3e49076260b82d009ff08c0230331795cac3a2815ca5052ae898d879921fc0c2e3b15721ccce6413
|
checksums.yaml.gz.sig
CHANGED
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
|
-
|
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
|
|
@@ -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
|
data/spec/support/models.rb
CHANGED
data/spec/support/schema.rb
CHANGED
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.
|
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-
|
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
|