This repository has been archived by the owner on Dec 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatically capture user context (#46)
* Automatically capture user context * Call include properly * Always yield * Check for around_action
- Loading branch information
1 parent
9076b47
commit 5bf7c37
Showing
11 changed files
with
135 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
module Timber | ||
module Probes | ||
# Responsible for automatically tracking controller call and http response events | ||
# for applications that use `ActionController`. | ||
class ActionControllerUserContext < Probe | ||
module AroundFilter | ||
def self.included(klass) | ||
klass.class_eval do | ||
if klass.respond_to?(:around_action) | ||
around_action :_timber_capture_user_context | ||
else | ||
around_filter :_timber_capture_user_context | ||
end | ||
|
||
private | ||
def _timber_capture_user_context | ||
if respond_to?(:current_user, true) | ||
id = Timber::Util::Object.try(current_user, :id) | ||
name = Timber::Util::Object.try(current_user, :name) | ||
if !name | ||
first_name = Timber::Util::Object.try(current_user, :first_name) | ||
last_name = Timber::Util::Object.try(current_user, :last_name) | ||
if first_name || last_name | ||
name = [first_name, last_name].compact.join(" ") | ||
end | ||
end | ||
email = Timber::Util::Object.try(current_user, :email) | ||
user_context = Timber::Contexts::User.new(:id => id, :name => name, :email => email) | ||
Timber::CurrentContext.with(user_context) do | ||
yield | ||
end | ||
else | ||
yield | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
def initialize | ||
require "action_controller" | ||
rescue LoadError => e | ||
raise RequirementNotMetError.new(e.message) | ||
end | ||
|
||
def insert! | ||
return true if ActionController::Base.include?(AroundFilter) | ||
ActionController::Base.send(:include, AroundFilter) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
require "spec_helper" | ||
|
||
describe Timber::Probes::ActionControllerUserContext do | ||
describe described_class::AroundFilter do | ||
let(:time) { Time.utc(2016, 9, 1, 12, 0, 0) } | ||
let(:io) { StringIO.new } | ||
let(:logger) do | ||
logger = Timber::Logger.new(io) | ||
logger.level = ::Logger::WARN | ||
logger | ||
end | ||
|
||
around(:each) do |example| | ||
class UserContextController < ActionController::Base | ||
layout nil | ||
|
||
def index | ||
logger.error "test" | ||
render json: {} | ||
end | ||
|
||
def method_for_action(action_name) | ||
action_name | ||
end | ||
|
||
private | ||
def current_user | ||
@current_user ||= begin | ||
user_struct = Struct.new(:id, :name, :email) | ||
user_struct.new(1, "Ben Johnson", "hi@timber.io") | ||
end | ||
end | ||
end | ||
|
||
::RailsApp.routes.draw do | ||
get 'user_context' => 'user_context#index' | ||
end | ||
|
||
old_logger = ::ActionController::Base.logger | ||
::ActionController::Base.logger = logger | ||
|
||
Timecop.freeze(time) { example.run } | ||
|
||
Object.send(:remove_const, :UserContextController) | ||
::ActionController::Base.logger = old_logger | ||
end | ||
|
||
describe "#index" do | ||
it "should capture the user context" do | ||
dispatch_rails_request("/user_context") | ||
expect(io.string).to include("\"user\":{\"id\":\"1\",\"name\":\"Ben Johnson\",\"email\":\"hi@timber.io\"}") | ||
end | ||
end | ||
end | ||
end |