Decorating Devise's current_user with Draper

| Posted: - Last updated:
| Tagged rails devise draper decorators
| ~200wrds (~1min)

I’ve become a big fan of decorators, especially Draper.

Decorators allow you to move view related functionality for your models in to separate decorator classes. This keeps both your models and views clean and readable.

Anyway, if you use Devise you’re provided with a current_user helper. However, this helper returns an instance of User - without your decorators. To enable decorators for your current_user by default, simple add this to app/controllers/application_controller.rb:

def current_user
  UserDecorator.decorate(super) unless super.nil?

Now, anywhere in your views where you call current_user you’ll get a decorated version instead.

Check here to see how to use Draper with Sorcery

My site is free of ads and trackers. I record privacy-respecting usage statistics with Fathom.

Was this post helpful to you? Why not ☕ Buy me a coffee or use Brave Browser to send a tip.