Hi, Is it possible to extend a plugin method? In a class that uses a plugin which already defines, say, a before_create method, is it possible to extend this before_create so the code in the plugin''s method is also invoked ? Something in effect like calling super in an inherited class'' overwriting method. I suppose this is either easy with some less-known syntax, which I haven''t found, or this is impossible currently (I know it''s not a biggie to copy over the code from the plugin method, but...) Thanks! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Philip Tsai wrote:> Hi, > > Is it possible to extend a plugin method? > > In a class that uses a plugin which already defines, say, a > before_create method, > is it possible to extend this before_create so the code in the plugin''s > method is also invoked ? Something in effect like calling super in an > inherited class'' overwriting method. I suppose this is either easy > with some less-known syntax, which I haven''t found, or this is > impossible currently (I know it''s not a biggie to copy over the code > from the plugin method, but...) > > Thanks!You have do a double alias_method trick class Bar def foo ''foo'' end def foo_with_bar foo_without_bar + ''bar'' end alias_method :foo_without_bar, :foo alias_method :foo, :foo_with_bar end Bar.new.foo #=> "foobar" But, if you ask me, the best is the subclass it. This is what super is for. class MyBar < Bar def foo super + ''bar'' end end Much cleaner. Now just use MyBar in your app instead of the class provided by the plugin. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 12/14/06, Alex Wayne <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > You have do a double alias_method trick > > class Bar > def foo > ''foo'' > end > > def foo_with_bar > foo_without_bar + ''bar'' > end > > alias_method :foo_without_bar, :foo > alias_method :foo, :foo_with_bar > end > > Bar.new.foo #=> "foobar" > > But, if you ask me, the best is the subclass it. This is what super is > for. > > class MyBar < Bar > def foo > super + ''bar'' > end > end > > Much cleaner. Now just use MyBar in your app instead of the class > provided by the plugin. >I agree that subclasses are better when possible. That said, if you do have to alias and chain, use alias_method_chain to keep it cleaner: http://weblog.rubyonrails.org/2006/4/26/new-in-rails-module-alias_method_chain - Rob -- http://www.robsanheim.com http://www.seekingalpha.com http://www.ajaxian.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks Alex! Unfortunately the plugin offers mixin methods, such as one of those acts_as_... plugins.... So, there is no class I can subclass from. What a wicked double alias.... This is oddly cool in a way.... --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks Rob! Pretty cool. So glad to know advanced RoR''ers out there like you and Alex. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---