Ruby/Rails: alias_method practices
alias-method, ruby, ruby-on-rails, rubygems
Solution
this seems like exactly the situation that `alias_method_chain` is meant for (although I don't know offhand if it will work on a Module - have only used it on AR::Base)
You'd just do
module ActionView::Helpers::FormHelper
include ActionView::Helpers::FormTagHelper
alias_method_chain :fields_for, :honeypot
def fields_for_with_honeypot(<my arguments>)
# Some extra stuff
# ...
output.safe_concat fields_for_without_honeypot(<my other arguments>)
end
end
interesting idea to do this to `fields_for`, but it should work.
There is a minor controversy around a_m_c you should be aware of - this post sums it up well http://erniemiller.org/2011/02/03/when-to-use-alias_method_chain/
In this case, I don't think you can use `super` because you want to monkey-patch form_for without modifying the calling code/views.
Problem
I'm trying to override Rails' "fields_for" method, which I'm currently doing as follows: ``` module ActionView::Helpers::FormHelper include ActionView::Helpers::FormTagHelper alias_method :original_fields_for, :fields_for def fields_for(<my arguments>) # Some extra stuff # ... output.safe_concat original_fields_for(<my other arguments>) end end ``` The functionality works just fine, but I'm starting to suspect that my use of alias_method isn't the most elegant. Most especially, if I were to package this functionality into a gem, and there were another gem that overrode fields_for, am I write in thinking either my new fields_for OR the alternate fields_for would be skipped? Assuming so, what's the correct way to go about slapping in some extra functionality to an existing rails method? Cheers...