3

I'm having a little trouble understanding how to access devise's current_user from a model/super level. I want to set a few attributes as session vars (so I don't have to run an app-wide before_filter every time.

Would you recommend setting session keys (what's the proper term?), or can I modify the current_user object by tapping a devise method?

Kevin Brown
  • 12,602
  • 34
  • 95
  • 155

1 Answers1

0

Depending on what you're trying to do, you could always override the current_user method (which is set on the ApplicationController) and make whatever modifications will suit your needs. More information here:

Where to override current_user helper method of devise gem

In the above example, you could always take what devise_current_user returns and then modify it before returning from your overriding current_user method.

Community
  • 1
  • 1
vthomas2007
  • 153
  • 6
  • As far as best practices, if I've got things I want to do on or around involving the `current_user` object, it wouldn't be great to have several ApplicationController filters? Wouldn't that cut down on my app efficiency? – Kevin Brown Jan 13 '16 at 19:13
  • Actually, current_user isn't an object at all, it's a helper method that's already being added to the ApplicationController! I wouldn't worry too much about taking what it returns and possibly modifying it from a performance standpoint, it's probably a premature optimization. For more information, check out the Devise code: https://github.com/plataformatec/devise/blob/master/lib/devise/controllers/helpers.rb – vthomas2007 Jan 13 '16 at 19:19
  • Can you explain? I'm a bit of a novice, so when I see `current_user.first_name`, it looks like an object to me! – Kevin Brown Jan 13 '16 at 19:21
  • Sure thing. The short version of the story is that current_user is a method that returns a user object. You're actually calling first_name on the user object that is being returned. So you can always call current_user, assign the resulting object to a variable, make changes to the object, and then return it instead. What kinds of attributes are you trying to set? Perhaps a better approach would be to add methods to your User class that will do what you want (such as "full_name") and then call that method instead (i.e. "current_user.full_name") – vthomas2007 Jan 13 '16 at 19:29
  • Several, one for first-time-login tutorial, one for gravatar, but I want to check for gravatar existence first, then assign a temp robohash if not...just UX things. – Kevin Brown Jan 13 '16 at 21:17
  • Gotcha, you'll probably need to handle these on a case-by-case basis. For the first-time login tutorial, check out Devise's sign_in_count (you should be able to access this via current_user.sign_in_count). – vthomas2007 Jan 13 '16 at 21:32
  • And it's OK to have an ApplicationController before_filter to check for `sign_in_count`? I just imagine ending up with like 20 before filters... – Kevin Brown Jan 13 '16 at 21:35
  • We would need to know more about the specifics of what you're trying to do to provide more guidance, I'm just pointing you towards some existing functionality that might be helpful. There are lots of ways to approach this. For the first-time-login tutorial issue, one way would be to use the after_sign_in_path_for method and have it return the tutorial path if current_user's login count is 1. (more info on this method: https://github.com/plataformatec/devise/wiki/How-To:-redirect-to-a-specific-page-on-successful-sign-in ) – vthomas2007 Jan 13 '16 at 22:06