couchrest-uniqueness-validation 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.0
@@ -9,6 +9,7 @@ module CouchRest
9
9
  super
10
10
  @field_name, @options = field_name, options
11
11
  @options[:view] ||= "by_#{@field_name}"
12
+ @options[:downcase] ||= false
12
13
  end
13
14
 
14
15
  def call(target)
@@ -23,6 +24,7 @@ module CouchRest
23
24
 
24
25
  def unique?(target)
25
26
  value = target.validation_property_value(@field_name)
27
+ value.downcase! if @options[:downcase] && !value.blank?
26
28
  existing_docs = target.class.view(@options[:view].to_sym, :key => value, :limit => 1, :include_docs => false)['rows']
27
29
 
28
30
  # normal case when target.new_document? == true and
@@ -59,7 +61,11 @@ module CouchRest
59
61
  # validates_uniqueness_of :nickname
60
62
  #
61
63
  # # uses a different from default view
62
- # validates_uniqueness_of :login, :view => 'my_custom_view'
64
+ # validates_uniqueness_of :login, :view => 'by_my_custom_view'
65
+ #
66
+ # # or, you can do a case insensitive uniqueness validation
67
+ # validates_uniqueness_of :login, :view => 'by_case_insensitive_login_view', :downcase => true
68
+ # # view_by :case_insensitive_login_view, :map => "function(doc) {if (doc.login) {emit(doc.title.toLowerCase(), 1);}}"
63
69
  #
64
70
  # # a call to valid? will return false unless no other document exists with
65
71
  # # the same couchrest-type, nickname and login
@@ -68,7 +68,35 @@ describe CouchRest::Validation::UniquenessValidator do
68
68
  it "should work" do
69
69
  SomeOtherUniqueDoc.should_receive(:view).
70
70
  with(:my_custom_view, hash_including(:key => @some_doc.another_prop, :limit => 1, :include_docs => false))
71
- @some_doc.valid?
71
+ @some_doc.should be_valid
72
+ end
73
+ end
74
+
75
+ describe "using downcase" do
76
+ class YetAnotherUniqueDoc < CouchRest::ExtendedDocument
77
+ include CouchRest::Validation
78
+ use_database SPEC_COUCH
79
+
80
+ property :another_prop
81
+ validates_uniqueness_of :another_prop, :downcase => true
82
+ end
83
+
84
+ before(:each) do
85
+ @some_doc = YetAnotherUniqueDoc.new :another_prop => 'Some Property Value'
86
+ YetAnotherUniqueDoc.stub(:view).and_return({'rows' => []})
87
+ end
88
+
89
+ it "should work" do
90
+ YetAnotherUniqueDoc.should_receive(:view).
91
+ with(:by_another_prop, hash_including(:key => @some_doc.another_prop.downcase, :limit => 1, :include_docs => false))
92
+ @some_doc.should be_valid
93
+ end
94
+
95
+ it "works for nil values" do
96
+ @some_doc.another_prop = nil
97
+ YetAnotherUniqueDoc.should_receive(:view).
98
+ with(:by_another_prop, hash_including(:key => nil, :limit => 1, :include_docs => false))
99
+ lambda{@some_doc.valid?}.should_not raise_error
72
100
  end
73
101
  end
74
102
  end
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: couchrest-uniqueness-validation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
5
10
  platform: ruby
6
11
  authors:
7
12
  - alex
@@ -9,29 +14,36 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2010-01-17 00:00:00 +01:00
17
+ date: 2010-06-24 00:00:00 +02:00
13
18
  default_executable:
14
19
  dependencies:
15
20
  - !ruby/object:Gem::Dependency
16
21
  name: couchrest
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
20
24
  requirements:
21
25
  - - ">="
22
26
  - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ - 34
23
30
  version: "0.34"
24
- version:
31
+ type: :runtime
32
+ version_requirements: *id001
25
33
  - !ruby/object:Gem::Dependency
26
34
  name: rspec
27
- type: :development
28
- version_requirement:
29
- version_requirements: !ruby/object:Gem::Requirement
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
30
37
  requirements:
31
38
  - - ">="
32
39
  - !ruby/object:Gem::Version
40
+ segments:
41
+ - 1
42
+ - 2
43
+ - 9
33
44
  version: 1.2.9
34
- version:
45
+ type: :development
46
+ version_requirements: *id002
35
47
  description: adds uniquness validation by using a design doc view
36
48
  email: alex@digns.com
37
49
  executables: []
@@ -65,18 +77,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
65
77
  requirements:
66
78
  - - ">="
67
79
  - !ruby/object:Gem::Version
80
+ segments:
81
+ - 0
68
82
  version: "0"
69
- version:
70
83
  required_rubygems_version: !ruby/object:Gem::Requirement
71
84
  requirements:
72
85
  - - ">="
73
86
  - !ruby/object:Gem::Version
87
+ segments:
88
+ - 0
74
89
  version: "0"
75
- version:
76
90
  requirements: []
77
91
 
78
92
  rubyforge_project:
79
- rubygems_version: 1.3.5
93
+ rubygems_version: 1.3.6
80
94
  signing_key:
81
95
  specification_version: 3
82
96
  summary: adds validates_uniqueness_of validator to CouchRest::Validation