2017-12-21 17:36:29 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2009-11-24 17:18:42 +00:00
|
|
|
require 'devise/hooks/trackable'
|
|
|
|
|
|
|
|
module Devise
|
|
|
|
module Models
|
|
|
|
# Track information about your user sign in. It tracks the following columns:
|
|
|
|
#
|
|
|
|
# * sign_in_count - Increased every time a sign in is made (by form, openid, oauth)
|
2011-09-09 09:46:35 +00:00
|
|
|
# * current_sign_in_at - A timestamp updated when the user signs in
|
2009-11-24 17:18:42 +00:00
|
|
|
# * last_sign_in_at - Holds the timestamp of the previous sign in
|
|
|
|
# * current_sign_in_ip - The remote ip updated when the user sign in
|
2011-07-23 22:03:10 +00:00
|
|
|
# * last_sign_in_ip - Holds the remote ip of the previous sign in
|
2009-11-24 17:18:42 +00:00
|
|
|
#
|
|
|
|
module Trackable
|
2012-02-19 00:50:59 +00:00
|
|
|
def self.required_fields(klass)
|
|
|
|
[:current_sign_in_at, :current_sign_in_ip, :last_sign_in_at, :last_sign_in_ip, :sign_in_count]
|
2012-02-17 19:43:15 +00:00
|
|
|
end
|
|
|
|
|
2014-05-21 23:18:35 +00:00
|
|
|
def update_tracked_fields(request)
|
2011-11-09 20:25:48 +00:00
|
|
|
old_current, new_current = self.current_sign_in_at, Time.now.utc
|
2010-03-03 11:03:09 +00:00
|
|
|
self.last_sign_in_at = old_current || new_current
|
|
|
|
self.current_sign_in_at = new_current
|
|
|
|
|
2012-07-12 18:34:31 +00:00
|
|
|
old_current, new_current = self.current_sign_in_ip, request.remote_ip
|
2010-03-03 11:03:09 +00:00
|
|
|
self.last_sign_in_ip = old_current || new_current
|
|
|
|
self.current_sign_in_ip = new_current
|
|
|
|
|
|
|
|
self.sign_in_count ||= 0
|
|
|
|
self.sign_in_count += 1
|
2014-05-21 23:18:35 +00:00
|
|
|
end
|
2010-03-03 11:03:09 +00:00
|
|
|
|
2014-05-21 23:18:35 +00:00
|
|
|
def update_tracked_fields!(request)
|
|
|
|
update_tracked_fields(request)
|
2017-11-28 14:58:41 +00:00
|
|
|
save
|
2010-03-03 11:03:09 +00:00
|
|
|
end
|
2009-11-24 17:18:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|