sexy_validations 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/sexy_validations.rb +12 -5
- data/sexy_validations.gemspec +1 -1
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.4.
|
1
|
+
0.4.1
|
data/lib/sexy_validations.rb
CHANGED
@@ -39,18 +39,21 @@ module SexyValidations
|
|
39
39
|
end
|
40
40
|
|
41
41
|
def validates(attribute = nil, validations = nil, &block)
|
42
|
-
|
43
|
-
condition = nil
|
42
|
+
condition = nil
|
44
43
|
|
44
|
+
if validations
|
45
45
|
if validations[:if]
|
46
46
|
condition = validations.delete(:if)
|
47
47
|
end
|
48
48
|
|
49
49
|
if validations[:unless]
|
50
|
+
raise ArgumentError, "if and unless condition given" if condition
|
50
51
|
ref = validations.delete(:unless)
|
51
52
|
condition = lambda { |model| !ref.call(model) }
|
52
53
|
end
|
54
|
+
end
|
53
55
|
|
56
|
+
unless validations.blank?
|
54
57
|
validations.each_pair do |validator, options|
|
55
58
|
klass = load_validator(validator)
|
56
59
|
self.validations << {
|
@@ -64,10 +67,13 @@ module SexyValidations
|
|
64
67
|
if attribute
|
65
68
|
self.validations << {
|
66
69
|
:method => "#{attribute}_validation",
|
70
|
+
:condition => condition,
|
67
71
|
}
|
68
72
|
else
|
73
|
+
raise ArgumentError, "at least a block has to be given" unless block_given?
|
69
74
|
self.validations << {
|
70
75
|
:block => block,
|
76
|
+
:condition => condition,
|
71
77
|
}
|
72
78
|
end
|
73
79
|
end
|
@@ -78,11 +84,12 @@ module SexyValidations
|
|
78
84
|
def validate!
|
79
85
|
errors.clear
|
80
86
|
validations.each do |validation|
|
87
|
+
if validation[:condition]
|
88
|
+
next unless validation[:condition].call(self)
|
89
|
+
end
|
90
|
+
|
81
91
|
case
|
82
92
|
when validation[:validator]
|
83
|
-
if validation[:condition]
|
84
|
-
next unless validation[:condition].call(self)
|
85
|
-
end
|
86
93
|
if errors[validation[:attribute]].empty?
|
87
94
|
validation[:validator].validate(self, validation[:attribute], send(validation[:attribute]), validation[:options])
|
88
95
|
end
|
data/sexy_validations.gemspec
CHANGED