Skip to main content

Decorating Devise's current_user with Draper

·102 words·1 min· ·
Rails Devise Draper Decorators
Ariejan de Vroom
Author
Ariejan de Vroom
Jack of all Trades, Professional Software Craftsman

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?
end

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

Related

Rails 3 + Devise + Uploadify = No Flash Session Hacks
·550 words·3 mins
Rails Rails3 Devise Uploadify Flash
Showing Ruby, Rails and git info in your app
·206 words·1 min
Ruby Rails Protip
Automatically switch between SSL and non-SSL with Nginx+Unicorn+Rails
·1502 words·8 mins
Rails Rails3 Unicorn Nginx Ssl