durran-validatable 1.8.4 → 2.0.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/VERSION.yml +3 -3
- data/lib/macros.rb +1 -1
- data/lib/validatable_instance_methods.rb +14 -18
- data/test/functional/test_validatable.rb +22 -117
- metadata +2 -2
data/VERSION.yml
CHANGED
data/lib/macros.rb
CHANGED
@@ -4,42 +4,38 @@ module Validatable
|
|
4
4
|
klass.extend Validatable::Macros
|
5
5
|
klass.class_eval do
|
6
6
|
include ActiveSupport::Callbacks
|
7
|
-
define_callbacks :
|
7
|
+
define_callbacks :before_validation, :after_validation
|
8
8
|
end
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
11
|
# call-seq: valid?
|
12
12
|
#
|
13
13
|
# Returns true if no errors were added otherwise false. Only executes validations that have no :groups option specified
|
14
14
|
def valid?
|
15
15
|
valid_for_group?(nil)
|
16
16
|
end
|
17
|
-
|
17
|
+
|
18
18
|
# call-seq: errors
|
19
19
|
#
|
20
20
|
# Returns the Errors object that holds all information about attribute error messages.
|
21
21
|
def errors
|
22
22
|
@errors ||= Validatable::Errors.new
|
23
23
|
end
|
24
|
-
|
24
|
+
|
25
25
|
def valid_for_group?(group) #:nodoc:
|
26
26
|
run_before_validations
|
27
27
|
errors.clear
|
28
|
-
run_callbacks(:
|
29
|
-
|
30
|
-
if respond_to?(:new?)
|
31
|
-
new? ? run_callbacks(:validate_on_create) : run_callbacks(:validate_on_update)
|
32
|
-
end
|
33
|
-
|
28
|
+
run_callbacks(:before_validation)
|
34
29
|
self.class.validate_children(self, group)
|
35
30
|
self.validate_group(group)
|
31
|
+
run_callbacks(:after_validation)
|
36
32
|
errors.empty?
|
37
33
|
end
|
38
|
-
|
34
|
+
|
39
35
|
def times_validated(key) #:nodoc:
|
40
36
|
times_validated_hash[key] || 0
|
41
37
|
end
|
42
|
-
|
38
|
+
|
43
39
|
def increment_times_validated_for(validation) #:nodoc:
|
44
40
|
if validation.key != nil
|
45
41
|
if times_validated_hash[validation.key].nil?
|
@@ -86,31 +82,31 @@ module Validatable
|
|
86
82
|
increment_times_validated_for(validation)
|
87
83
|
validation.run_after_validate(validation_result, self, validation.attribute)
|
88
84
|
end
|
89
|
-
|
85
|
+
|
90
86
|
def run_before_validations #:nodoc:
|
91
87
|
self.class.all_before_validations.each do |block|
|
92
88
|
instance_eval &block
|
93
89
|
end
|
94
90
|
end
|
95
|
-
|
91
|
+
|
96
92
|
def add_error(attribute, message) #:nodoc:
|
97
93
|
self.class.add_error(self, attribute, message)
|
98
94
|
end
|
99
|
-
|
95
|
+
|
100
96
|
def validations_for_level_and_group(level, group) #:nodoc:
|
101
97
|
validations_for_level = self.all_validations.select { |validation| validation.level == level }
|
102
98
|
return validations_for_level.select { |validation| validation.groups.empty? } if group.nil?
|
103
99
|
validations_for_level.select { |validation| validation.groups.include?(group) }
|
104
100
|
end
|
105
|
-
|
101
|
+
|
106
102
|
def all_validations #:nodoc:
|
107
103
|
res = self.class.validations_to_include.inject(self.class.all_validations) do |result, included_validation_class|
|
108
104
|
result += self.send(included_validation_class.attribute).all_validations
|
109
105
|
result
|
110
106
|
end
|
111
107
|
end
|
112
|
-
|
108
|
+
|
113
109
|
def validation_levels #:nodoc:
|
114
110
|
self.class.all_validations.inject([1]) { |result, validation| result << validation.level }.uniq.sort
|
115
111
|
end
|
116
|
-
end
|
112
|
+
end
|
@@ -9,7 +9,7 @@ functional_tests do
|
|
9
9
|
end
|
10
10
|
klass = Class.new do
|
11
11
|
include Validatable
|
12
|
-
include_validations_from :child
|
12
|
+
include_validations_from :child
|
13
13
|
define_method :child do
|
14
14
|
child_class
|
15
15
|
end
|
@@ -19,7 +19,7 @@ functional_tests do
|
|
19
19
|
instance.valid?
|
20
20
|
instance.errors.on(:name)
|
21
21
|
end
|
22
|
-
|
22
|
+
|
23
23
|
expect "can't be blank" do
|
24
24
|
child_class = Module.new do
|
25
25
|
include Validatable
|
@@ -28,7 +28,7 @@ functional_tests do
|
|
28
28
|
klass = Class.new do
|
29
29
|
include Validatable
|
30
30
|
validates_presence_of :address
|
31
|
-
include_validations_from :child
|
31
|
+
include_validations_from :child
|
32
32
|
define_method :child do
|
33
33
|
child_class
|
34
34
|
end
|
@@ -38,21 +38,21 @@ functional_tests do
|
|
38
38
|
instance.valid?
|
39
39
|
instance.errors.on(:address)
|
40
40
|
end
|
41
|
-
|
41
|
+
|
42
42
|
expect :is_set do
|
43
43
|
klass = Class.new do
|
44
44
|
include Validatable
|
45
45
|
attr_accessor :result
|
46
|
-
|
46
|
+
before_validate do
|
47
47
|
self.result = :is_set
|
48
48
|
end
|
49
49
|
end
|
50
|
-
|
50
|
+
|
51
51
|
instance = klass.new
|
52
52
|
instance.valid?
|
53
53
|
instance.result
|
54
54
|
end
|
55
|
-
|
55
|
+
|
56
56
|
expect :is_set do
|
57
57
|
klass = Class.new do
|
58
58
|
include Validatable
|
@@ -77,7 +77,7 @@ functional_tests do
|
|
77
77
|
end
|
78
78
|
subsubclass.new.valid?
|
79
79
|
end
|
80
|
-
|
80
|
+
|
81
81
|
expect false do
|
82
82
|
klass = Class.new do
|
83
83
|
include Validatable
|
@@ -88,7 +88,7 @@ functional_tests do
|
|
88
88
|
end
|
89
89
|
subclass.new.valid?
|
90
90
|
end
|
91
|
-
|
91
|
+
|
92
92
|
expect true do
|
93
93
|
klass = Class.new do
|
94
94
|
include Validatable
|
@@ -129,7 +129,7 @@ functional_tests do
|
|
129
129
|
end
|
130
130
|
klass = Class.new do
|
131
131
|
include Validatable
|
132
|
-
include_errors_from :child
|
132
|
+
include_errors_from :child
|
133
133
|
define_method :child do
|
134
134
|
child_class.new
|
135
135
|
end
|
@@ -148,7 +148,7 @@ functional_tests do
|
|
148
148
|
end
|
149
149
|
klass = Class.new do
|
150
150
|
include Validatable
|
151
|
-
include_errors_from :child
|
151
|
+
include_errors_from :child
|
152
152
|
define_method :child do
|
153
153
|
child_class.new
|
154
154
|
end
|
@@ -171,7 +171,7 @@ functional_tests do
|
|
171
171
|
extend Forwardable
|
172
172
|
def_delegator :child, :name
|
173
173
|
validates_true_for :name, :logic => lambda { false }, :level => 2, :message => "invalid message"
|
174
|
-
include_errors_from :child
|
174
|
+
include_errors_from :child
|
175
175
|
define_method :child do
|
176
176
|
@child ||= child_class.new
|
177
177
|
end
|
@@ -191,7 +191,7 @@ functional_tests do
|
|
191
191
|
klass = Class.new do
|
192
192
|
include Validatable
|
193
193
|
validates_true_for :address, :logic => lambda { false }, :level => 1, :message => "invalid message"
|
194
|
-
include_errors_from :child
|
194
|
+
include_errors_from :child
|
195
195
|
define_method :child do
|
196
196
|
@child ||= child_class.new
|
197
197
|
end
|
@@ -255,13 +255,13 @@ functional_tests do
|
|
255
255
|
instance = klass.new
|
256
256
|
instance.valid?
|
257
257
|
end
|
258
|
-
|
258
|
+
|
259
259
|
test ':if with symbol should work' do
|
260
260
|
klass = Class.new do
|
261
261
|
include Validatable
|
262
262
|
attr_accessor :name, :name_required
|
263
263
|
validates_presence_of :name, :if => :name_required?
|
264
|
-
|
264
|
+
|
265
265
|
def name_required?
|
266
266
|
name_required
|
267
267
|
end
|
@@ -273,13 +273,13 @@ functional_tests do
|
|
273
273
|
assert !instance.valid?
|
274
274
|
assert_equal "can't be blank", instance.errors.on(:name)
|
275
275
|
end
|
276
|
-
|
276
|
+
|
277
277
|
test ':if with string should work' do
|
278
278
|
klass = Class.new do
|
279
279
|
include Validatable
|
280
280
|
attr_accessor :name, :name_required
|
281
281
|
validates_presence_of :name, :if => 'name_required?'
|
282
|
-
|
282
|
+
|
283
283
|
def name_required?
|
284
284
|
name_required
|
285
285
|
end
|
@@ -430,7 +430,7 @@ functional_tests do
|
|
430
430
|
instance.valid?
|
431
431
|
instance.errors.on(:address)
|
432
432
|
end
|
433
|
-
|
433
|
+
|
434
434
|
expect "Mod::Klass/Validatable::ValidatesPresenceOf/name" do
|
435
435
|
module Mod
|
436
436
|
class Klass
|
@@ -448,7 +448,7 @@ functional_tests do
|
|
448
448
|
end
|
449
449
|
klass.validations.first.key
|
450
450
|
end
|
451
|
-
|
451
|
+
|
452
452
|
expect "can't be blank" do
|
453
453
|
klass = Class.new do
|
454
454
|
include Validatable
|
@@ -470,61 +470,13 @@ functional_tests do
|
|
470
470
|
instance.validate_only("presence_of/name")
|
471
471
|
instance.errors.on(:address)
|
472
472
|
end
|
473
|
-
|
473
|
+
|
474
474
|
test 'validate callback' do
|
475
475
|
klass = Class.new do
|
476
476
|
include Validatable
|
477
477
|
attr_accessor :action
|
478
|
-
|
479
|
-
|
480
|
-
private
|
481
|
-
def action_presence
|
482
|
-
errors.add(:action, 'is invalid') if action.blank?
|
483
|
-
end
|
484
|
-
end
|
485
|
-
instance = klass.new
|
486
|
-
instance.valid?
|
487
|
-
assert_equal 'is invalid', instance.errors.on(:action)
|
488
|
-
|
489
|
-
instance.action = 'walk'
|
490
|
-
instance.valid?
|
491
|
-
assert_nil instance.errors.on(:action)
|
492
|
-
end
|
493
|
-
|
494
|
-
test 'validate on create callback for new record' do
|
495
|
-
klass = Class.new do
|
496
|
-
include Validatable
|
497
|
-
attr_accessor :action
|
498
|
-
validate_on_create :action_presence
|
499
|
-
|
500
|
-
def new?
|
501
|
-
true
|
502
|
-
end
|
503
|
-
|
504
|
-
private
|
505
|
-
def action_presence
|
506
|
-
errors.add(:action, 'is invalid') if action.blank?
|
507
|
-
end
|
508
|
-
end
|
509
|
-
instance = klass.new
|
510
|
-
instance.valid?
|
511
|
-
assert_equal 'is invalid', instance.errors.on(:action)
|
512
|
-
|
513
|
-
instance.action = 'walk'
|
514
|
-
instance.valid?
|
515
|
-
assert_nil instance.errors.on(:action)
|
516
|
-
end
|
478
|
+
before_validation :action_presence
|
517
479
|
|
518
|
-
test 'validate on create callback for not new record' do
|
519
|
-
klass = Class.new do
|
520
|
-
include Validatable
|
521
|
-
attr_accessor :action
|
522
|
-
validate_on_create :action_presence
|
523
|
-
|
524
|
-
def new?
|
525
|
-
false
|
526
|
-
end
|
527
|
-
|
528
480
|
private
|
529
481
|
def action_presence
|
530
482
|
errors.add(:action, 'is invalid') if action.blank?
|
@@ -532,58 +484,11 @@ functional_tests do
|
|
532
484
|
end
|
533
485
|
instance = klass.new
|
534
486
|
instance.valid?
|
535
|
-
|
536
|
-
|
537
|
-
instance.action = 'walk'
|
538
|
-
instance.valid?
|
539
|
-
assert_nil instance.errors.on(:action)
|
540
|
-
end
|
487
|
+
assert_equal 'is invalid', instance.errors.on(:action)
|
541
488
|
|
542
|
-
test 'validate on update callback for new record' do
|
543
|
-
klass = Class.new do
|
544
|
-
include Validatable
|
545
|
-
attr_accessor :action
|
546
|
-
validate_on_update :action_presence
|
547
|
-
|
548
|
-
def new?
|
549
|
-
true
|
550
|
-
end
|
551
|
-
|
552
|
-
private
|
553
|
-
def action_presence
|
554
|
-
errors.add(:action, 'is invalid') if action.blank?
|
555
|
-
end
|
556
|
-
end
|
557
|
-
instance = klass.new
|
558
|
-
instance.valid?
|
559
|
-
assert_nil instance.errors.on(:action)
|
560
|
-
|
561
489
|
instance.action = 'walk'
|
562
490
|
instance.valid?
|
563
491
|
assert_nil instance.errors.on(:action)
|
564
492
|
end
|
565
493
|
|
566
|
-
test 'validate on update callback for not new record' do
|
567
|
-
klass = Class.new do
|
568
|
-
include Validatable
|
569
|
-
attr_accessor :action
|
570
|
-
validate_on_update :action_presence
|
571
|
-
|
572
|
-
def new?
|
573
|
-
false
|
574
|
-
end
|
575
|
-
|
576
|
-
private
|
577
|
-
def action_presence
|
578
|
-
errors.add(:action, 'is invalid') if action.blank?
|
579
|
-
end
|
580
|
-
end
|
581
|
-
instance = klass.new
|
582
|
-
instance.valid?
|
583
|
-
assert_equal 'is invalid', instance.errors.on(:action)
|
584
|
-
|
585
|
-
instance.action = 'walk'
|
586
|
-
instance.valid?
|
587
|
-
assert_nil instance.errors.on(:action)
|
588
|
-
end
|
589
494
|
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:
|
4
|
+
version: 2.0.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:
|
14
|
+
date: 2010-01-09 00:00:00 -05:00
|
15
15
|
default_executable:
|
16
16
|
dependencies: []
|
17
17
|
|