hashrocket-validatable 1.7.2 → 1.7.4
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/VERSION.yml +1 -1
- data/lib/errors.rb +2 -0
- data/lib/validations/validates_associated.rb +13 -0
- data/test/functional/test_validates_associated.rb +41 -0
- data/test/unit/test_errors.rb +7 -1
- data/test/unit/test_validates_associated.rb +29 -0
- metadata +11 -5
data/VERSION.yml
CHANGED
data/lib/errors.rb
CHANGED
@@ -28,6 +28,8 @@ module Validatable
|
|
28
28
|
return nil if errors[attribute.to_sym].nil?
|
29
29
|
errors[attribute.to_sym].size == 1 ? errors[attribute.to_sym].first : errors[attribute.to_sym]
|
30
30
|
end
|
31
|
+
|
32
|
+
alias [] on
|
31
33
|
|
32
34
|
def add(attribute, message) #:nodoc:
|
33
35
|
errors[attribute.to_sym] = [] if errors[attribute.to_sym].nil?
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Validatable
|
2
|
+
class ValidatesAssociated < ValidationBase #:nodoc:
|
3
|
+
def valid?(instance)
|
4
|
+
Array(instance.send(attribute)).compact.map do |child|
|
5
|
+
child.valid?
|
6
|
+
end.all?
|
7
|
+
end
|
8
|
+
|
9
|
+
def message(instance)
|
10
|
+
super || "is invalid"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
|
2
|
+
|
3
|
+
module Functional
|
4
|
+
class ValidatesAssociatedTest < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
@parent = Class.new do
|
7
|
+
include Validatable
|
8
|
+
attr_accessor :children
|
9
|
+
validates_associated :children
|
10
|
+
end
|
11
|
+
@child = Class.new do
|
12
|
+
include Validatable
|
13
|
+
attr_accessor :name
|
14
|
+
validates_presence_of :name
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
test "given invalid child, when validated, then error is in the parent's error collection" do
|
19
|
+
instance = @parent.new
|
20
|
+
instance.children = [@child.new]
|
21
|
+
instance.valid?
|
22
|
+
assert_equal "is invalid", instance.errors.on(:children)
|
23
|
+
end
|
24
|
+
|
25
|
+
test "given two invalid children, when validated, then both children have errors" do
|
26
|
+
instance = @parent.new
|
27
|
+
instance.children = [@child.new, @child.new]
|
28
|
+
instance.valid?
|
29
|
+
instance.children.each do |child|
|
30
|
+
assert_not_nil child.errors.on(:name)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
test "given valid child, when validated, the parent is valid" do
|
35
|
+
instance = @parent.new
|
36
|
+
instance.children = [@child.new]
|
37
|
+
instance.children.first.name = 'x'
|
38
|
+
assert instance.valid?
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/test/unit/test_errors.rb
CHANGED
@@ -60,5 +60,11 @@ Expectations do
|
|
60
60
|
errors.add(:attribute1, "message1")
|
61
61
|
errors.add(:attribute2, "message2")
|
62
62
|
errors.count
|
63
|
-
end
|
63
|
+
end
|
64
|
+
|
65
|
+
expect "message" do
|
66
|
+
errors = Validatable::Errors.new
|
67
|
+
errors.add(:attribute, "message")
|
68
|
+
errors[:attribute]
|
69
|
+
end
|
64
70
|
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
|
2
|
+
|
3
|
+
Expectations do
|
4
|
+
|
5
|
+
expect false do
|
6
|
+
parent = stub('parent', :child => stub(:valid? => false))
|
7
|
+
validation = Validatable::ValidatesAssociated.new stub_everything, :child
|
8
|
+
validation.valid?(parent)
|
9
|
+
end
|
10
|
+
|
11
|
+
expect true do
|
12
|
+
parent = stub('parent', :child => stub(:valid? => true))
|
13
|
+
validation = Validatable::ValidatesAssociated.new stub_everything, :child
|
14
|
+
validation.valid?(parent)
|
15
|
+
end
|
16
|
+
|
17
|
+
expect false do
|
18
|
+
parent = stub('parent', :children => [stub(:valid? => false)])
|
19
|
+
validation = Validatable::ValidatesAssociated.new stub_everything, :children
|
20
|
+
validation.valid?(parent)
|
21
|
+
end
|
22
|
+
|
23
|
+
expect true do
|
24
|
+
parent = stub('parent', :children => [stub(:valid? => true)])
|
25
|
+
validation = Validatable::ValidatesAssociated.new stub_everything, :children
|
26
|
+
validation.valid?(parent)
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hashrocket-validatable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.7.
|
4
|
+
version: 1.7.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jay Fields
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2009-
|
13
|
+
date: 2009-08-31 00:00:00 -07:00
|
14
14
|
default_executable:
|
15
15
|
dependencies: []
|
16
16
|
|
@@ -37,6 +37,7 @@ files:
|
|
37
37
|
- lib/validatable_class_methods.rb
|
38
38
|
- lib/validatable_instance_methods.rb
|
39
39
|
- lib/validations/validates_acceptance_of.rb
|
40
|
+
- lib/validations/validates_associated.rb
|
40
41
|
- lib/validations/validates_confirmation_of.rb
|
41
42
|
- lib/validations/validates_each.rb
|
42
43
|
- lib/validations/validates_format_of.rb
|
@@ -48,6 +49,7 @@ files:
|
|
48
49
|
- test/all_tests.rb
|
49
50
|
- test/functional/test_validatable.rb
|
50
51
|
- test/functional/test_validates_acceptance_of.rb
|
52
|
+
- test/functional/test_validates_associated.rb
|
51
53
|
- test/functional/test_validates_confirmation_of.rb
|
52
54
|
- test/functional/test_validates_each.rb
|
53
55
|
- test/functional/test_validates_format_of.rb
|
@@ -60,6 +62,7 @@ files:
|
|
60
62
|
- test/unit/test_understandable.rb
|
61
63
|
- test/unit/test_validatable.rb
|
62
64
|
- test/unit/test_validates_acceptance_of.rb
|
65
|
+
- test/unit/test_validates_associated.rb
|
63
66
|
- test/unit/test_validates_confirmation_of.rb
|
64
67
|
- test/unit/test_validates_format_of.rb
|
65
68
|
- test/unit/test_validates_length_of.rb
|
@@ -67,8 +70,9 @@ files:
|
|
67
70
|
- test/unit/test_validates_presence_of.rb
|
68
71
|
- test/unit/test_validates_true_for.rb
|
69
72
|
- test/unit/test_validation_base.rb
|
70
|
-
has_rdoc:
|
73
|
+
has_rdoc: false
|
71
74
|
homepage: http://github.com/jnunemaker/validatable
|
75
|
+
licenses:
|
72
76
|
post_install_message:
|
73
77
|
rdoc_options:
|
74
78
|
- --charset=UTF-8
|
@@ -89,14 +93,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
89
93
|
requirements: []
|
90
94
|
|
91
95
|
rubyforge_project:
|
92
|
-
rubygems_version: 1.
|
96
|
+
rubygems_version: 1.3.5
|
93
97
|
signing_key:
|
94
|
-
specification_version:
|
98
|
+
specification_version: 3
|
95
99
|
summary: Validatable is a library for adding validations.
|
96
100
|
test_files:
|
97
101
|
- test/all_tests.rb
|
98
102
|
- test/functional/test_validatable.rb
|
99
103
|
- test/functional/test_validates_acceptance_of.rb
|
104
|
+
- test/functional/test_validates_associated.rb
|
100
105
|
- test/functional/test_validates_confirmation_of.rb
|
101
106
|
- test/functional/test_validates_each.rb
|
102
107
|
- test/functional/test_validates_format_of.rb
|
@@ -109,6 +114,7 @@ test_files:
|
|
109
114
|
- test/unit/test_understandable.rb
|
110
115
|
- test/unit/test_validatable.rb
|
111
116
|
- test/unit/test_validates_acceptance_of.rb
|
117
|
+
- test/unit/test_validates_associated.rb
|
112
118
|
- test/unit/test_validates_confirmation_of.rb
|
113
119
|
- test/unit/test_validates_format_of.rb
|
114
120
|
- test/unit/test_validates_length_of.rb
|