lib/authtrail.rb in authtrail-0.2.1 vs lib/authtrail.rb in authtrail-0.2.2
- old
+ new
@@ -7,11 +7,11 @@
require "auth_trail/manager"
require "auth_trail/version"
module AuthTrail
class << self
- attr_accessor :exclude_method, :geocode, :track_method, :identity_method, :job_queue
+ attr_accessor :exclude_method, :geocode, :track_method, :identity_method, :job_queue, :transform_method
end
self.geocode = true
self.identity_method = lambda do |request, opts, user|
if user
user.try(:email)
@@ -20,11 +20,11 @@
request.params[scope] && request.params[scope][:email] rescue nil
end
end
def self.track(strategy:, scope:, identity:, success:, request:, user: nil, failure_reason: nil)
- info = {
+ data = {
strategy: strategy,
scope: scope,
identity: identity,
success: success,
failure_reason: failure_reason,
@@ -33,21 +33,25 @@
user_agent: request.user_agent,
referrer: request.referrer
}
if request.params[:controller]
- info[:context] = "#{request.params[:controller]}##{request.params[:action]}"
+ data[:context] = "#{request.params[:controller]}##{request.params[:action]}"
end
+ # add request data before exclude_method since exclude_method doesn't have access to request
+ # could also add 2nd argument to exclude_method when arity > 1
+ AuthTrail.transform_method.call(data, request) if AuthTrail.transform_method
+
# if exclude_method throws an exception, default to not excluding
- exclude = AuthTrail.exclude_method && AuthTrail.safely(default: false) { AuthTrail.exclude_method.call(info) }
+ exclude = AuthTrail.exclude_method && AuthTrail.safely(default: false) { AuthTrail.exclude_method.call(data) }
unless exclude
if AuthTrail.track_method
- AuthTrail.track_method.call(info)
+ AuthTrail.track_method.call(data)
else
login_activity = LoginActivity.new
- info.each do |k, v|
+ data.each do |k, v|
login_activity.try("#{k}=", v)
end
login_activity.save!
AuthTrail::GeocodeJob.perform_later(login_activity) if AuthTrail.geocode
end