Created
August 3, 2011 15:54
-
-
Save betawaffle/1122985 to your computer and use it in GitHub Desktop.
Controller Ancestry
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<!-- Which allows you to do cool stuff like this: --> | |
<%= stylesheet_link_tag *controller.ancestry %> | |
<%= javascript_include_tag *controller.ancestry %> | |
<!-- So from within BarController, the stylesheets would look like this: --> | |
<link href="/assets/application.css" media="screen" rel="stylesheet" type="text/css" /> | |
<link href="/assets/foo.css" media="screen" rel="stylesheet" type="text/css" /> | |
<link href="/assets/bar.css" media="screen" rel="stylesheet" type="text/css" /> | |
</head> | |
<body> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationController < ActionController::Base | |
def ancestry | |
klass = self.class | |
names = [] | |
while name = klass.controller_name | |
names.unshift name | |
break if name == 'application' | |
klass = klass.superclass | |
end | |
names | |
end | |
end | |
class FooController < ApplicationController | |
end | |
class BarController < FooController | |
end | |
# FooController.ancestry | |
# => ['application', 'foo'] | |
# BarController.ancestry | |
# => ['application', 'foo', 'bar'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment