Decorate Devise current_user with Draper
24 Jan 2017Draper is awesome.
I normally use the handy decorates_assigned method to decorate model
objects in controllers.
Devise makes the
currently signed in user available through a current_user (or
current_admin, etc.) method. To decorate this with your Draper
UserDecorator, just add the following method to
ApplicationController:
protected
def current_user
super&.decorate
end
This just calls decorate on the model object returned by the Devise
helper. The &. is Ruby 2.3’s safe
navigation
method and ensures that the call to decorate doesn’t raise an
exception if no user is signed in (current_user is nil). For older
Ruby versions, you can use Rails’ #try method:
super.try(:decorate)
h.t. Ariejan de Vroom for the original idea.