Created
February 2, 2013 18:09
-
-
Save wpeterson/4698635 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CoercedValueValidator < ActiveModel::Validator | |
def validate(record) | |
coercible_attrs = record.keys.select { |k,v| [Integer, Float, Date, Time].include?(v.type) }.map(&:first) | |
coercible_attrs.each do |attr| | |
record.errors.add(attr, 'is invalid') if record.send("#{attr}_before_type_cast").present? && record.send(attr).nil? | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment