Skip to content

Instantly share code, notes, and snippets.

@bltavares
Created September 21, 2011 03:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bltavares/1231138 to your computer and use it in GitHub Desktop.
Save bltavares/1231138 to your computer and use it in GitHub Desktop.
Custom ActiveModel Validator
#coding: utf-8
module Blacklist
class Blacklisting < ActiveModel::EachValidator
def validate_each(record, attr, value)
message = options[:message] || "Ops, esse nome já foi escolhido"
against = options[:against] || []
record.errors.add(attr, message) if against.include? value
end
end
module Helpers
def blacklist(*attr_names)
validates_with Blacklisting, _merge_attributes(attr_names)
end
end
end
module ActiveRecord
class Base
extend Blacklist::Helpers
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment