amatsuda/active_decorator

How to decorate Devise's current_user ?

Closed this issue ยท 2 comments

First of all, thank you very much for this nice gem ! ๐Ÿ‘

Like 99% Rails projets I'm using devise in order to manage users and I'd like to decorate the current_user object from Devise and I'm wondering what would be the best way to do it?

As of now, based on this blog article and this issue I've added the following to my ApplicationController :

class ApplicationController < ActionController::Base
  # ...

  def current_user
    ActiveDecorator::Decorator.instance.decorate(super) unless super.nil?
  end
end

This works just fine.

Wow, I wasn't aware that I'm a 1% Rails developer...
Anyway, that indeed seems like the best way to simply implement what you need.

As trivial nitpicks,

  • generally, it'd be better to avoid calling the same super method twice here. You can use a lvar, e.g.)
    (u = super) && ActiveDecorator::Decorator.instance.decorate(u)
  • I'd define the current_user method as a private method since it's not an action_method (don't ask me why Devise doesn't do so)

Thank you @amatsuda for your comment !