<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Wood for the Trees: hide_action, a hidden treasure</title>
    <link>http://www.mathewabonyi.com/articles/2006/08/11/hide_action-a-hidden-treasure</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>struggling to dig life</description>
    <item>
      <title>hide_action, a hidden treasure</title>
      <description>&lt;p style="text-align:justify;"&gt;This is a quickie in my preparation for releasing Simple Scope, a &lt;span class="caps"&gt;DRY&lt;/span&gt; one-liner version of ScopedAccess. I think there are many hidden treats in the ActionController::Base which aren&amp;#8217;t talked about.&lt;/p&gt;


	&lt;p style="text-align:justify;"&gt;One such treat is the hide_action class method. By default, everything in ActionController::Base and its modules are hidden from being actions, but its children need to protect and privatise their methods to prevent them from being executed by :controller/:action style URLs. For plugin developers working with ActionControllers especially, adding a call to hide_action can save a lot of pain. For example, for a controller plugin:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;self.included&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;base&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
&lt;span class="ident"&gt;base&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;send&lt;/span&gt; &lt;span class="symbol"&gt;:helper_method&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="symbol"&gt;:methodone&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="symbol"&gt;:methodtwo&lt;/span&gt;
&lt;span class="ident"&gt;base&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;send&lt;/span&gt; &lt;span class="symbol"&gt;:hide_action&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="symbol"&gt;:methodone&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="symbol"&gt;:methodtwo&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p style="text-align:justify;"&gt;Now you can have public methods included into controllers without the worry of them being executed.&lt;/p&gt;


	&lt;p style="text-align:justify;"&gt;An example usage is my Authenticated Cookie variant of Technoweeine&amp;#8217;s &lt;span class="caps"&gt;AAA&lt;/span&gt; plugin. Both versions protect the entire module, where in fact accessing current_user should be public to other controllers, their classes, views and models (god forbid). I just recently changed logged_in? and current_user to be public and added the above snippet to the self.included method. Now class defined procs, such as before_filters, can do this:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;  &lt;span class="ident"&gt;before_filter&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="symbol"&gt;:only&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="symbol"&gt;:when_needed&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt; &lt;span class="punct"&gt;{&lt;/span&gt; &lt;span class="punct"&gt;|&lt;/span&gt;&lt;span class="ident"&gt;c&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt; &lt;span class="ident"&gt;params&lt;/span&gt;&lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="symbol"&gt;:user_id&lt;/span&gt;&lt;span class="punct"&gt;]&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;c&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;current_user&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;id&lt;/span&gt; &lt;span class="punct"&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p style="text-align:justify;"&gt;Much cleaner than c.send :current_user.&lt;/p&gt;</description>
      <pubDate>Fri, 11 Aug 2006 12:21:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:6f89cf09-066f-46cc-b9cf-ecb0be081837</guid>
      <author>Mathew Abonyi</author>
      <link>http://www.mathewabonyi.com/articles/2006/08/11/hide_action-a-hidden-treasure</link>
      <category>Rails</category>
      <trackback:ping>http://www.mathewabonyi.com/articles/trackback/19</trackback:ping>
    </item>
    <item>
      <title>"hide_action, a hidden treasure" by Alex</title>
      <description>&lt;p&gt;Well, that works, except if more than one plugin does it, or if the user is doing it themselves, the methods will conflict.&lt;/p&gt;</description>
      <pubDate>Thu, 09 Aug 2007 02:10:12 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:a8b70ca5-5625-4d25-8897-b31b3d5f6be4</guid>
      <link>http://www.mathewabonyi.com/articles/2006/08/11/hide_action-a-hidden-treasure#comment-175</link>
    </item>
  </channel>
</rss>
