Created
January 12, 2010 14:13
-
-
Save stephencelis/275230 to your computer and use it in GitHub Desktop.
validates :something, validator: fallback
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
# Adds a fallback <tt>:is</tt> option to +validates+. | |
# | |
# # validates :preferences, class: Hash | |
# class ClassValidator < ActiveModel::EachValidator | |
# def validate_each(record, attribute, value) | |
# record[attribute].is_a?(options[:is]) || | |
# record.errors.add attribute, options[:message] || | |
# record.errors.generate_message(attribute, :class) | |
# end | |
# end | |
ActiveModel::Validations::ClassMethods.module_eval do | |
protected | |
def _parse_validates_options_with_fallback(options) | |
_parse_validates_options_without_fallback(options) || { is: options } | |
end | |
alias_method_chain :_parse_validates_options, :fallback | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment