Sha256: ae798e0e55f6e88137ae66d0d07f4416cf9abaa957a34cadbfd08429f9105cfe

Contents?: true

Size: 1.8 KB

Versions: 11

Compression:

Stored size: 1.8 KB

Contents

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

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
timber-1.1.14 lib/timber/probes/action_controller_user_context.rb
timber-1.1.13 lib/timber/probes/action_controller_user_context.rb
timber-1.1.12 lib/timber/probes/action_controller_user_context.rb
timber-1.1.11 lib/timber/probes/action_controller_user_context.rb
timber-1.1.10 lib/timber/probes/action_controller_user_context.rb
timber-1.1.9 lib/timber/probes/action_controller_user_context.rb
timber-1.1.8 lib/timber/probes/action_controller_user_context.rb
timber-1.1.7 lib/timber/probes/action_controller_user_context.rb
timber-1.1.6 lib/timber/probes/action_controller_user_context.rb
timber-1.1.5 lib/timber/probes/action_controller_user_context.rb
timber-1.1.4 lib/timber/probes/action_controller_user_context.rb