Created
August 20, 2010 21:08
-
-
Save lapluviosilla/541195 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
ActiveRecord::Base.class_eval do | |
class << self | |
# Ugly bit of metaprogramming to remove an existing validation | |
# @param type of validation. ex. :validates_uniqueness_of | |
# @param name of attribute to be validated. ex. :email | |
def remove_validation type, name | |
@validate_callbacks.reject! do |back| | |
proc = back.method | |
if proc.is_a?(Proc) | |
# Returns the name of method the proc was declared in | |
method = eval("caller[0] =~ /`([^']*)'/ and $1", proc.binding).to_sym rescue nil | |
attrs = eval("attrs", proc.binding) rescue nil | |
if method == type | |
if attrs.include?(name) && attrs.length > 1 | |
eval("attrs.delete(#{name})", proc.binding) | |
false | |
elsif attrs.include?(name) | |
true | |
else | |
false | |
end | |
end | |
else | |
false | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment