Refactor and expand Paper validations

This commit is contained in:
Lars Henrik Mai 2015-09-27 13:51:13 +02:00
parent d6571e4042
commit ebca521e2f
6 changed files with 90 additions and 9 deletions

View file

@ -0,0 +1,12 @@
class ParseableDateValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
raw_value = record.read_attribute_before_type_cast(attribute)
return nil if raw_value.nil?
Date.parse(raw_value)
nil
rescue ArgumentError => e
record.errors[attribute] << (options[:message] || I18n.t("errors.messages.unparseable_date"))
end
end