client_side_validations 3.0.1 → 3.0.2
Sign up to get free protection for your applications and to get access to all the features.
@@ -22,7 +22,7 @@ module ClientSideValidations::ActiveModel
|
|
22
22
|
validator_hash = attr[1].inject({}) do |kind_hash, validator|
|
23
23
|
client_side_hash = validator.client_side_hash(self, attr[0])
|
24
24
|
# Yeah yeah, #new_record? is not part of ActiveModel :p
|
25
|
-
if (
|
25
|
+
if (can_use_for_client_side_validation?(client_side_hash, validator))
|
26
26
|
kind_hash.merge!(validator.kind => client_side_hash.except(:on))
|
27
27
|
else
|
28
28
|
kind_hash.merge!({})
|
@@ -32,6 +32,12 @@ module ClientSideValidations::ActiveModel
|
|
32
32
|
attr_hash.merge!(attr[0] => validator_hash)
|
33
33
|
end.delete_if { |key, value| value.blank? }
|
34
34
|
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def can_use_for_client_side_validation?(client_side_hash, validator)
|
39
|
+
((self.respond_to?(:new_record?) && client_side_hash[:on] == (self.new_record? ? :create : :update)) || client_side_hash[:on].nil?) && !validator.options.key?(:if) && !validator.options.key?(:unless)
|
40
|
+
end
|
35
41
|
end
|
36
42
|
end
|
37
43
|
|
@@ -137,5 +137,15 @@ class ActiveModel::ValidationsTest < ClientSideValidations::ActiveModelTestBase
|
|
137
137
|
expected_hash = {}
|
138
138
|
assert_equal expected_hash, person.client_side_validation_hash
|
139
139
|
end
|
140
|
+
|
141
|
+
def test_validators_with_if_or_unless_are_ignored
|
142
|
+
person = new_person do |p|
|
143
|
+
p.validates_presence_of :first_name, :if => Proc.new { |p| true }
|
144
|
+
p.validates_presence_of :last_name, :unless => Proc.new { |p| true }
|
145
|
+
end
|
146
|
+
|
147
|
+
expected_hash = {}
|
148
|
+
assert_equal expected_hash, person.client_side_validation_hash
|
149
|
+
end
|
140
150
|
end
|
141
151
|
|