masta Blasta
2013-Sep-17 19:59 UTC
Unable to override methods built with alias_method_chain
I am using this plugin and trying to extend it: https://github.com/goncalossilva/subdomain_routes/blob/master/lib/subdomain_routes/url_writer.rb The plugin heavily modifies some the Rails routing (Rails 2.3.x in this case, but that''s not important) It usually follows this pattern: module SubdomainRoutes module UrlWriter def self.included(base) base.alias_method_chain :rewrite, :subdomains end def native_plugin_method ...a bunch of code end def rewrite_with_subdomains ...code end end end ActionController::UrlWriter.send :include, SubdomainRoutes::UrlWriter In my own classes I am attempting to override some of these methods. So i do this: module CustomRouting def native_plugin_method #successfully invoked ...a bunch of code super #will call method from SubdomainRoutes::UrlWriter end def rewrite_with_subdomains #method is never invoked ...code super end end ActionController::UrlWriter.send :include, CustomRouting The method created with alias_method_chain is never affected! My code successfully overrides ''native_plugin_method'', but it seems to do nothing with rewrite_with_subdomains, even though everything is identical. Is there something about aliased methods that does not resolve in the same way as standard methods? -- 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 unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/f0f56d2755352ff90a7c3f43f33432ea%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.
Frederick Cheung
2013-Sep-17 21:44 UTC
Re: Unable to override methods built with alias_method_chain
On Tuesday, September 17, 2013 8:59:06 PM UTC+1, Ruby-Forum.com User wrote:> > > The method created with alias_method_chain is never affected! My code > successfully overrides ''native_plugin_method'', but it seems to do > nothing with rewrite_with_subdomains, even though everything is > identical. > > Is there something about aliased methods that does not resolve in the > same way as standard methods? > >aliasing pretty much creates a copy of a method - later attempts to redefine the original method don''t affect the copy. For example class Example def foo puts "foo" end alias_method :bar, :foo def foo puts "new foo" end end Example.new.foo() #=> "new foo" Example.new.bar() #=> "foo" You''ll have to do your own alias method chaining when you include your module (in newer versions of rails alias_method_chain has falled out of favour) Fred -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/4e24caee-3573-463c-9b17-c6a75abf01f8%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
masta Blasta
2013-Sep-18 14:27 UTC
Re: Unable to override methods built with alias_method_chain
Frederick Cheung wrote in post #1121711:> On Tuesday, September 17, 2013 8:59:06 PM UTC+1, Ruby-Forum.com User > wrote: >> > aliasing pretty much creates a copy of a method - later attempts to > redefine the original method don''t affect the copy. For example > > class Example > def foo > puts "foo" > end > > alias_method :bar, :foo > > def foo > puts "new foo" > end > end > > Example.new.foo() #=> "new foo" > Example.new.bar() #=> "foo" > > You''ll have to do your own alias method chaining when you include your > module (in newer versions of rails alias_method_chain has falled out of > favour) > > FredThat''s exactly what it was! I had to re-alias the method names. So the original plugin used alias_method_chain to alias :rewrite, :rewrite_with_subdomain I had to restructure my module like this: module CustomRouting def included(base) base.send :alias_method, :rewrite, :rewrite_with_subdomains end def native_plugin_method #successfully invoked ...a bunch of code super #will call method from SubdomainRoutes::UrlWriter end def rewrite_with_subdomains #method is never invoked ...code super end end And now my overloaded method is correctly invoked, and the ''super'' call works properly and invokes the previous alias. This was really complicated. phew -- 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 unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/d4155e7b902318790b55afddae90ba84%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.