couchrest-uniqueness-validation 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +22 -8
- data/Rakefile +10 -0
- data/VERSION +1 -1
- data/lib/couchrest-uniqueness-validation.rb +14 -8
- data/spec/couchrest-uniqueness-validation_spec.rb +63 -2
- data/spec/spec_helper.rb +1 -0
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -1,16 +1,23 @@
|
|
1
|
-
=
|
1
|
+
= Couchrest Uniqueness Validation
|
2
|
+
|
3
|
+
This is a simple add-on to {couchrest}[http://github.com/couchrest/couchrest] gem
|
2
4
|
|
3
5
|
Validates that the specified attribute is unique across documents with the same
|
4
6
|
couchrest-type using a view.
|
5
7
|
|
6
|
-
|
7
|
-
You have to define a design doc view.
|
8
|
-
@see http://rdoc.info/rdoc/couchrest/couchrest/blob/1b34fe4b60694683e98866a51c2109c1885f7e42/CouchRest/Mixins/Views/ClassMethods.html#view_by-instance_method for more details about views.
|
9
|
-
|
10
|
-
@example [Usage]
|
8
|
+
== Install
|
11
9
|
|
12
|
-
|
10
|
+
gem install couchrest-uniqueness-validation
|
13
11
|
|
12
|
+
== Usage
|
13
|
+
|
14
|
+
require 'rubygems'
|
15
|
+
require 'couchrest'
|
16
|
+
require 'couchrest-uniqueness-validation'
|
17
|
+
|
18
|
+
class User < CouchRest::ExtendedDocument
|
19
|
+
include CouchRest::Validation
|
20
|
+
|
14
21
|
property :nickname
|
15
22
|
property :login
|
16
23
|
|
@@ -26,9 +33,16 @@ couchrest-type using a view.
|
|
26
33
|
# the same couchrest-type, nickname and login
|
27
34
|
end
|
28
35
|
|
36
|
+
Note: at least two views should exist in the User design doc for this example to work -
|
37
|
+
:by_nickname and :my_custom_view.
|
38
|
+
|
39
|
+
See {CouchRest Views docs}[http://rdoc.info/rdoc/couchrest/couchrest/blob/1b34fe4b60694683e98866a51c2109c1885f7e42/CouchRest/Mixins/Views/ClassMethods.html#view_by-instance_method]
|
40
|
+
for more info on views.
|
41
|
+
|
42
|
+
|
29
43
|
== Note on Patches/Pull Requests
|
30
44
|
|
31
|
-
* Fork the project.
|
45
|
+
* Fork the {project}[http://github.com/crhym3/couchrest-uniqueness-validation].
|
32
46
|
* Make your feature addition or bug fix.
|
33
47
|
* Add tests for it. This is important so I don't break it in a
|
34
48
|
future version unintentionally.
|
data/Rakefile
CHANGED
@@ -23,12 +23,22 @@ require 'spec/rake/spectask'
|
|
23
23
|
Spec::Rake::SpecTask.new(:spec) do |spec|
|
24
24
|
spec.libs << 'lib' << 'spec'
|
25
25
|
spec.spec_files = FileList['spec/**/*_spec.rb']
|
26
|
+
spec.spec_opts = ['--options', "\"spec/spec.opts\""]
|
27
|
+
end
|
28
|
+
|
29
|
+
namespace :spec do
|
30
|
+
desc "Print Specdoc for all specs"
|
31
|
+
Spec::Rake::SpecTask.new(:doc) do |t|
|
32
|
+
t.spec_opts = ["--format", "specdoc", "--dry-run"]
|
33
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
34
|
+
end
|
26
35
|
end
|
27
36
|
|
28
37
|
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
29
38
|
spec.libs << 'lib' << 'spec'
|
30
39
|
spec.pattern = 'spec/**/*_spec.rb'
|
31
40
|
spec.rcov = true
|
41
|
+
spec.rcov_opts = ['--exclude "spec/*,gems/*"']
|
32
42
|
end
|
33
43
|
|
34
44
|
task :spec => :check_dependencies
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0
|
1
|
+
0.1.0
|
@@ -6,7 +6,7 @@ module CouchRest
|
|
6
6
|
def initialize(field_name, options = {})
|
7
7
|
super
|
8
8
|
@field_name, @options = field_name, options
|
9
|
-
@options[:view] ||= "by_#{@field_name}"
|
9
|
+
@options[:view] ||= "by_#{@field_name}"
|
10
10
|
end
|
11
11
|
|
12
12
|
def call(target)
|
@@ -21,7 +21,7 @@ module CouchRest
|
|
21
21
|
|
22
22
|
def unique?(target)
|
23
23
|
value = target.validation_property_value(@field_name)
|
24
|
-
existing_docs = target.class.view(@options[:view], :key => value, :limit => 1, :include_docs => false)
|
24
|
+
existing_docs = target.class.view(@options[:view].to_sym, :key => value, :limit => 1, :include_docs => false)['rows']
|
25
25
|
|
26
26
|
# normal case when target.new_document? == true and
|
27
27
|
# no other document exists
|
@@ -40,13 +40,13 @@ module CouchRest
|
|
40
40
|
# Validates that the specified attribute is unique across documents with the same
|
41
41
|
# couchrest-type using a view.
|
42
42
|
#
|
43
|
-
#
|
44
|
-
# You have to define a design doc view.
|
45
|
-
# @see http://rdoc.info/rdoc/couchrest/couchrest/blob/1b34fe4b60694683e98866a51c2109c1885f7e42/CouchRest/Mixins/Views/ClassMethods.html#view_by-instance_method for more details about views.
|
43
|
+
# == Example
|
46
44
|
#
|
47
|
-
#
|
48
|
-
#
|
49
|
-
#
|
45
|
+
# require 'rubygems'
|
46
|
+
# require 'couchrest'
|
47
|
+
# require 'couchrest-uniqueness-validation'
|
48
|
+
#
|
49
|
+
# class User < CouchRest::ExtendedDocument
|
50
50
|
#
|
51
51
|
# property :nickname
|
52
52
|
# property :login
|
@@ -62,6 +62,12 @@ module CouchRest
|
|
62
62
|
# # a call to valid? will return false unless no other document exists with
|
63
63
|
# # the same couchrest-type, nickname and login
|
64
64
|
# end
|
65
|
+
#
|
66
|
+
# Note: at least two views should exist in the User design doc for this example to work -
|
67
|
+
# :by_nickname and :my_custom_view.
|
68
|
+
#
|
69
|
+
# See {CouchRest Views docs}[http://rdoc.info/rdoc/couchrest/couchrest/blob/1b34fe4b60694683e98866a51c2109c1885f7e42/CouchRest/Mixins/Views/ClassMethods.html#view_by-instance_method]
|
70
|
+
# for more info on views
|
65
71
|
def validates_uniqueness_of(*fields)
|
66
72
|
opts = opts_from_validator_args(fields)
|
67
73
|
add_validator_to_context(opts, fields, CouchRest::Validation::UniquenessValidator)
|
@@ -2,12 +2,73 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
2
2
|
|
3
3
|
describe CouchRest::Validation::ValidatesUniqueness do
|
4
4
|
|
5
|
-
class
|
5
|
+
class SomeDoc < CouchRest::ExtendedDocument
|
6
6
|
include CouchRest::Validation
|
7
7
|
use_database SPEC_COUCH
|
8
8
|
end
|
9
9
|
|
10
10
|
it "should provide validates_uniqueness_of method" do
|
11
|
-
|
11
|
+
SomeDoc.should respond_to :validates_uniqueness_of
|
12
12
|
end
|
13
13
|
end
|
14
|
+
|
15
|
+
describe CouchRest::Validation::UniquenessValidator do
|
16
|
+
|
17
|
+
class SomeUniqueDoc < CouchRest::ExtendedDocument
|
18
|
+
include CouchRest::Validation
|
19
|
+
use_database SPEC_COUCH
|
20
|
+
|
21
|
+
property :unique_prop
|
22
|
+
validates_uniqueness_of :unique_prop
|
23
|
+
end
|
24
|
+
|
25
|
+
before(:each) do
|
26
|
+
@some_doc = SomeUniqueDoc.new :unique_prop => 'some property value'
|
27
|
+
SomeUniqueDoc.stub(:view).and_return({'rows' => []})
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should use a corresponding default view for uniqueness test" do
|
31
|
+
SomeUniqueDoc.should_receive(:view).
|
32
|
+
with(:by_unique_prop, hash_including(:key => @some_doc.unique_prop, :limit => 1, :include_docs => false))
|
33
|
+
@some_doc.valid?
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should pass validation if no other document exist with the same property value" do
|
37
|
+
@some_doc.should be_valid
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should not pass validation if another document exists already" do
|
41
|
+
SomeUniqueDoc.should_receive(:view).and_return({'rows' => [{'id' => 123}]})
|
42
|
+
@some_doc.should_not be_valid
|
43
|
+
@some_doc.errors.should have_key :unique_prop
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should pass validation for an existing document (should remove itself from the list)" do
|
47
|
+
SomeUniqueDoc.should_receive(:view).and_return({'rows' => [{'id' => 123}]})
|
48
|
+
@some_doc.stub(:new_document?).and_return(false)
|
49
|
+
@some_doc.stub(:id).and_return(123)
|
50
|
+
@some_doc.should be_valid
|
51
|
+
end
|
52
|
+
|
53
|
+
describe "using custom view" do
|
54
|
+
|
55
|
+
class SomeOtherUniqueDoc < CouchRest::ExtendedDocument
|
56
|
+
include CouchRest::Validation
|
57
|
+
use_database SPEC_COUCH
|
58
|
+
|
59
|
+
property :another_prop
|
60
|
+
validates_uniqueness_of :another_prop, :view => 'my_custom_view'
|
61
|
+
end
|
62
|
+
|
63
|
+
before(:each) do
|
64
|
+
@some_doc = SomeOtherUniqueDoc.new :another_prop => 'some property value'
|
65
|
+
SomeOtherUniqueDoc.stub(:view).and_return({'rows' => []})
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should work" do
|
69
|
+
SomeOtherUniqueDoc.should_receive(:view).
|
70
|
+
with(:my_custom_view, hash_including(:key => @some_doc.another_prop, :limit => 1, :include_docs => false))
|
71
|
+
@some_doc.valid?
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
data/spec/spec_helper.rb
CHANGED