evelpidon_validators 0.2.0 → 0.3.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 +6 -0
- data/Gemfile +4 -0
- data/README.md +25 -10
- data/evelpidon_validators.gemspec +1 -0
- data/lib/assets/javascripts/evelpidon_validators.js +1 -1
- data/lib/evelpidon_validators/all.rb +1 -0
- data/lib/evelpidon_validators/associated.rb +15 -0
- data/lib/evelpidon_validators/version.rb +1 -1
- data/test/associated_test.rb +55 -0
- metadata +64 -83
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
# Evelpidon Validators changelog
|
2
2
|
|
3
|
+
## 0.3.0 / 2011-12-29
|
4
|
+
|
5
|
+
* Added AssociationValidator
|
6
|
+
* Fix for `than` validation options finding only input siblings
|
7
|
+
* README updates (+ StillMaintained and Travis-CI)
|
8
|
+
|
3
9
|
## 0.2.0 / 2011-11-07
|
4
10
|
|
5
11
|
* or_equal option to greater and less validators
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
# Evelpidon Validators (for Rails / ActiveModel / ActiveRecord)
|
2
2
|
|
3
|
+
[](http://stillmaintained.com/e-travel/evelpidon_validators)
|
4
|
+
[](http://travis-ci.org/e-travel/evelpidon_validators)
|
5
|
+
|
3
6
|
Collection of various ActiveModel validators, alongside their client side implementation
|
4
7
|
as ClientSideValidations::Middleware (see [ClientSideValidations](https://github.com/bcardarella/client_side_validations)
|
5
8
|
for more info). Client side validations are optional and are enabled only if the client_side_validations gem is
|
@@ -17,31 +20,41 @@ already loaded.
|
|
17
20
|
|
18
21
|
Add on your Gemfile :
|
19
22
|
|
20
|
-
|
23
|
+
```ruby
|
24
|
+
gem 'evelpidon_validators'
|
25
|
+
```
|
21
26
|
|
22
27
|
### By hand
|
23
28
|
|
24
29
|
On the console :
|
25
30
|
|
26
|
-
|
31
|
+
```bash
|
32
|
+
gem install evelpidon_validators
|
33
|
+
```
|
27
34
|
|
28
35
|
On your code :
|
29
36
|
|
30
|
-
|
37
|
+
```ruby
|
38
|
+
require 'evelpidon_validators'
|
39
|
+
```
|
31
40
|
|
32
41
|
## Usage
|
33
42
|
|
34
43
|
Validators are *not* automatically loaded. You can require the validators you need either explicitly one-by-one
|
35
44
|
or all of them. So for example :
|
36
45
|
|
37
|
-
|
38
|
-
|
39
|
-
|
46
|
+
```ruby
|
47
|
+
# Load only "Greater than" and "Less than" validators :
|
48
|
+
require 'evelpidon_validators/greater'
|
49
|
+
require 'evelpidon_validators/less'
|
50
|
+
```
|
40
51
|
|
41
52
|
or
|
42
53
|
|
43
|
-
|
44
|
-
|
54
|
+
```ruby
|
55
|
+
# Load everything
|
56
|
+
require 'evelpidon_validators/all'
|
57
|
+
```
|
45
58
|
|
46
59
|
### Enable client side validations support
|
47
60
|
|
@@ -52,8 +65,10 @@ Client-side validations work out of the box with Rails 3.1 (through the asset pi
|
|
52
65
|
|
53
66
|
So for example the following should work :
|
54
67
|
|
55
|
-
|
56
|
-
|
68
|
+
```javascript
|
69
|
+
//= require rails.validations
|
70
|
+
//= require evelpidon_validators
|
71
|
+
```
|
57
72
|
|
58
73
|
## TODOs
|
59
74
|
|
@@ -21,6 +21,7 @@ Gem::Specification.new do |s|
|
|
21
21
|
s.add_development_dependency "rake"
|
22
22
|
s.add_development_dependency "activesupport"
|
23
23
|
s.add_development_dependency "evelpidon_test_helpers"
|
24
|
+
s.add_development_dependency "test-unit"
|
24
25
|
|
25
26
|
s.add_runtime_dependency "activemodel"
|
26
27
|
end
|
@@ -5,5 +5,5 @@ clientSideValidations.helpers = clientSideValidations.helpers || {};
|
|
5
5
|
|
6
6
|
clientSideValidations.helpers.getSiblingField = function(field, siblingName) {
|
7
7
|
var thanName = field.attr('name').replace(/\[[^\]]*\]/, '[' + siblingName + ']');
|
8
|
-
return field.parents('form').find("
|
8
|
+
return field.parents('form').find("[name='" + thanName + "']");
|
9
9
|
};
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module ActiveModel
|
2
|
+
module Validations
|
3
|
+
# Shamelessly stolen from:
|
4
|
+
# https://github.com/rails/rails/blob/v3.1.1/activerecord/lib/active_record/validations/associated.rb
|
5
|
+
#
|
6
|
+
# Beware! This validator is not triggered in the client side, however if the association is in a form, client side
|
7
|
+
# validations will handle that on its own.
|
8
|
+
class AssociatedValidator < EachValidator
|
9
|
+
def validate_each(record, attribute, value)
|
10
|
+
return if (value.is_a?(Array) ? value : [value]).collect{ |r| r.nil? || r.valid? }.all?
|
11
|
+
record.errors.add(attribute, :invalid, options.merge(:value => value))
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'evelpidon_validators/associated'
|
3
|
+
|
4
|
+
# Tests inspired by:
|
5
|
+
# https://github.com/rails/rails/blob/v3.1.1/activerecord/test/cases/validations/association_validation_test.rb
|
6
|
+
class AssociatedValidatorTest < ActiveSupport::TestCase
|
7
|
+
|
8
|
+
class Topic
|
9
|
+
include ActiveModel::Validations
|
10
|
+
attr_accessor :title, :content, :replies
|
11
|
+
|
12
|
+
def initialize(params = {})
|
13
|
+
self.title = params['title']
|
14
|
+
self.content = params['content']
|
15
|
+
self.replies = params['replies']
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class Reply
|
20
|
+
include ActiveModel::Validations
|
21
|
+
attr_accessor :title, :content, :topic
|
22
|
+
|
23
|
+
def initialize(params = {})
|
24
|
+
self.title = params['title']
|
25
|
+
self.content = params['content']
|
26
|
+
self.topic = params['topic']
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_validates_associated_many
|
31
|
+
Topic.validates :replies, :associated => true
|
32
|
+
Reply.validates :content, :presence => true
|
33
|
+
t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
|
34
|
+
t.replies = [r = Reply.new("title" => "A reply"), r2 = Reply.new("title" => "Another reply", "content" => "non-empty"), r3 = Reply.new("title" => "Yet another reply"), r4 = Reply.new("title" => "The last reply", "content" => "non-empty")]
|
35
|
+
assert !t.valid?
|
36
|
+
assert t.errors[:replies].any?
|
37
|
+
assert_equal 1, r.errors.count # make sure all associated objects have been validated
|
38
|
+
assert_equal 0, r2.errors.count
|
39
|
+
assert_equal 1, r3.errors.count
|
40
|
+
assert_equal 0, r4.errors.count
|
41
|
+
r.content = r3.content = "non-empty"
|
42
|
+
assert t.valid?
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_validates_associated_one
|
46
|
+
Reply.validates :topic, :associated => true
|
47
|
+
Topic.validates :content, :presence => true
|
48
|
+
r = Reply.new("title" => "A reply", "content" => "with content!")
|
49
|
+
r.topic = Topic.new("title" => "uhohuhoh")
|
50
|
+
assert !r.valid?
|
51
|
+
assert r.errors[:topic].any?
|
52
|
+
r.topic.content = "non-empty"
|
53
|
+
assert r.valid?
|
54
|
+
end
|
55
|
+
end
|
metadata
CHANGED
@@ -1,90 +1,80 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: evelpidon_validators
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.3.0
|
5
5
|
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 2
|
9
|
-
- 0
|
10
|
-
version: 0.2.0
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Nikos Dimitrakopoulos
|
14
9
|
- Eric Cohen
|
15
10
|
autorequire:
|
16
11
|
bindir: bin
|
17
12
|
cert_chain: []
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
- !ruby/object:Gem::Dependency
|
13
|
+
date: 2011-12-29 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
22
16
|
name: rake
|
23
|
-
|
24
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
17
|
+
requirement: &17211260 !ruby/object:Gem::Requirement
|
25
18
|
none: false
|
26
|
-
requirements:
|
27
|
-
- -
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
|
30
|
-
segments:
|
31
|
-
- 0
|
32
|
-
version: "0"
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '0'
|
33
23
|
type: :development
|
34
|
-
version_requirements: *id001
|
35
|
-
- !ruby/object:Gem::Dependency
|
36
|
-
name: activesupport
|
37
24
|
prerelease: false
|
38
|
-
|
25
|
+
version_requirements: *17211260
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: activesupport
|
28
|
+
requirement: &17210780 !ruby/object:Gem::Requirement
|
39
29
|
none: false
|
40
|
-
requirements:
|
41
|
-
- -
|
42
|
-
- !ruby/object:Gem::Version
|
43
|
-
|
44
|
-
segments:
|
45
|
-
- 0
|
46
|
-
version: "0"
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
47
34
|
type: :development
|
48
|
-
|
49
|
-
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *17210780
|
37
|
+
- !ruby/object:Gem::Dependency
|
50
38
|
name: evelpidon_test_helpers
|
39
|
+
requirement: &17210360 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
type: :development
|
51
46
|
prerelease: false
|
52
|
-
|
47
|
+
version_requirements: *17210360
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: test-unit
|
50
|
+
requirement: &17209920 !ruby/object:Gem::Requirement
|
53
51
|
none: false
|
54
|
-
requirements:
|
55
|
-
- -
|
56
|
-
- !ruby/object:Gem::Version
|
57
|
-
|
58
|
-
segments:
|
59
|
-
- 0
|
60
|
-
version: "0"
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
61
56
|
type: :development
|
62
|
-
version_requirements: *id003
|
63
|
-
- !ruby/object:Gem::Dependency
|
64
|
-
name: activemodel
|
65
57
|
prerelease: false
|
66
|
-
|
58
|
+
version_requirements: *17209920
|
59
|
+
- !ruby/object:Gem::Dependency
|
60
|
+
name: activemodel
|
61
|
+
requirement: &17209440 !ruby/object:Gem::Requirement
|
67
62
|
none: false
|
68
|
-
requirements:
|
69
|
-
- -
|
70
|
-
- !ruby/object:Gem::Version
|
71
|
-
|
72
|
-
segments:
|
73
|
-
- 0
|
74
|
-
version: "0"
|
63
|
+
requirements:
|
64
|
+
- - ! '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
75
67
|
type: :runtime
|
76
|
-
|
68
|
+
prerelease: false
|
69
|
+
version_requirements: *17209440
|
77
70
|
description: Useful ActiveModel validators with ClientSideValidations support.
|
78
|
-
email:
|
71
|
+
email:
|
79
72
|
- n.dimitrakopoulos@pamediakopes.gr
|
80
73
|
- e.koen@pamediakopes.gr
|
81
74
|
executables: []
|
82
|
-
|
83
75
|
extensions: []
|
84
|
-
|
85
76
|
extra_rdoc_files: []
|
86
|
-
|
87
|
-
files:
|
77
|
+
files:
|
88
78
|
- .gitignore
|
89
79
|
- CHANGELOG.md
|
90
80
|
- Gemfile
|
@@ -98,48 +88,39 @@ files:
|
|
98
88
|
- lib/assets/javascripts/evelpidon_validators/less.js
|
99
89
|
- lib/evelpidon_validators.rb
|
100
90
|
- lib/evelpidon_validators/all.rb
|
91
|
+
- lib/evelpidon_validators/associated.rb
|
101
92
|
- lib/evelpidon_validators/different.rb
|
102
93
|
- lib/evelpidon_validators/greater.rb
|
103
94
|
- lib/evelpidon_validators/less.rb
|
104
95
|
- lib/evelpidon_validators/rails.rb
|
105
96
|
- lib/evelpidon_validators/version.rb
|
97
|
+
- test/associated_test.rb
|
106
98
|
- test/different_test.rb
|
107
99
|
- test/greater_test.rb
|
108
100
|
- test/less_test.rb
|
109
101
|
- test/test_helper.rb
|
110
|
-
homepage:
|
102
|
+
homepage: ''
|
111
103
|
licenses: []
|
112
|
-
|
113
104
|
post_install_message:
|
114
105
|
rdoc_options: []
|
115
|
-
|
116
|
-
require_paths:
|
106
|
+
require_paths:
|
117
107
|
- lib
|
118
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
108
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
119
109
|
none: false
|
120
|
-
requirements:
|
121
|
-
- -
|
122
|
-
- !ruby/object:Gem::Version
|
123
|
-
|
124
|
-
|
125
|
-
- 0
|
126
|
-
version: "0"
|
127
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - ! '>='
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
128
115
|
none: false
|
129
|
-
requirements:
|
130
|
-
- -
|
131
|
-
- !ruby/object:Gem::Version
|
132
|
-
|
133
|
-
segments:
|
134
|
-
- 0
|
135
|
-
version: "0"
|
116
|
+
requirements:
|
117
|
+
- - ! '>='
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
136
120
|
requirements: []
|
137
|
-
|
138
121
|
rubyforge_project: evelpidon_validators
|
139
122
|
rubygems_version: 1.8.10
|
140
123
|
signing_key:
|
141
124
|
specification_version: 3
|
142
125
|
summary: Useful ActiveModel validators
|
143
126
|
test_files: []
|
144
|
-
|
145
|
-
has_rdoc:
|