evelpidon_validators 0.1.0 → 0.2.0
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.
- data/CHANGELOG.md +5 -0
- data/README.md +10 -22
- data/evelpidon_validators.gemspec +1 -0
- data/lib/evelpidon_validators/greater.rb +1 -1
- data/lib/evelpidon_validators/less.rb +1 -1
- data/lib/evelpidon_validators/version.rb +1 -1
- data/test/greater_test.rb +43 -3
- data/test/less_test.rb +44 -4
- metadata +26 -16
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -17,41 +17,31 @@ already loaded.
|
|
17
17
|
|
18
18
|
Add on your Gemfile :
|
19
19
|
|
20
|
-
|
21
|
-
gem 'evelpidon_validators'
|
22
|
-
```
|
20
|
+
gem 'evelpidon_validators'
|
23
21
|
|
24
22
|
### By hand
|
25
23
|
|
26
24
|
On the console :
|
27
25
|
|
28
|
-
|
29
|
-
gem install evelpidon_validators
|
30
|
-
```
|
26
|
+
gem install evelpidon_validators
|
31
27
|
|
32
28
|
On your code :
|
33
29
|
|
34
|
-
|
35
|
-
require 'evelpidon_validators'
|
36
|
-
```
|
30
|
+
require 'evelpidon_validators'
|
37
31
|
|
38
32
|
## Usage
|
39
33
|
|
40
34
|
Validators are *not* automatically loaded. You can require the validators you need either explicitly one-by-one
|
41
35
|
or all of them. So for example :
|
42
36
|
|
43
|
-
|
44
|
-
|
45
|
-
require 'evelpidon_validators/greater'
|
46
|
-
require 'evelpidon_validators/greater'
|
47
|
-
```
|
37
|
+
# Load only "Greater than" and "Less than" validators :
|
38
|
+
require 'evelpidon_validators/greater'
|
39
|
+
require 'evelpidon_validators/greater'
|
48
40
|
|
49
41
|
or
|
50
42
|
|
51
|
-
|
52
|
-
|
53
|
-
require 'evelpidon_validators/all'
|
54
|
-
```
|
43
|
+
# Load everything
|
44
|
+
require 'evelpidon_validators/all'
|
55
45
|
|
56
46
|
### Enable client side validations support
|
57
47
|
|
@@ -62,10 +52,8 @@ Client-side validations work out of the box with Rails 3.1 (through the asset pi
|
|
62
52
|
|
63
53
|
So for example the following should work :
|
64
54
|
|
65
|
-
|
66
|
-
//= require
|
67
|
-
//= require evelpidon_validators
|
68
|
-
```
|
55
|
+
//= require rails.validations
|
56
|
+
//= require evelpidon_validators
|
69
57
|
|
70
58
|
## TODOs
|
71
59
|
|
@@ -18,6 +18,7 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
19
|
s.require_paths = ["lib"]
|
20
20
|
|
21
|
+
s.add_development_dependency "rake"
|
21
22
|
s.add_development_dependency "activesupport"
|
22
23
|
s.add_development_dependency "evelpidon_test_helpers"
|
23
24
|
|
@@ -6,7 +6,7 @@ module ActiveModel
|
|
6
6
|
end
|
7
7
|
|
8
8
|
def validate_each(record, attribute, value)
|
9
|
-
unless self.class.greater? value, record.send(options[:than])
|
9
|
+
unless self.class.greater? value, record.send(options[:than]) or (options[:or_equal] && value.eql?(record.send(options[:than])))
|
10
10
|
record.errors.add(attribute, :greater, options)
|
11
11
|
end
|
12
12
|
end
|
@@ -6,7 +6,7 @@ module ActiveModel
|
|
6
6
|
end
|
7
7
|
|
8
8
|
def validate_each(record, attribute, value)
|
9
|
-
unless self.class.less? value, record.send(options[:than])
|
9
|
+
unless self.class.less? value, record.send(options[:than]) or (options[:or_equal] && value.eql?(record.send(options[:than])))
|
10
10
|
record.errors.add(attribute, :less, options)
|
11
11
|
end
|
12
12
|
end
|
data/test/greater_test.rb
CHANGED
@@ -5,8 +5,9 @@ class GreaterValidatorTest < ActiveSupport::TestCase
|
|
5
5
|
|
6
6
|
class ModelWithGreaterValidation
|
7
7
|
include ActiveModel::Validations
|
8
|
-
attr_accessor :attr1, :attr2
|
8
|
+
attr_accessor :attr1, :attr2, :attr3
|
9
9
|
validates :attr1, :greater => { :than => :attr2 }
|
10
|
+
validates :attr3, :greater => { :than => :attr2, :or_equal => true }
|
10
11
|
end
|
11
12
|
|
12
13
|
setup do
|
@@ -18,7 +19,6 @@ class GreaterValidatorTest < ActiveSupport::TestCase
|
|
18
19
|
assert_valid_attr1 3
|
19
20
|
assert_valid_attr1 4
|
20
21
|
assert_invalid_attr1 1
|
21
|
-
assert_invalid_attr1 2
|
22
22
|
end
|
23
23
|
|
24
24
|
test "greater date" do
|
@@ -27,7 +27,6 @@ class GreaterValidatorTest < ActiveSupport::TestCase
|
|
27
27
|
assert_valid_attr1 today + 1
|
28
28
|
assert_valid_attr1 today + 2
|
29
29
|
assert_invalid_attr1 today - 1
|
30
|
-
assert_invalid_attr1 today
|
31
30
|
end
|
32
31
|
|
33
32
|
test "greater string" do
|
@@ -35,9 +34,40 @@ class GreaterValidatorTest < ActiveSupport::TestCase
|
|
35
34
|
assert_valid_attr1 'd'
|
36
35
|
assert_valid_attr1 'e'
|
37
36
|
assert_invalid_attr1 'b'
|
37
|
+
end
|
38
|
+
|
39
|
+
test "equal integer is not valid" do
|
40
|
+
@model.attr2 = 2
|
41
|
+
assert_invalid_attr1 2
|
42
|
+
end
|
43
|
+
|
44
|
+
test "greater date is not valid" do
|
45
|
+
today = Date.today
|
46
|
+
@model.attr2 = today
|
47
|
+
assert_invalid_attr1 today
|
48
|
+
end
|
49
|
+
|
50
|
+
test "greater string is not valid" do
|
51
|
+
@model.attr2 = 'c'
|
38
52
|
assert_invalid_attr1 'c'
|
39
53
|
end
|
40
54
|
|
55
|
+
test "equal integer is valid if is_equal is true" do
|
56
|
+
@model.attr2 = 2
|
57
|
+
assert_valid_attr3 2
|
58
|
+
end
|
59
|
+
|
60
|
+
test "greater date is valid if is_equal is true" do
|
61
|
+
today = Date.today
|
62
|
+
@model.attr2 = today
|
63
|
+
assert_valid_attr3 today
|
64
|
+
end
|
65
|
+
|
66
|
+
test "greater string is valid if is_equal is true" do
|
67
|
+
@model.attr2 = 'c'
|
68
|
+
assert_valid_attr3 'c'
|
69
|
+
end
|
70
|
+
|
41
71
|
#########
|
42
72
|
protected
|
43
73
|
#########
|
@@ -51,4 +81,14 @@ class GreaterValidatorTest < ActiveSupport::TestCase
|
|
51
81
|
@model.attr1 = value
|
52
82
|
assert_invalid_attribute @model, :attr1
|
53
83
|
end
|
84
|
+
|
85
|
+
def assert_valid_attr3(value)
|
86
|
+
@model.attr3 = value
|
87
|
+
assert_valid_attribute(@model, :attr3)
|
88
|
+
end
|
89
|
+
|
90
|
+
def assert_invalid_attr3(value)
|
91
|
+
@model.attr3 = value
|
92
|
+
assert_invalid_attribute @model, :attr3
|
93
|
+
end
|
54
94
|
end
|
data/test/less_test.rb
CHANGED
@@ -5,8 +5,9 @@ class LessValidatorTest < ActiveSupport::TestCase
|
|
5
5
|
|
6
6
|
class ModelWithLessValidation
|
7
7
|
include ActiveModel::Validations
|
8
|
-
attr_accessor :attr1, :attr2
|
8
|
+
attr_accessor :attr1, :attr2, :attr3
|
9
9
|
validates :attr1, :less => { :than => :attr2 }
|
10
|
+
validates :attr3, :less => { :than => :attr2, :or_equal => true }
|
10
11
|
end
|
11
12
|
|
12
13
|
setup do
|
@@ -17,7 +18,6 @@ class LessValidatorTest < ActiveSupport::TestCase
|
|
17
18
|
@model.attr2 = 2
|
18
19
|
assert_valid_attr1 0
|
19
20
|
assert_valid_attr1 1
|
20
|
-
assert_invalid_attr1 2
|
21
21
|
assert_invalid_attr1 3
|
22
22
|
end
|
23
23
|
|
@@ -26,7 +26,6 @@ class LessValidatorTest < ActiveSupport::TestCase
|
|
26
26
|
@model.attr2 = today
|
27
27
|
assert_valid_attr1 today - 2
|
28
28
|
assert_valid_attr1 today - 1
|
29
|
-
assert_invalid_attr1 today
|
30
29
|
assert_invalid_attr1 today + 1
|
31
30
|
end
|
32
31
|
|
@@ -34,10 +33,41 @@ class LessValidatorTest < ActiveSupport::TestCase
|
|
34
33
|
@model.attr2 = 'c'
|
35
34
|
assert_valid_attr1 'a'
|
36
35
|
assert_valid_attr1 'b'
|
37
|
-
assert_invalid_attr1 'c'
|
38
36
|
assert_invalid_attr1 'd'
|
39
37
|
end
|
40
38
|
|
39
|
+
test "equal integer is not valid" do
|
40
|
+
@model.attr2 = 2
|
41
|
+
assert_invalid_attr1 2
|
42
|
+
end
|
43
|
+
|
44
|
+
test "greater date is not valid" do
|
45
|
+
today = Date.today
|
46
|
+
@model.attr2 = today
|
47
|
+
assert_invalid_attr1 today
|
48
|
+
end
|
49
|
+
|
50
|
+
test "greater string is not valid" do
|
51
|
+
@model.attr2 = 'c'
|
52
|
+
assert_invalid_attr1 'c'
|
53
|
+
end
|
54
|
+
|
55
|
+
test "equal integer is valid if is_equal is true" do
|
56
|
+
@model.attr2 = 2
|
57
|
+
assert_valid_attr3 2
|
58
|
+
end
|
59
|
+
|
60
|
+
test "greater date is valid if is_equal is true" do
|
61
|
+
today = Date.today
|
62
|
+
@model.attr2 = today
|
63
|
+
assert_valid_attr3 today
|
64
|
+
end
|
65
|
+
|
66
|
+
test "greater string is valid if is_equal is true" do
|
67
|
+
@model.attr2 = 'c'
|
68
|
+
assert_valid_attr3 'c'
|
69
|
+
end
|
70
|
+
|
41
71
|
#########
|
42
72
|
protected
|
43
73
|
#########
|
@@ -51,4 +81,14 @@ class LessValidatorTest < ActiveSupport::TestCase
|
|
51
81
|
@model.attr1 = value
|
52
82
|
assert_invalid_attribute @model, :attr1
|
53
83
|
end
|
84
|
+
|
85
|
+
def assert_valid_attr3(value)
|
86
|
+
@model.attr3 = value
|
87
|
+
assert_valid_attribute(@model, :attr3)
|
88
|
+
end
|
89
|
+
|
90
|
+
def assert_invalid_attr3(value)
|
91
|
+
@model.attr3 = value
|
92
|
+
assert_invalid_attribute @model, :attr3
|
93
|
+
end
|
54
94
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: evelpidon_validators
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 2
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Nikos Dimitrakopoulos
|
@@ -16,11 +16,10 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2011-11-
|
20
|
-
default_executable:
|
19
|
+
date: 2011-11-07 00:00:00 Z
|
21
20
|
dependencies:
|
22
21
|
- !ruby/object:Gem::Dependency
|
23
|
-
name:
|
22
|
+
name: rake
|
24
23
|
prerelease: false
|
25
24
|
requirement: &id001 !ruby/object:Gem::Requirement
|
26
25
|
none: false
|
@@ -34,7 +33,7 @@ dependencies:
|
|
34
33
|
type: :development
|
35
34
|
version_requirements: *id001
|
36
35
|
- !ruby/object:Gem::Dependency
|
37
|
-
name:
|
36
|
+
name: activesupport
|
38
37
|
prerelease: false
|
39
38
|
requirement: &id002 !ruby/object:Gem::Requirement
|
40
39
|
none: false
|
@@ -48,7 +47,7 @@ dependencies:
|
|
48
47
|
type: :development
|
49
48
|
version_requirements: *id002
|
50
49
|
- !ruby/object:Gem::Dependency
|
51
|
-
name:
|
50
|
+
name: evelpidon_test_helpers
|
52
51
|
prerelease: false
|
53
52
|
requirement: &id003 !ruby/object:Gem::Requirement
|
54
53
|
none: false
|
@@ -59,8 +58,22 @@ dependencies:
|
|
59
58
|
segments:
|
60
59
|
- 0
|
61
60
|
version: "0"
|
62
|
-
type: :
|
61
|
+
type: :development
|
63
62
|
version_requirements: *id003
|
63
|
+
- !ruby/object:Gem::Dependency
|
64
|
+
name: activemodel
|
65
|
+
prerelease: false
|
66
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
hash: 3
|
72
|
+
segments:
|
73
|
+
- 0
|
74
|
+
version: "0"
|
75
|
+
type: :runtime
|
76
|
+
version_requirements: *id004
|
64
77
|
description: Useful ActiveModel validators with ClientSideValidations support.
|
65
78
|
email:
|
66
79
|
- n.dimitrakopoulos@pamediakopes.gr
|
@@ -94,7 +107,6 @@ files:
|
|
94
107
|
- test/greater_test.rb
|
95
108
|
- test/less_test.rb
|
96
109
|
- test/test_helper.rb
|
97
|
-
has_rdoc: true
|
98
110
|
homepage: ""
|
99
111
|
licenses: []
|
100
112
|
|
@@ -124,12 +136,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
124
136
|
requirements: []
|
125
137
|
|
126
138
|
rubyforge_project: evelpidon_validators
|
127
|
-
rubygems_version: 1.
|
139
|
+
rubygems_version: 1.8.10
|
128
140
|
signing_key:
|
129
141
|
specification_version: 3
|
130
142
|
summary: Useful ActiveModel validators
|
131
|
-
test_files:
|
132
|
-
|
133
|
-
|
134
|
-
- test/less_test.rb
|
135
|
-
- test/test_helper.rb
|
143
|
+
test_files: []
|
144
|
+
|
145
|
+
has_rdoc:
|