Skip to content

Instantly share code, notes, and snippets.

@stephencelis
Created January 12, 2010 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephencelis/275230 to your computer and use it in GitHub Desktop.
Save stephencelis/275230 to your computer and use it in GitHub Desktop.
validates :something, validator: fallback
# 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