rails
(48,293 views)

alias method chain in a module

There's a number of postings that explain how to use alias_method_chain, but it wasn't clear how to use it in a module.

alias_method_chain is a neat little method that lets you cleanly override a core method in Rails. But I ran into syntax problems using it in a module. Here's the solution.

Given:

file: models/foo.rb

class Foo < ActiveRecord::Base
  include FooBar
end


file: lib/foo_bar.rb

module FooBar
  module ClassMethods
    def find_with_bar( *args ) 
      find_without_bar( *args )
      #...or whatever 
    end
  end
  
  def self.included(base)
    base.class_eval do
      extend ClassMethods
      class << self
        alias_method_chain :find, :bar
      end
    end
  end
  
end

The alias_method_chain will alias the original find to find_without_bar, and will alias find_with_bar to find.

The self.included method is called when the module is included in the Foo class. It extends the class methods with those defined in module ClassMethods. And then it does the alias_method_chain, on the Foo class (class << self).

 

 

Comments

by Maurizio on Aug 08, 2008

>>

Happy to see you have begun posting again ... :)

by Anonymous on Sep 04, 2008

>>

exactly what i need! thanks for the great post. - sandeep

by Foliosus on Sep 09, 2008

Saved my bacon

Thanks for this -- it's exactly what I was looking for. :)

by korinthe on Mar 19, 2010

and for instance methods...

  base.class_eval do
    extend ClassMethods
    alias_method_chain :link_to, :authentication
  end

(leaving off the class << self ... end)

by korinthe on Mar 19, 2010

formatting lost

And for instance methods...

  base.class_eval do&lt;br/&gt;
    extend ClassMethods&lt;br/&gt;
    alias_method_chain :link_to, :authentication&lt;br/&gt;
  end&lt;br/&gt;

(Leaving off the class << self ... end)

by assemwbuwk8 on Aug 28, 2016

d2d244sy

http://augmentin500mg.trade/ - augmentin http://advair-inhaler.science/ - advair medication http://clonidine-0-1-mg.cricket/ - clonidine 0.1 mg http://retinacream.trade/ - tretinoin retin a cream http://amitriptylinehcl.science/ - amitriptyline http://prednisolone-tablets.xyz/ - prednisolone tablets

by assemmtatid on Oct 03, 2016

wdwi5of5

[url=http://serpina.press/]serpina[/url] [url=http://kamagraonline.cricket/]kamagra 50mg[/url] [url=http://clindamycin.host/]clindamycin hcl 300 mg[/url] [url=http://zofrangeneric.trade/]zofran[/url] [url=http://viagra-soft.website/]viagra soft[/url]

by assemba74mw on Oct 25, 2016

vunrkzw6

http://sildenafil-generic.us/ - sildenafil generic http://tadaciponline.cricket/ - tadacip http://prednisone5mg.party/ - prednisone 5mg tablets http://cheap-viagra-online.bid/ - generic viagra prices http://seroquel-online.us/ - seroquel online http://serpina.men/ - serpina

by assemx6yjuh on Nov 12, 2016

hjfxxstw

http://buycytotec.red/ - buy cytotec http://ampicillinsulbactam.site/ - ampicillin 500 mg http://generic-valtrex.bid/ - valtrex http://viagracoupons.webcam/ - viagra price without insurance

by assem6u0agf on Dec 01, 2016

uauzdnx4

http://nolvadex-pct.cricket/ - nolvadex http://lisinopril-hydrochlorothiazide.party/ - hydrochlorothiazide http://buyeurax.info/ - buy eurax http://tretinoin-cream-0-05.us/ - tretinoin cream .025 http://buy-levaquin.space/ - levaquin http://buytoradol.in.net/ - buy toradol

by assem5xhhdm on Dec 01, 2016

hysfbbqt

http://atenolol25mg.gdn/ - atenolol 50 mg tablets http://buy-baclofen.pro/ - buy baclofen http://tadalafil-20mg.us/ - tadalafil

New Comment

markdown formatting permitted