Using Devise with gem public_activity - overwrite sign action?

401 views
Skip to first unread message

martin

unread,
Jun 1, 2012, 9:28:41 AM6/1/12
to plataforma...@googlegroups.com
Hi there,

I'm still new to Rails, but I'm very excited about it! I love Devise and now I found a gem 'public_activity' which perfectly fits my needs. PublicActivity works great, but I want to track the users login and logout actions. PublicActivity works on the model. After create, update, destroy, etc. there will be generated a new record in the activities database.

I added public_activity to my user model and it works fine. If the user signs in into my app he gets updadted by devise (mainly setting last_login, ip etc.) and a public_activity update record will be done. But with public_activity it is possible to define custom actions by keys. Therefore I want to create a custom activity key on every sign in and sign out. To do this, I have to put the following line for the save call of the user which currently signed in.

"@user.activity :key => 'activity.users.login'"

If that is possible I can create a custom message "User xy signed in". Currently there will be displayed "User xy was changed". I have absolulety no idea where to put this line. I saw the SessionsController#new method, but didn't found any save call.

Is it possible to overwrite the login action to put this line before the user gets updated? Are there any callbacks I can use? 

Thanks for every tip!

Martin

Carlos Antonio da Silva

unread,
Jun 1, 2012, 10:50:22 AM6/1/12
to plataforma...@googlegroups.com
A user can "sign in" from many different ways, not only by sessions controller, for instance if you're using rememberable, it's coming through a cookie.

The best place for you to do this would be at Warden level: using after_authentication and before_logout callbacks, as you can see here: https://github.com/hassox/warden/wiki/Callbacks

You can check how devise uses such hooks: https://github.com/plataformatec/devise/tree/master/lib/devise/hooks, and base your code on that.

-- 
At.
Carlos Antonio

martin

unread,
Jun 4, 2012, 7:38:55 AM6/4/12
to plataforma...@googlegroups.com
Hi Carlos,

thanks for your quick reply! Inspired by another gem I created a new hook for devise, called public_activity_trackable which overwrites the trackable behaviour of devise and will set the key for sign in. Thanks a lot for your links, it was exactly what I needed. Maybe if I find enough time I will publish this behaviour on github as a gem, but I have no experience in publishing a gem so it can take a few weeks until I can publish it!

Regards,
Martin

Carlos Antonio da Silva

unread,
Jun 4, 2012, 7:47:50 AM6/4/12
to plataforma...@googlegroups.com
Great to see it is working :)

Please feel free to post an update here about your new gem when you release it. You can also add / update a wiki page in Devise's wiki with examples of what you did to help other people in the future.

Thanks.

-- 
At.
Carlos Antonio

Reply all
Reply to author
Forward
0 new messages