Created
September 20, 2010 16:36
-
-
Save kryzhovnik/588180 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 InclusionWithBlockValidator < ActiveModel::EachValidator | |
def validate_each(object, attribute, value) | |
begin | |
enum = options[:in].call | |
raise unless enum.kind_of?(Enumerable) | |
rescue | |
raise "value of :in option at inclusion_with_block validator " + | |
"must be a block those return enumerable object" | |
end | |
unless enum.include?(value) | |
object.errors[attribute] << (options[:message] || | |
I18n.t(:'activerecord.errors.messages.inclusion')) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment