<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>something learned comments on Snippet: using 'inherited' to set filters in controllers</title>
    <link>http://www.somethinglearned.com/</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>something learned comments</description>
    <item>
      <title>"Snippet: using 'inherited' to set filters in controllers" by trevor</title>
      <description>&lt;p&gt;One of my apps has a set of controllers in an &amp;#8217;/admin&amp;#8217; directory.  They all must have a &lt;code&gt;:login_required&lt;/code&gt; before_filter and they all must define an &lt;code&gt;authorized?&lt;/code&gt; method that checks if the current user is an administrator.&lt;/p&gt;


	&lt;p&gt;For a couple of reasons the idea of having a special &amp;#8220;AdminController&amp;#8221; which everything under &amp;#8217;/admin&amp;#8217; would inherit from (and that set up the filter and authorized? method) kind of irked me.&lt;/p&gt;


	&lt;p&gt;First, there was the name clash &amp;#8211; AdminController becomes &amp;#8216;admin&amp;#8217; in routes and that clashes with my directory called &amp;#8216;admin&amp;#8217;, so I&amp;#8217;d have to choose a name like &amp;#8220;AdminBaseController&amp;#8221; or some-such.  &lt;em&gt;Blech&lt;/em&gt;.&lt;/p&gt;


	&lt;p&gt;Second, I just didn&amp;#8217;t like the extra file for the base controller.  Call me picky, I can take it.&lt;/p&gt;


	&lt;p&gt;What I wanted was a way to say &amp;#8220;if the controller is in the /admin directory then it needs this filter and this method&amp;#8221;.&lt;/p&gt;


	&lt;p&gt;Class#inherited to the rescue.  Here&amp;#8217;s what I put in &lt;code&gt;/app/controllers/application.rb&lt;/code&gt;:&lt;/p&gt;


&lt;code&gt;&lt;pre&gt;
# For all controllers in the 'Admin' namespace we set
# the login_required before_filter and define an authorized?
# method that checks user.is_administrator? 
def self.inherited(subclass)
  # call super first - otherwise any before_filters we add are lost
  super
  if subclass.name.split('::').first == 'Admin'
    subclass.before_filter :login_required
    subclass.send(:define_method, :authorized?, Proc.new {|user| user.is_administrator?})
    subclass.send(:protected, :authorized?)
  end
end
&lt;/pre&gt;&lt;/code&gt;

	&lt;p&gt;This is fine for my present needs.  If controllers under /admin start to need any more shared behavior I&amp;#8217;ll just bite the bullet and do the AdminBaseController thing &amp;#8211; but for now this is clean and works.&lt;/p&gt;

</description>
      <pubDate>Fri, 26 May 2006 18:51:37 EDT</pubDate>
      <guid>&lt;a href="/articles/2006/05/26/snippet-using-inherited-to-set-filters-in-controllers"&gt;Snippet: using 'inherited' to set filters in controllers&lt;/a&gt;</guid>
      <link>&lt;a href="/articles/2006/05/26/snippet-using-inherited-to-set-filters-in-controllers"&gt;Snippet: using 'inherited' to set filters in controllers&lt;/a&gt;</link>
    </item>
  </channel>
</rss>
