jnunemaker-validatable 1.7.0 → 1.7.1
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION.yml +2 -1
- data/lib/validatable.rb +20 -18
- data/lib/validatable_instance_methods.rb +12 -2
- data/test/functional/test_validatable.rb +116 -0
- metadata +2 -2
data/VERSION.yml
CHANGED
data/lib/validatable.rb
CHANGED
@@ -2,21 +2,23 @@ require 'forwardable'
|
|
2
2
|
require 'rubygems'
|
3
3
|
require 'activesupport'
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
require File.
|
8
|
-
require File.
|
9
|
-
require File.
|
10
|
-
require File.
|
11
|
-
require File.
|
12
|
-
require File.
|
13
|
-
require File.
|
14
|
-
require File.
|
15
|
-
require File.
|
16
|
-
require File.
|
17
|
-
require File.
|
18
|
-
require File.
|
19
|
-
require File.
|
20
|
-
require File.
|
21
|
-
require File.
|
22
|
-
require File.
|
5
|
+
dir = File.expand_path(File.dirname(__FILE__))
|
6
|
+
|
7
|
+
require File.join(dir, 'object_extension')
|
8
|
+
require File.join(dir, 'errors')
|
9
|
+
require File.join(dir, 'validatable_class_methods')
|
10
|
+
require File.join(dir, 'macros')
|
11
|
+
require File.join(dir, 'validatable_instance_methods')
|
12
|
+
require File.join(dir, 'included_validation')
|
13
|
+
require File.join(dir, 'child_validation')
|
14
|
+
require File.join(dir, 'understandable')
|
15
|
+
require File.join(dir, 'requireable')
|
16
|
+
require File.join(dir, 'validations/validation_base')
|
17
|
+
require File.join(dir, 'validations/validates_format_of')
|
18
|
+
require File.join(dir, 'validations/validates_presence_of')
|
19
|
+
require File.join(dir, 'validations/validates_acceptance_of')
|
20
|
+
require File.join(dir, 'validations/validates_confirmation_of')
|
21
|
+
require File.join(dir, 'validations/validates_length_of')
|
22
|
+
require File.join(dir, 'validations/validates_true_for')
|
23
|
+
require File.join(dir, 'validations/validates_numericality_of')
|
24
|
+
require File.join(dir, 'validations/validates_each')
|
@@ -2,6 +2,10 @@ module Validatable
|
|
2
2
|
def self.included(klass) #:nodoc:
|
3
3
|
klass.extend Validatable::ClassMethods
|
4
4
|
klass.extend Validatable::Macros
|
5
|
+
klass.class_eval do
|
6
|
+
include ActiveSupport::Callbacks
|
7
|
+
define_callbacks :validate, :validate_on_create, :validate_on_update
|
8
|
+
end
|
5
9
|
end
|
6
10
|
|
7
11
|
# call-seq: valid?
|
@@ -21,8 +25,14 @@ module Validatable
|
|
21
25
|
def valid_for_group?(group) #:nodoc:
|
22
26
|
run_before_validations
|
23
27
|
errors.clear
|
28
|
+
run_callbacks(:validate)
|
29
|
+
|
30
|
+
if respond_to?(:new?)
|
31
|
+
new? ? run_callbacks(:validate_on_create) : run_callbacks(:validate_on_update)
|
32
|
+
end
|
33
|
+
|
24
34
|
self.class.validate_children(self, group)
|
25
|
-
self.
|
35
|
+
self.validate_group(group)
|
26
36
|
errors.empty?
|
27
37
|
end
|
28
38
|
|
@@ -61,7 +71,7 @@ module Validatable
|
|
61
71
|
@times_validated_hash ||= {}
|
62
72
|
end
|
63
73
|
|
64
|
-
def
|
74
|
+
def validate_group(group) #:nodoc:
|
65
75
|
validation_levels.each do |level|
|
66
76
|
validations_for_level_and_group(level, group).each do |validation|
|
67
77
|
run_validation(validation) if validation.should_validate?(self)
|
@@ -434,4 +434,120 @@ functional_tests do
|
|
434
434
|
instance.validate_only("presence_of/name")
|
435
435
|
instance.errors.on(:address)
|
436
436
|
end
|
437
|
+
|
438
|
+
test 'validate callback' do
|
439
|
+
klass = Class.new do
|
440
|
+
include Validatable
|
441
|
+
attr_accessor :action
|
442
|
+
validate :action_presence
|
443
|
+
|
444
|
+
private
|
445
|
+
def action_presence
|
446
|
+
errors.add(:action, 'is invalid') if action.blank?
|
447
|
+
end
|
448
|
+
end
|
449
|
+
instance = klass.new
|
450
|
+
instance.valid?
|
451
|
+
assert_equal 'is invalid', instance.errors.on(:action)
|
452
|
+
|
453
|
+
instance.action = 'walk'
|
454
|
+
instance.valid?
|
455
|
+
assert_nil instance.errors.on(:action)
|
456
|
+
end
|
457
|
+
|
458
|
+
test 'validate on create callback for new record' do
|
459
|
+
klass = Class.new do
|
460
|
+
include Validatable
|
461
|
+
attr_accessor :action
|
462
|
+
validate_on_create :action_presence
|
463
|
+
|
464
|
+
def new?
|
465
|
+
true
|
466
|
+
end
|
467
|
+
|
468
|
+
private
|
469
|
+
def action_presence
|
470
|
+
errors.add(:action, 'is invalid') if action.blank?
|
471
|
+
end
|
472
|
+
end
|
473
|
+
instance = klass.new
|
474
|
+
instance.valid?
|
475
|
+
assert_equal 'is invalid', instance.errors.on(:action)
|
476
|
+
|
477
|
+
instance.action = 'walk'
|
478
|
+
instance.valid?
|
479
|
+
assert_nil instance.errors.on(:action)
|
480
|
+
end
|
481
|
+
|
482
|
+
test 'validate on create callback for not new record' do
|
483
|
+
klass = Class.new do
|
484
|
+
include Validatable
|
485
|
+
attr_accessor :action
|
486
|
+
validate_on_create :action_presence
|
487
|
+
|
488
|
+
def new?
|
489
|
+
false
|
490
|
+
end
|
491
|
+
|
492
|
+
private
|
493
|
+
def action_presence
|
494
|
+
errors.add(:action, 'is invalid') if action.blank?
|
495
|
+
end
|
496
|
+
end
|
497
|
+
instance = klass.new
|
498
|
+
instance.valid?
|
499
|
+
assert_nil instance.errors.on(:action)
|
500
|
+
|
501
|
+
instance.action = 'walk'
|
502
|
+
instance.valid?
|
503
|
+
assert_nil instance.errors.on(:action)
|
504
|
+
end
|
505
|
+
|
506
|
+
test 'validate on update callback for new record' do
|
507
|
+
klass = Class.new do
|
508
|
+
include Validatable
|
509
|
+
attr_accessor :action
|
510
|
+
validate_on_update :action_presence
|
511
|
+
|
512
|
+
def new?
|
513
|
+
true
|
514
|
+
end
|
515
|
+
|
516
|
+
private
|
517
|
+
def action_presence
|
518
|
+
errors.add(:action, 'is invalid') if action.blank?
|
519
|
+
end
|
520
|
+
end
|
521
|
+
instance = klass.new
|
522
|
+
instance.valid?
|
523
|
+
assert_nil instance.errors.on(:action)
|
524
|
+
|
525
|
+
instance.action = 'walk'
|
526
|
+
instance.valid?
|
527
|
+
assert_nil instance.errors.on(:action)
|
528
|
+
end
|
529
|
+
|
530
|
+
test 'validate on update callback for not new record' do
|
531
|
+
klass = Class.new do
|
532
|
+
include Validatable
|
533
|
+
attr_accessor :action
|
534
|
+
validate_on_update :action_presence
|
535
|
+
|
536
|
+
def new?
|
537
|
+
false
|
538
|
+
end
|
539
|
+
|
540
|
+
private
|
541
|
+
def action_presence
|
542
|
+
errors.add(:action, 'is invalid') if action.blank?
|
543
|
+
end
|
544
|
+
end
|
545
|
+
instance = klass.new
|
546
|
+
instance.valid?
|
547
|
+
assert_equal 'is invalid', instance.errors.on(:action)
|
548
|
+
|
549
|
+
instance.action = 'walk'
|
550
|
+
instance.valid?
|
551
|
+
assert_nil instance.errors.on(:action)
|
552
|
+
end
|
437
553
|
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.
|
4
|
+
version: 1.7.1
|
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-05
|
13
|
+
date: 2009-07-05 00:00:00 -07:00
|
14
14
|
default_executable:
|
15
15
|
dependencies: []
|
16
16
|
|