Skip to content

Instantly share code, notes, and snippets.

@betawaffle
Created August 3, 2011 15:54
Show Gist options
  • Save betawaffle/1122985 to your computer and use it in GitHub Desktop.
Save betawaffle/1122985 to your computer and use it in GitHub Desktop.
Controller Ancestry
<!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>
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