Created
December 7, 2012 18:15
-
-
Save sh-ft/4235205 to your computer and use it in GitHub Desktop.
Controller inheritance use case for heimdallr-resource
This file contains 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
# 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