whyvalidationssuckin96 1.4.0 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.
|
1
|
+
1.5.0
|
@@ -86,7 +86,7 @@ module WhyValidationsSuckIn96
|
|
86
86
|
def valid_with_lifecycle_checking?
|
87
87
|
validations_for_current_lifecycle.collect do |validation|
|
88
88
|
# Checks manually because a 'nil' return is considered a skipped validation, not a failed one.
|
89
|
-
(validation.validates? == false)
|
89
|
+
!(validation.validates? == false)
|
90
90
|
end.all?
|
91
91
|
end
|
92
92
|
|
@@ -14,15 +14,36 @@ module WhyValidationsSuckIn96
|
|
14
14
|
# Instance methods added to any class or module that mixes in ValidationSupport
|
15
15
|
module InstanceMethods
|
16
16
|
|
17
|
+
def self.included(klass)
|
18
|
+
klass.module_eval do
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
17
23
|
# Is this object invalid?
|
18
24
|
# @return [true, false]
|
19
25
|
def invalid?
|
20
26
|
!valid?
|
21
27
|
end
|
22
28
|
|
29
|
+
def valid?
|
30
|
+
self.class.run_with_generic_callbacks? ? valid_with_generic_callbacks? : valid_without_generic_callbacks?
|
31
|
+
end
|
32
|
+
|
23
33
|
# Is this object valid?
|
34
|
+
# Also runs any callbacks if the class has callback support as defined by the
|
35
|
+
# instance method run_callbacks being present.
|
24
36
|
# @return [true, false]
|
25
|
-
def
|
37
|
+
def valid_with_generic_callbacks?
|
38
|
+
run_callbacks(:before_validation)
|
39
|
+
valid_without_generic_callbacks?
|
40
|
+
ensure
|
41
|
+
run_callbacks(:after_validation)
|
42
|
+
end
|
43
|
+
|
44
|
+
# Is this object valid?
|
45
|
+
# @return [true, false]
|
46
|
+
def valid_without_generic_callbacks?
|
26
47
|
all_validations.collect do |validation|
|
27
48
|
# Checks manually because a 'nil' return is considered a skipped validation, not a failed one.
|
28
49
|
(validation.validates? == false) ? false : true
|
@@ -54,6 +75,17 @@ module WhyValidationsSuckIn96
|
|
54
75
|
|
55
76
|
module ClassMethods
|
56
77
|
|
78
|
+
# If the class or module has a public 'run_callbacks' instance method, we run validations and fire
|
79
|
+
# appropriate callbacks
|
80
|
+
# @return [true, false]
|
81
|
+
def run_with_generic_callbacks?
|
82
|
+
if defined?(@run_with_generic_callbacks)
|
83
|
+
@run_with_generic_callbacks
|
84
|
+
else
|
85
|
+
@run_with_generic_callbacks = public_instance_methods.include?('run_callbacks')
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
57
89
|
# An array of arrays, the first element of each being the validation subclass that will be instantiated
|
58
90
|
# when validation is performed, the last element being the options the validation will be instantiated
|
59
91
|
# with.
|
@@ -1,6 +1,41 @@
|
|
1
1
|
require 'teststrap'
|
2
2
|
|
3
3
|
context "validation_support" do
|
4
|
+
|
5
|
+
context "when mixed into a class supporting callbacks" do
|
6
|
+
setup do
|
7
|
+
Class.new do
|
8
|
+
attr_reader :callbacks_run
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
@callbacks_run = []
|
12
|
+
end
|
13
|
+
|
14
|
+
def run_callbacks(callback_name)
|
15
|
+
@callbacks_run << callback_name
|
16
|
+
end
|
17
|
+
|
18
|
+
include WhyValidationsSuckIn96::ValidationSupport
|
19
|
+
setup_validations do
|
20
|
+
validate(:foo) { pass }
|
21
|
+
end
|
22
|
+
end.new
|
23
|
+
end
|
24
|
+
|
25
|
+
should "run the before_validation callback when calling valid?" do
|
26
|
+
topic.callbacks_run.clear
|
27
|
+
topic.valid?
|
28
|
+
topic.callbacks_run
|
29
|
+
end.includes(:before_validation)
|
30
|
+
|
31
|
+
should "run the after_validation callback when calling valid?" do
|
32
|
+
topic.callbacks_run.clear
|
33
|
+
topic.valid?
|
34
|
+
topic.callbacks_run
|
35
|
+
end.includes(:after_validation)
|
36
|
+
|
37
|
+
end # when mixed into a class supporting callbacks
|
38
|
+
|
4
39
|
context "when mixed into a class" do
|
5
40
|
setup do
|
6
41
|
Class.new { include WhyValidationsSuckIn96::ValidationSupport }
|
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{whyvalidationssuckin96}
|
8
|
-
s.version = "1.
|
8
|
+
s.version = "1.5.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["gabrielg", "douglasmeyer"]
|