durran-validatable 1.7.5 → 1.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION.yml +2 -2
- data/lib/validatable.rb +3 -0
- data/lib/validations/validates_exclusion_of.rb +16 -0
- data/lib/validations/validates_inclusion_of.rb +16 -0
- data/lib/validations/validates_uniqueness_of.rb +18 -0
- data/test/unit/test_validates_exclusion_of.rb +20 -0
- data/test/unit/test_validates_inclusion_of.rb +25 -0
- data/test/unit/test_validates_uniqueness_of.rb +18 -0
- metadata +11 -2
data/VERSION.yml
CHANGED
data/lib/validatable.rb
CHANGED
@@ -23,3 +23,6 @@ require File.join(dir, 'validations/validates_true_for')
|
|
23
23
|
require File.join(dir, 'validations/validates_numericality_of')
|
24
24
|
require File.join(dir, 'validations/validates_each')
|
25
25
|
require File.join(dir, 'validations/validates_associated')
|
26
|
+
require File.join(dir, 'validations/validates_uniqueness_of')
|
27
|
+
require File.join(dir, 'validations/validates_inclusion_of')
|
28
|
+
require File.join(dir, 'validations/validates_exclusion_of')
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Validatable
|
2
|
+
class ValidatesExclusionOf < Validatable::ValidationBase
|
3
|
+
required_option :within
|
4
|
+
|
5
|
+
def valid?(instance)
|
6
|
+
value = instance.send(attribute)
|
7
|
+
return true if allow_nil && value.nil?
|
8
|
+
return true if allow_blank && value.blank?
|
9
|
+
!within.include?(value)
|
10
|
+
end
|
11
|
+
|
12
|
+
def message(instance)
|
13
|
+
super || "is excluded"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Validatable
|
2
|
+
class ValidatesInclusionOf < Validatable::ValidationBase
|
3
|
+
required_option :within
|
4
|
+
|
5
|
+
def valid?(instance)
|
6
|
+
value = instance.send(attribute)
|
7
|
+
return true if allow_nil && value.nil?
|
8
|
+
return true if allow_blank && value.blank?
|
9
|
+
within.include?(value)
|
10
|
+
end
|
11
|
+
|
12
|
+
def message(instance)
|
13
|
+
super || "is not included"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Validatable
|
2
|
+
class ValidatesUniquenessOf < Validatable::ValidationBase
|
3
|
+
option :scope
|
4
|
+
|
5
|
+
def valid?(instance)
|
6
|
+
conditions = { self.attribute => instance.send(attribute) }
|
7
|
+
if scope
|
8
|
+
conditions.merge!(scope => instance.send(scope))
|
9
|
+
end
|
10
|
+
doc = instance.class.first(:conditions => conditions)
|
11
|
+
doc.nil? || instance.id == doc.id
|
12
|
+
end
|
13
|
+
|
14
|
+
def message(instance)
|
15
|
+
super || "must be unique"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
|
2
|
+
|
3
|
+
Expectations do
|
4
|
+
|
5
|
+
expect true do
|
6
|
+
validation = Validatable::ValidatesExclusionOf.new stub_everything, :name, :within => ["test"]
|
7
|
+
validation.valid?(stub(:name => "testing"))
|
8
|
+
end
|
9
|
+
|
10
|
+
expect true do
|
11
|
+
validation = Validatable::ValidatesExclusionOf.new stub_everything, :name, :allow_nil => true, :within => ["test"]
|
12
|
+
validation.valid?(stub(:name => nil))
|
13
|
+
end
|
14
|
+
|
15
|
+
expect true do
|
16
|
+
validation = Validatable::ValidatesExclusionOf.new stub_everything, :name, :allow_blank => true, :within => ["test"]
|
17
|
+
validation.valid?(stub(:name => ''))
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
|
2
|
+
|
3
|
+
Expectations do
|
4
|
+
|
5
|
+
expect false do
|
6
|
+
validation = Validatable::ValidatesInclusionOf.new stub_everything, :name, :within => ["test"]
|
7
|
+
validation.valid?(stub_everything)
|
8
|
+
end
|
9
|
+
|
10
|
+
expect true do
|
11
|
+
validation = Validatable::ValidatesInclusionOf.new stub_everything, :name, :within => ["test"]
|
12
|
+
validation.valid?(stub(:name => "test"))
|
13
|
+
end
|
14
|
+
|
15
|
+
expect true do
|
16
|
+
validation = Validatable::ValidatesInclusionOf.new stub_everything, :name, :allow_nil => true, :within => ["test"]
|
17
|
+
validation.valid?(stub(:name => nil))
|
18
|
+
end
|
19
|
+
|
20
|
+
expect true do
|
21
|
+
validation = Validatable::ValidatesInclusionOf.new stub_everything, :name, :allow_blank => true, :within => ["test"]
|
22
|
+
validation.valid?(stub(:name => ''))
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
|
2
|
+
|
3
|
+
Expectations do
|
4
|
+
|
5
|
+
expect false do
|
6
|
+
doc = stub(:id => 1)
|
7
|
+
instance = stub(:class => stub(:first => doc), :id => 2, :name => "test")
|
8
|
+
validation = Validatable::ValidatesUniquenessOf.new stub_everything, :name
|
9
|
+
validation.valid?(instance)
|
10
|
+
end
|
11
|
+
|
12
|
+
expect true do
|
13
|
+
instance = stub(:class => stub(:first => nil), :id => 2, :name => "test")
|
14
|
+
validation = Validatable::ValidatesUniquenessOf.new stub_everything, :name
|
15
|
+
validation.valid?(instance)
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: durran-validatable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.8.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jay Fields
|
@@ -11,7 +11,7 @@ autorequire:
|
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
13
|
|
14
|
-
date: 2009-10-
|
14
|
+
date: 2009-10-31 00:00:00 -04:00
|
15
15
|
default_executable:
|
16
16
|
dependencies: []
|
17
17
|
|
@@ -41,11 +41,14 @@ files:
|
|
41
41
|
- lib/validations/validates_associated.rb
|
42
42
|
- lib/validations/validates_confirmation_of.rb
|
43
43
|
- lib/validations/validates_each.rb
|
44
|
+
- lib/validations/validates_exclusion_of.rb
|
44
45
|
- lib/validations/validates_format_of.rb
|
46
|
+
- lib/validations/validates_inclusion_of.rb
|
45
47
|
- lib/validations/validates_length_of.rb
|
46
48
|
- lib/validations/validates_numericality_of.rb
|
47
49
|
- lib/validations/validates_presence_of.rb
|
48
50
|
- lib/validations/validates_true_for.rb
|
51
|
+
- lib/validations/validates_uniqueness_of.rb
|
49
52
|
- lib/validations/validation_base.rb
|
50
53
|
- test/all_tests.rb
|
51
54
|
- test/functional/test_validatable.rb
|
@@ -65,11 +68,14 @@ files:
|
|
65
68
|
- test/unit/test_validates_acceptance_of.rb
|
66
69
|
- test/unit/test_validates_associated.rb
|
67
70
|
- test/unit/test_validates_confirmation_of.rb
|
71
|
+
- test/unit/test_validates_exclusion_of.rb
|
68
72
|
- test/unit/test_validates_format_of.rb
|
73
|
+
- test/unit/test_validates_inclusion_of.rb
|
69
74
|
- test/unit/test_validates_length_of.rb
|
70
75
|
- test/unit/test_validates_numericality_of.rb
|
71
76
|
- test/unit/test_validates_presence_of.rb
|
72
77
|
- test/unit/test_validates_true_for.rb
|
78
|
+
- test/unit/test_validates_uniqueness_of.rb
|
73
79
|
- test/unit/test_validation_base.rb
|
74
80
|
has_rdoc: true
|
75
81
|
homepage: http://github.com/durran/validatable
|
@@ -118,9 +124,12 @@ test_files:
|
|
118
124
|
- test/unit/test_validates_acceptance_of.rb
|
119
125
|
- test/unit/test_validates_associated.rb
|
120
126
|
- test/unit/test_validates_confirmation_of.rb
|
127
|
+
- test/unit/test_validates_exclusion_of.rb
|
121
128
|
- test/unit/test_validates_format_of.rb
|
129
|
+
- test/unit/test_validates_inclusion_of.rb
|
122
130
|
- test/unit/test_validates_length_of.rb
|
123
131
|
- test/unit/test_validates_numericality_of.rb
|
124
132
|
- test/unit/test_validates_presence_of.rb
|
125
133
|
- test/unit/test_validates_true_for.rb
|
134
|
+
- test/unit/test_validates_uniqueness_of.rb
|
126
135
|
- test/unit/test_validation_base.rb
|