Skip to content

Instantly share code, notes, and snippets.

@kryzhovnik
Created September 20, 2010 16:36
Show Gist options
  • Save kryzhovnik/588180 to your computer and use it in GitHub Desktop.
Save kryzhovnik/588180 to your computer and use it in GitHub Desktop.
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