lib/mongoid/validations/presence.rb in mongoid-3.0.4 vs lib/mongoid/validations/presence.rb in mongoid-3.0.5
- old
+ new
@@ -31,18 +31,18 @@
value.each_pair do |_locale, _value|
document.errors.add(
attribute,
:blank_in_locale,
options.merge(location: _locale)
- ) if _value.blank?
+ ) if not_present?(_value)
end
elsif document.relations.has_key?(attribute.to_s)
if relation_or_fk_missing?(document, attribute, value)
document.errors.add(attribute, :blank, options)
end
else
- document.errors.add(attribute, :blank, options) if value.blank?
+ document.errors.add(attribute, :blank, options) if not_present?(value)
end
end
private
@@ -62,9 +62,25 @@
# @since 3.0.0
def relation_or_fk_missing?(doc, attr, value)
return true if value.blank? && doc.send(attr).blank?
metadata = doc.relations[attr.to_s]
metadata.stores_foreign_key? && doc.send(metadata.foreign_key).blank?
+ end
+
+ # For guarding against false values.
+ #
+ # @api private
+ #
+ # @example Is the value not present?
+ # validator.not_present?(value)
+ #
+ # @param [ Object ] value The value.
+ #
+ # @return [ true, false ] If the value is not present.
+ #
+ # @since 3.0.5
+ def not_present?(value)
+ value.blank? && value != false
end
end
end
end