# opening ApplicationController here to add default functionality # to override anything just override the appropriate method here. class ApplicationController < ActionController::Base before_action do create_log_entry! end def self.skip_logging! define_method(:create_log_entry!) do #nothing end end def create_log_entry! geo_data = begin ExpressGeoip.lookup(request.remote_ip) rescue => e {} end entity = entity_for_log entry_data = { action: action_for_log, username: current_user.try(:email), ip_address: request.ip, user_agent: request.user_agent, entity_type: entity.try(:class).try(:to_s), entity_id: entity.try(:id), notes: notes_for_log } ExpressAnalytics::LogEntry.create(entry_data.merge(geo_data)) end def action_for_log # ExpressAnalytics::LogEntriesController#show becomes # express_analytics/log_entries_controller#show # show log entry is_plural = %w(list index).include?(request.params[:action]) object_name = self.class.to_s.demodulize.underscore.titleize object_name.gsub!(/ Controller/, '') object_name = is_plural ? object_name : object_name.singularize verb = is_plural ? 'view' : request.params[:action] "#{verb} #{object_name}" end def entity_for_log self.respond_to?(:resource) ? resource : nil end def notes_for_log request.path # override to add notes end end # TODO: Move this to ExpressGeoip module ExpressGeoip def lookup(remote_ip) geo_data = {} if result = ExpressGeoip::GeoipLookup.lookup(request.remote_ip) if result.present? geo_data.merge!(geo_country_code: result.country.name, geo_administrative_area: nil, geo_locality: result.city.name, geo_latitude: (result.location.present? ? result.location.latitude : nil), geo_longitude: (result.location.present? ? result.location.latitude : nil) ) end end geo_data end end