I save the login time when the session is destroyed
UserSessionsController (controller)
def destroy
connectime = (Time.now - current_user.current_login_at).to_i
current_user.logtime(connectime)
current_user_session.destroy
redirect_to new_user_session_url
end
User (model)
def logtime(connectime)
self.log_duration ||= 0
self.log_duration += connectime
self.save
end
It's OK when the user logout, but doesn't work for logout_on_timeout
Where can I call a method when the user is logged out after timeout out to save this time of connexion ?
[Edit] An other way I'm looking at, is to send some method when the persistence_token is changed with after_persisting
, but cannot access to current_user in the model.
The problem is to find the right place to send instruction from a controller (session or user) when the user make some action on the server.
I find a solution trough application_controller. I'm afraid that multiple call slow down the server, but don't know how to make it better…
I add a column log_total_duration When the user interact to the server it call this before_filter to store the time the user is logged (log_duration)
When The user log in I use in the create method of the UserSessionController. It add the log_duration to the log_duration_total
in the User Model