lib/dynamoid/validations.rb in dynamoid-edge-1.1.1 vs lib/dynamoid/validations.rb in dynamoid-edge-1.1.2

- old
+ new

@@ -30,7 +30,34 @@ # @since 0.2.0 def save! raise Dynamoid::Errors::DocumentNotValid.new(self) unless valid? save(:validate => false) end + + module ClassMethods + + # Override validates_presence_of to handle false values as present. + # + # @since 1.1.1 + def validates_presence_of(*attr_names) + validates_with PresenceValidator, _merge_attributes(attr_names) + end + + private + + # Validates that the specified attributes are present (false or not blank). + class PresenceValidator < ActiveModel::EachValidator + # Validate the record for the record and value. + def validate_each(record, attr_name, value) + record.errors.add(attr_name, :blank, options) if not_present?(value) + end + + private + + # Check whether a value is not present. + def not_present?(value) + value.blank? && value != false + end + end + end end end