How to override / add method_missing while maintaining search functionality?
I want to add some behavior to my model when method_missing
called, but I know that searchlogic is already using this for its named search scopes.
What is the correct way to extend the functionality method_missing
? Is there a way to react to a trigger method_missing
? Much how to bind functions to events in javascript?
source to share
when you say you want something to happen when a method is missing, do you mean every time or only when the search logic hasn't found anything? The first is pretty simple:
def method_missing(name, *)
if name =~ /foo/i
# call your custom method
# or for example define_method
else
# you do not want to handle the
# method, so fall back to standard behavior
super
end
end
Where you replace with a name =~ /foo/i
condition that checks if you want to handle the missing method or not. The latter is not easy. I can’t answer it because I don’t know the search logic, but you will need to check if the search logic was found something or not before calling your own method.
source to share