I am having method called link_to_admin then I have aliased another method called simple_link_to
def link_to_admin(name,url,options={})
# My stuff here
link_to(name,url,options)
end
alias_method :simple_link_to, :link_to_admin
Here I am facing one problem if I call link_to_admin, I want print the value into <li> tag
Ex.
def link_to_admin(name,url,options={})
# My stuff here
menu = ""
menu << "<li> #{link_to(name,url,options)}</li>"
menu.html_safe
end
And if I call simple_link_to no need of <li> tag. So currently I am passing one options like li_required then am checking condition in my method. This is working perfectly, I know this is correct way to do it.
def link_to_admin(name,url,options={})
menu = ""
menu << options[:li_required] ? "<li> #{link_to(name,url,options)}</li>" : link_to(name,url,options)
menu.html_safe
end
But,Before I was trying to find the calling method like simple_method or link_to_admin, so I tried with:
1.__method__
2.caller[0]=~/`(.*?)'/
This is not working as expected.
I am calling simple_method or link_to_admin methods in layout so __method__ is always returning link_to_admin only.
But caller[0]=~/(.*?)'/if am calling fromlayoutmeans, it's returning layout path and if I am calling from any otherhelperorclass` means it's returning current method.
Any other way is there to check method name.
I know this is not good question, could any one please tell me any other way.
Note:- I have created alias_method for naming conversion only. Other than adding <li> tag I am not doing any thing.
Not only here am having this doubt from long back. I know I can define one more method and I can do it.
Leave my scenario, generally I wants to know this answer.
Using __callee__ instead of __method__ will get the name of the alias being run.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With