<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: alias_method_chain in models</title>
	<atom:link href="http://yehudakatz.com/2009/03/06/alias_method_chain-in-models/feed/" rel="self" type="application/rss+xml" />
	<link>http://yehudakatz.com/2009/03/06/alias_method_chain-in-models/</link>
	<description>Random Geek-Related Thoughts</description>
	<lastBuildDate>Tue, 16 Mar 2010 14:46:49 -0700</lastBuildDate>
	
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Decorating ActiveRecord Accessor Methods &#8226; Caffeine &#8226; Onehub</title>
		<link>http://yehudakatz.com/2009/03/06/alias_method_chain-in-models/comment-page-1/#comment-17967</link>
		<dc:creator>Decorating ActiveRecord Accessor Methods &#8226; Caffeine &#8226; Onehub</dc:creator>
		<pubDate>Tue, 12 Jan 2010 00:49:12 +0000</pubDate>
		<guid isPermaLink="false">http://yehudakatz.com/?p=226#comment-17967</guid>
		<description>[...] don&#8217;t like about this solution is its rampant use of alias_method_chain. There has been some debate in the Rails community over its validity as a design choice, though there really are no other [...]</description>
		<content:encoded><![CDATA[<p>[...] don&#8217;t like about this solution is its rampant use of alias_method_chain. There has been some debate in the Rails community over its validity as a design choice, though there really are no other [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rasheed Abdul-Aziz</title>
		<link>http://yehudakatz.com/2009/03/06/alias_method_chain-in-models/comment-page-1/#comment-17174</link>
		<dc:creator>Rasheed Abdul-Aziz</dc:creator>
		<pubDate>Tue, 22 Sep 2009 03:13:45 +0000</pubDate>
		<guid isPermaLink="false">http://yehudakatz.com/?p=226#comment-17174</guid>
		<description>Isn&#039;t it generally held that calling super is an anti-pattern? I&#039;ve always found it to be a fairly brittle approach to anything I do, and replace it with IOC/DI anywhere I can, especially when coding in a statically typed language. In Rails and Ruby, I thought the method aliasing approach was a far healthier approach so long as everyone is cautious about what the extend. Aliasing gives us multiple-inheritance, which isn&#039;t hideous, so long as you&#039;re clearly not out to hang yourself. Rubyists idiomatically avoid hanging themselves, and so welcome multiple inheritance in this way.

Or have I missed something obvious.</description>
		<content:encoded><![CDATA[<p>Isn&#8217;t it generally held that calling super is an anti-pattern? I&#8217;ve always found it to be a fairly brittle approach to anything I do, and replace it with IOC/DI anywhere I can, especially when coding in a statically typed language. In Rails and Ruby, I thought the method aliasing approach was a far healthier approach so long as everyone is cautious about what the extend. Aliasing gives us multiple-inheritance, which isn&#8217;t hideous, so long as you&#8217;re clearly not out to hang yourself. Rubyists idiomatically avoid hanging themselves, and so welcome multiple inheritance in this way.</p>
<p>Or have I missed something obvious.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michael Johnston</title>
		<link>http://yehudakatz.com/2009/03/06/alias_method_chain-in-models/comment-page-1/#comment-17109</link>
		<dc:creator>Michael Johnston</dc:creator>
		<pubDate>Thu, 03 Sep 2009 21:26:48 +0000</pubDate>
		<guid isPermaLink="false">http://yehudakatz.com/?p=226#comment-17109</guid>
		<description>@Nathan: which, to be honest, totally sucks from a maintainability standpoint in larger projects.

I would rather the model class DID tell you which bits of plugins it was using which change the behaviour of the standard api.</description>
		<content:encoded><![CDATA[<p>@Nathan: which, to be honest, totally sucks from a maintainability standpoint in larger projects.</p>
<p>I would rather the model class DID tell you which bits of plugins it was using which change the behaviour of the standard api.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nathan de Vries</title>
		<link>http://yehudakatz.com/2009/03/06/alias_method_chain-in-models/comment-page-1/#comment-16966</link>
		<dc:creator>Nathan de Vries</dc:creator>
		<pubDate>Sat, 08 Aug 2009 07:08:00 +0000</pubDate>
		<guid isPermaLink="false">http://yehudakatz.com/?p=226#comment-16966</guid>
		<description>I have a feeling that many people learn these techniques by looking at gems and plugins, and end up using them in their own applications where inheritance would be more appropriate.

But in the case of gems and plugins, alias_method_chain is exactly what you would want to use to sprinkle additional functionality on top of standard Rails methods without forcing users to adopt a different API. A good example of this is will_paginate, which allows users to call paginate() on standard AR::Base collections without modifying their models to inherit from something like WillPaginate::PaginatedRecord.</description>
		<content:encoded><![CDATA[<p>I have a feeling that many people learn these techniques by looking at gems and plugins, and end up using them in their own applications where inheritance would be more appropriate.</p>
<p>But in the case of gems and plugins, alias_method_chain is exactly what you would want to use to sprinkle additional functionality on top of standard Rails methods without forcing users to adopt a different API. A good example of this is will_paginate, which allows users to call paginate() on standard AR::Base collections without modifying their models to inherit from something like WillPaginate::PaginatedRecord.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: alias_method_chain em Models &#171; Spreading Logic by Rodrigo</title>
		<link>http://yehudakatz.com/2009/03/06/alias_method_chain-in-models/comment-page-1/#comment-16945</link>
		<dc:creator>alias_method_chain em Models &#171; Spreading Logic by Rodrigo</dc:creator>
		<pubDate>Wed, 05 Aug 2009 14:47:41 +0000</pubDate>
		<guid isPermaLink="false">http://yehudakatz.com/?p=226#comment-16945</guid>
		<description>[...]    Esse artigo é uma tradução do Post original do Yehuda [...]</description>
		<content:encoded><![CDATA[<p>[...]    Esse artigo é uma tradução do Post original do Yehuda [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Asfand Yar Qazi</title>
		<link>http://yehudakatz.com/2009/03/06/alias_method_chain-in-models/comment-page-1/#comment-16919</link>
		<dc:creator>Asfand Yar Qazi</dc:creator>
		<pubDate>Fri, 31 Jul 2009 05:38:40 +0000</pubDate>
		<guid isPermaLink="false">http://yehudakatz.com/?p=226#comment-16919</guid>
		<description>I was GOING to use alias_method in a custom form builder that I was writing, so that the form could either use (for example) the text_field method if it wanted the customizations the custom form builder introduced, or resort to (for example) original_text_field if it did not (the aliased-away old method).

But then I found something.  The excellent Ruby Facets library introduces a method called &#039;Kernel#as&#039; (description: &quot;Returns a As-functor that allows one to call any ancestor‘s method directly of the given object.&quot;)

So I now have a method in the form builder called &#039;original&#039;, which is simply:

    def original; return self.as(ancestor); end

So the form can choose to have the customizations provided by my form builder if it wants, by simply using text_field, or it can do this if I have a unique requirement that I don&#039;t want to hack into the form builder (because it&#039;s only going to be used once):

    

Another reason to use alias_method instead of super gone!

Note on usage: &#039;form.original.send(:text_field, ...)&#039; doesn&#039;t work.  Kernel#as can&#039;t be used like this, you have to use Kernel#send_as</description>
		<content:encoded><![CDATA[<p>I was GOING to use alias_method in a custom form builder that I was writing, so that the form could either use (for example) the text_field method if it wanted the customizations the custom form builder introduced, or resort to (for example) original_text_field if it did not (the aliased-away old method).</p>
<p>But then I found something.  The excellent Ruby Facets library introduces a method called &#8216;Kernel#as&#8217; (description: &#8220;Returns a As-functor that allows one to call any ancestor‘s method directly of the given object.&#8221;)</p>
<p>So I now have a method in the form builder called &#8216;original&#8217;, which is simply:</p>
<p>    def original; return self.as(ancestor); end</p>
<p>So the form can choose to have the customizations provided by my form builder if it wants, by simply using text_field, or it can do this if I have a unique requirement that I don&#8217;t want to hack into the form builder (because it&#8217;s only going to be used once):</p>
<p>Another reason to use alias_method instead of super gone!</p>
<p>Note on usage: &#8216;form.original.send(:text_field, &#8230;)&#8217; doesn&#8217;t work.  Kernel#as can&#8217;t be used like this, you have to use Kernel#send_as</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: wycats</title>
		<link>http://yehudakatz.com/2009/03/06/alias_method_chain-in-models/comment-page-1/#comment-16056</link>
		<dc:creator>wycats</dc:creator>
		<pubDate>Tue, 10 Mar 2009 15:16:43 +0000</pubDate>
		<guid isPermaLink="false">http://yehudakatz.com/?p=226#comment-16056</guid>
		<description>Heh... I give bad replies when I&#039;m half-asleep. Probably a better approach:

&lt;pre&gt;def find(*args)
  options = args.last.is_a?(Hash) ? args.last : {}
  unless options.delete(:skip_my_new_feature)
    # perform new feature
  end
  super
end&lt;/pre&gt;

This effectively provides a flag to turn off the new feature through the existing API. This sort of API is already in use by Rails (via a_m_c) in AR::Base#save, and it works better than trying to access an incidental byproduct of a_m_c.</description>
		<content:encoded><![CDATA[<p>Heh&#8230; I give bad replies when I&#8217;m half-asleep. Probably a better approach:</p>
<pre>def find(*args)
  options = args.last.is_a?(Hash) ? args.last : {}
  unless options.delete(:skip_my_new_feature)
    # perform new feature
  end
  super
end</pre>
<p>This effectively provides a flag to turn off the new feature through the existing API. This sort of API is already in use by Rails (via a_m_c) in AR::Base#save, and it works better than trying to access an incidental byproduct of a_m_c.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: hukl</title>
		<link>http://yehudakatz.com/2009/03/06/alias_method_chain-in-models/comment-page-1/#comment-16055</link>
		<dc:creator>hukl</dc:creator>
		<pubDate>Tue, 10 Mar 2009 14:36:40 +0000</pubDate>
		<guid isPermaLink="false">http://yehudakatz.com/?p=226#comment-16055</guid>
		<description>Alright thank you!</description>
		<content:encoded><![CDATA[<p>Alright thank you!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: wycats</title>
		<link>http://yehudakatz.com/2009/03/06/alias_method_chain-in-models/comment-page-1/#comment-16046</link>
		<dc:creator>wycats</dc:creator>
		<pubDate>Tue, 10 Mar 2009 09:03:51 +0000</pubDate>
		<guid isPermaLink="false">http://yehudakatz.com/?p=226#comment-16046</guid>
		<description>You wouldn&#039;t! The idea here is to modify the &quot;save&quot; functionality to modify it with some changes. If you *really* need access to the old functionality alone, you could do what you suggested (&lt;code&gt;def original_find(*args, &amp;blk) self.superclass.find(*args, &amp;blk)&lt;/code&gt;)

But honestly, if you wanted the original around, you should probably alias it off to the side for continued use, and then redefine the original method and use super.</description>
		<content:encoded><![CDATA[<p>You wouldn&#8217;t! The idea here is to modify the &#8220;save&#8221; functionality to modify it with some changes. If you *really* need access to the old functionality alone, you could do what you suggested (<code>def original_find(*args, &#038;blk) self.superclass.find(*args, &#038;blk)</code>)</p>
<p>But honestly, if you wanted the original around, you should probably alias it off to the side for continued use, and then redefine the original method and use super.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: hukl</title>
		<link>http://yehudakatz.com/2009/03/06/alias_method_chain-in-models/comment-page-1/#comment-16045</link>
		<dc:creator>hukl</dc:creator>
		<pubDate>Tue, 10 Mar 2009 08:00:55 +0000</pubDate>
		<guid isPermaLink="false">http://yehudakatz.com/?p=226#comment-16045</guid>
		<description>The only way I found was to call Foo.superclass.say_hello or Foo.superclass.find to stick with your example which would be totally okay i guess.

def original_find
  self.superclass.find
end

Is that the way then ?</description>
		<content:encoded><![CDATA[<p>The only way I found was to call Foo.superclass.say_hello or Foo.superclass.find to stick with your example which would be totally okay i guess.</p>
<p>def original_find<br />
  self.superclass.find<br />
end</p>
<p>Is that the way then ?</p>
]]></content:encoded>
	</item>
</channel>
</rss>
