4

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...

PlankTon
  • 12,443
  • 16
  • 84
  • 153
  • Why don't you simply create a new form builder? – apneadiving May 03 '12 at 23:17
  • Hi Apnea - thanks for the response. The "some extra stuff" involves injecting honeypots (defined in the model) into the form. It's pretty general functionality & I'd like it to be used across form builders, which I'm guessing won't be possible if I specify a specific builder (For example, if I specify 'honeypot_fields_for', that won't be called from a formtastic/simple_form form builders, etc). – PlankTon May 04 '12 at 00:53
  • By "New form builder" I assume @apnediving is talking about http://www.likeawritingdesk.com/posts/very-custom-form-builders-in-rails - in that sense you're giving the people the choice to use your form builder, whereas by overriding that method directly it's impossible for people *not* to use it – Gareth Jun 14 '12 at 21:19

1 Answers1

3

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.

sbeam
  • 4,622
  • 6
  • 33
  • 43