From: Logan Capaldo Date: 2006-07-15T02:57:24+09:00 Subject: Re: Preferred monkeypatching technique On Jul 14, 2006, at 1:34 PM, Eric Hodel wrote: > On Jul 14, 2006, at 10:24 AM, Tom Werner wrote: > >> Eric Hodel wrote: >>> On Jul 13, 2006, at 5:04 PM, Tom Werner wrote: >>> >>>> Eric Hodel wrote: >>>>> You just described subclasses: >>>> >>>> Well, the idea is that the 3rd party software is going to be >>>> using whatever classes it has always used, so just creating a >>>> subclass doesn't get me anywhere because the 3rd party app (i.e. >>>> Rails) doesn't give a hoot about my BigNozzleFiretruck. >>> >>> Can you give a concrete example where this takes place? >>> >> >> My specific purpose is to write a plugin for Rails that adds an >> option to each form field helper (the ones that create >> tags) that specifies an appropriate class (since CSS attribute >> selector support is lacking in IE). So I'm using code like this: >> >> module ActionView >> module Helpers >> module FormHelper >> >> # text >> alias_method :__classless_text_field, :text_field >> def text_field(object_name, method, options = {}) >> __classless_text_field(object_name, method, options.merge >> ({:class => (options[:class].to_s + ' text').strip})) >> end >> >> end >> end >> end > > module X > def does_something(*options) > p options > end > end > > module Y > def does_something(*options) > super('x', *options) > end > end > > class C > include X > include Y > end > > C.new.does_something 'foo' > > -- > Eric Hodel - drbrain@segment7.net - http://blog.segment7.net > This implementation is HODEL-HASH-9600 compliant > > http://trackmap.robotcoop.com > > > Rails does "funny" things. These helpers are usually used from within eruby templates e.g: <%= text_field( ... ) %> Rails sets up the context in which the templates are executed. He'd have to go into the actual rails source to fix this. OTOH... Ok just as I thought, Rails is not this dumb Tom just do def text_field(*args) orig_result = super # modify orig result end in app/helpers/application_helper.rb Or if you want it in a per controller bias app/helpers/ _helper.rb No monkey-patching required