jnunemaker-validatable 1.7.0 → 1.7.1

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.
@@ -1,3 +1,4 @@
1
+ ---
1
2
  :major: 1
2
3
  :minor: 7
3
- :patch: 0
4
+ :patch: 1
@@ -2,21 +2,23 @@ require 'forwardable'
2
2
  require 'rubygems'
3
3
  require 'activesupport'
4
4
 
5
- require File.expand_path(File.dirname(__FILE__) + '/object_extension')
6
- require File.expand_path(File.dirname(__FILE__) + '/errors')
7
- require File.expand_path(File.dirname(__FILE__) + '/validatable_class_methods')
8
- require File.expand_path(File.dirname(__FILE__) + '/macros')
9
- require File.expand_path(File.dirname(__FILE__) + '/validatable_instance_methods')
10
- require File.expand_path(File.dirname(__FILE__) + '/included_validation')
11
- require File.expand_path(File.dirname(__FILE__) + '/child_validation')
12
- require File.expand_path(File.dirname(__FILE__) + '/understandable')
13
- require File.expand_path(File.dirname(__FILE__) + '/requireable')
14
- require File.expand_path(File.dirname(__FILE__) + '/validations/validation_base')
15
- require File.expand_path(File.dirname(__FILE__) + '/validations/validates_format_of')
16
- require File.expand_path(File.dirname(__FILE__) + '/validations/validates_presence_of')
17
- require File.expand_path(File.dirname(__FILE__) + '/validations/validates_acceptance_of')
18
- require File.expand_path(File.dirname(__FILE__) + '/validations/validates_confirmation_of')
19
- require File.expand_path(File.dirname(__FILE__) + '/validations/validates_length_of')
20
- require File.expand_path(File.dirname(__FILE__) + '/validations/validates_true_for')
21
- require File.expand_path(File.dirname(__FILE__) + '/validations/validates_numericality_of')
22
- require File.expand_path(File.dirname(__FILE__) + '/validations/validates_each')
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.validate(group)
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 validate(group) #:nodoc:
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.0
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-28 00:00:00 -07:00
13
+ date: 2009-07-05 00:00:00 -07:00
14
14
  default_executable:
15
15
  dependencies: []
16
16