1
0
Fork 0
mirror of https://github.com/heartcombo/devise.git synced 2022-11-09 12:18:31 -05:00

Move trackabe logic to the model.

This commit is contained in:
José Valim 2010-03-03 12:03:09 +01:00
parent 038627574c
commit 0e64bc74b7
2 changed files with 16 additions and 13 deletions

View file

@ -1,18 +1,7 @@
# After each sign in, update sign in time, sign in count and sign in IP.
Warden::Manager.after_set_user :except => :fetch do |record, warden, options|
scope = options[:scope]
if Devise.mappings[scope].try(:trackable?) && warden.authenticated?(scope)
old_current, new_current = record.current_sign_in_at, Time.now
record.last_sign_in_at = old_current || new_current
record.current_sign_in_at = new_current
old_current, new_current = record.current_sign_in_ip, warden.request.remote_ip
record.last_sign_in_ip = old_current || new_current
record.current_sign_in_ip = new_current
record.sign_in_count ||= 0
record.sign_in_count += 1
record.save(:validate => false)
if record.respond_to?(:update_tracked_fields!) && warden.authenticated?(scope)
record.update_tracked_fields!(warden.request)
end
end

View file

@ -11,6 +11,20 @@ module Devise
# * last_sign_in_at - Holds the remote ip of the previous sign in
#
module Trackable
def update_tracked_fields!(request)
old_current, new_current = self.current_sign_in_at, Time.now
self.last_sign_in_at = old_current || new_current
self.current_sign_in_at = new_current
old_current, new_current = self.current_sign_in_ip, request.remote_ip
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
save(:validate => false)
end
end
end
end