3

I have a rails app that is using Devise, with a User model, no scope. I've also added activeadmin gem in to the app, Active Admin is a gem used for adding an admin dashboard in your application. It uses Devise for logging in users and creates a separate admin_user model for the admins.

I am allowing anonymous, non-logged in users to create shopping carts, creating a session[:cart_id]. If a user logs in I want associate the user with the cart, something like

Cart.find(session[:cart_id]).user = current_user 

I was planning to use Wardens callbacks wardens callbacks to impliment this, something like so :

Warden::Manager.after_set_user :scope => :user do |user, auth, opts|

    Cart.find(session[:cart_id]).user = user 

end

However I get an error if I do that:

<% unless user_signed_in? %> throws an error :admin_user user is not logged in

Anyone got any ideas what is going on?

I've looked at related questions, but no help:

How to access session from Warden/Devise after_authentication callback in Rails

Where should warden callbacks be placed in a rails app?

Community
  • 1
  • 1
macarthy
  • 3,074
  • 2
  • 23
  • 24
  • What does Active Admin have to do with your example? I am guess that is the problem, you don't know why admin_user is showing up? – Amala Dec 01 '11 at 21:40
  • I implemented ActiveAdmin with a single user, I didn't want to user a separate admin_user. With that your authentication is integrated, a single devise class. Then there are roles per user. If you are interested in that solution, I can explain. – Amala Dec 02 '11 at 19:20

2 Answers2

2

The AdminUser model that Active Admin uses also executes this callback. So, maybe, an if can solve your problem:

 Warden::Manager.after_set_user :scope => :user do |user, auth, opts|
   Cart.find(session[:cart_id]).user = user if user.class == User
 end
Rodrigo Flores
  • 2,411
  • 18
  • 17
2

Actually it turned out the issue was solved by setting the default scope in warden,in the devise initializer file.

  # Configure the default scope given to Warden. By default it's the first
  # devise role declared in your routes (usually :user).
  # config.default_scope = :user

Since the active admin routes were added above the devise routes for my user, the adminuser became the default user.

macarthy
  • 3,074
  • 2
  • 23
  • 24