Skip to content

Instantly share code, notes, and snippets.

@jondeandres
Last active December 21, 2015 14:18
Show Gist options
  • Save jondeandres/6318186 to your computer and use it in GitHub Desktop.
Save jondeandres/6318186 to your computer and use it in GitHub Desktop.
DCI example
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