In a plugin I am writing I am using alias to override one of the default Rails validators like so:
# Alias the original validator so it's still available under a different name
alias original_validates_uniqueness_of :validates_uniqueness_of unless method_defined?(:original_validates_uniqueness_of)
# Then alias the custom validator under the original name
alias validates_uniqueness_of :custom_validates_uniqueness_of
This all works pretty well. When “validates_uniqueness_of” is defined on an attribute in a AR model, it will use my “custom_validates_uniqueness_of” method instead. Validations are running as expected.
However, when I call:
SomeARclass.respond_to?(:validates_uniqueness_of)
..it will return false. This behavior will mess with several popular plugins.
My question:
Why is respond_to returning “false”? Is this behavior the result of alliasing? How can adjust my custom validator to make it return true?
Thank you for your help.
Erwin
Your sample, on the surface, looks like it should work. I expect you’ve found that, inside of the class body for
class SomeARclass, you actually can callvalidates_uniqueness_of.However, I expect that
scoped_validates_uniqueness_ofis a private method. That’s why you have been unable to send that message toSomeARclassfrom outside of its class body.When you alias a private method with a new name, such as aliasing the private method
validates_scoped_uniqueness_ofwith the new namevalidates_uniqueness_of, then that new method is also private. I expect that’s what happened here.The documentation for
#respond_to?reads: