Skip to content

Instantly share code, notes, and snippets.

@sh-ft
Created December 7, 2012 18:15
Show Gist options
  • Save sh-ft/4235205 to your computer and use it in GitHub Desktop.
Save sh-ft/4235205 to your computer and use it in GitHub Desktop.
Controller inheritance use case for heimdallr-resource
# Let's say we have a blog app. A blog post has several child resources (comments, tags,
# etc.). The controllers for each of these resources share the same layout. Here's one
# way we could do it:
class BasePostsController < ApplicationController
include Heimdallr::Resource
load_and_authorize_resource :resource => :post
# We could have a before_filter here that sets some instance variables for the layout
# based on the loaded @post.
end
class PostCommentsController < BasePostsController
load_and_authorize_resource :resource => :comment, :through => :post
def index
# ...
end
# ...
end
class PostTagsController < BasePostsController
load_and_authorize_resource :resource => :tags, :through => :post
def index
# ...
end
# ...
end
# And so on for the other child controllers.
# This way we have extracted the common code into the base controller class.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment