jnunemaker-validatable 1.7.2 → 1.7.3

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 CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :major: 1
3
3
  :minor: 7
4
- :patch: 2
4
+ :patch: 3
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?
data/lib/macros.rb CHANGED
@@ -193,7 +193,30 @@ module Validatable
193
193
  def validates_true_for(*args)
194
194
  add_validations(args, ValidatesTrueFor)
195
195
  end
196
-
196
+
197
+ # call-seq: validates_associated(*args)
198
+ #
199
+ # Checks the validity of an associated object or objects and adds a single
200
+ # error if validation fails.
201
+ #
202
+ # class Person
203
+ # include Validatable
204
+ # attr_accessor :addresses
205
+ # validates_associated :addresses
206
+ # end
207
+ #
208
+ # Configuration options:
209
+ #
210
+ # * after_validate - A block that executes following the run of a validation
211
+ # * message - The message to add to the errors collection when the validation fails
212
+ # * times - The number of times the validation applies
213
+ # * level - The level at which the validation should occur
214
+ # * if - A block that when executed must return true of the validation will not occur
215
+ # * group - The group that this validation belongs to. A validation can belong to multiple groups
216
+ def validates_associated(*args)
217
+ add_validations(args, ValidatesAssociated)
218
+ end
219
+
197
220
  # call-seq: include_validations_from(attribute)
198
221
  #
199
222
  # Includes all the validations that are defined on the attribute.
@@ -280,4 +303,4 @@ module Validatable
280
303
  before_validations << block
281
304
  end
282
305
  end
283
- end
306
+ end
data/lib/validatable.rb CHANGED
@@ -21,4 +21,5 @@ require File.join(dir, 'validations/validates_confirmation_of')
21
21
  require File.join(dir, 'validations/validates_length_of')
22
22
  require File.join(dir, 'validations/validates_true_for')
23
23
  require File.join(dir, 'validations/validates_numericality_of')
24
- require File.join(dir, 'validations/validates_each')
24
+ require File.join(dir, 'validations/validates_each')
25
+ require File.join(dir, 'validations/validates_associated')
@@ -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
@@ -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: jnunemaker-validatable
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.2
4
+ version: 1.7.3
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-07-27 00:00:00 -07:00
13
+ date: 2009-10-02 00:00:00 -04: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
@@ -69,7 +72,8 @@ files:
69
72
  - test/unit/test_validation_base.rb
70
73
  has_rdoc: true
71
74
  homepage: http://github.com/jnunemaker/validatable
72
- licenses:
75
+ licenses: []
76
+
73
77
  post_install_message:
74
78
  rdoc_options:
75
79
  - --charset=UTF-8
@@ -92,12 +96,13 @@ requirements: []
92
96
  rubyforge_project:
93
97
  rubygems_version: 1.3.5
94
98
  signing_key:
95
- specification_version: 2
99
+ specification_version: 3
96
100
  summary: Validatable is a library for adding validations.
97
101
  test_files:
98
102
  - test/all_tests.rb
99
103
  - test/functional/test_validatable.rb
100
104
  - test/functional/test_validates_acceptance_of.rb
105
+ - test/functional/test_validates_associated.rb
101
106
  - test/functional/test_validates_confirmation_of.rb
102
107
  - test/functional/test_validates_each.rb
103
108
  - test/functional/test_validates_format_of.rb
@@ -110,6 +115,7 @@ test_files:
110
115
  - test/unit/test_understandable.rb
111
116
  - test/unit/test_validatable.rb
112
117
  - test/unit/test_validates_acceptance_of.rb
118
+ - test/unit/test_validates_associated.rb
113
119
  - test/unit/test_validates_confirmation_of.rb
114
120
  - test/unit/test_validates_format_of.rb
115
121
  - test/unit/test_validates_length_of.rb