devroom.io/content/posts/2012-04-14-decorating-devise-s-current_user-with-draper.md

23 lines
1.0 KiB
Markdown
Raw Normal View History

2017-03-20 15:35:19 +00:00
+++
date = "2012-04-14"
title = "Decorating Devise's current_user with Draper"
tags = ["Rails", "devise", "draper", "decorators"]
slug = "decorating-devise-s-current_user-with-draper"
+++
2015-03-26 11:28:08 +00:00
I've become a big fan of decorators, especially [Draper](https://github.com/jcasimir/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`:
2017-03-20 15:35:19 +00:00
``` ruby
def current_user
UserDecorator.decorate(super) unless super.nil?
end
```
2015-03-26 11:28:08 +00:00
Now, anywhere in your views where you call `current_user` you'll get a decorated version instead.
2017-03-20 15:35:19 +00:00
[Check here to see how to use Draper with Sorcery](http://ariejan.net/2012/11/02/decorating_sorcery_current_user_with_draper)