Last active
December 21, 2015 14:18
-
-
Save jondeandres/6318186 to your computer and use it in GitHub Desktop.
DCI example
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
require 'delegate' | |
class GuestRole < SimpleDelegator | |
def books | |
Book.published | |
end | |
end | |
class AdminRole < SimpleDelegator | |
def books | |
Book.scoped | |
end | |
end | |
class BooksController | |
def index | |
role = role_for(current_user) | |
books = role.books | |
render :index, locals: { books: books } | |
end | |
def role_for(user) | |
role_name = user.role | |
klass = Object.const_get("#{role_name.classify}Role") | |
klass.new(user) | |
end | |
private :role_for | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment