Sha256: 5e97980ae16af277440b44b9f481c257a59be059a16161df2fb6814556d3a095

Contents?: true

Size: 1.56 KB

Versions: 11

Compression:

Stored size: 1.56 KB

Contents

module Timber
  module Contexts
    # The user context tracks the currently authenticated user.
    #
    # You will want to add this context at the time log the user in, typically
    # during the authentication flow.
    #
    # Note: Timber will attempt to automatically add this if you add a #current_user
    # method to your controllers. Most authentication solutions do this for you automatically.
    #
    # @example Basic example
    #   user_context = Timber::Contexts::User.new(id: "abc1234", name: "Ben Johnson")
    #   Timber::CurrentContext.with(user_context) do
    #     # Logging will automatically include this context
    #     logger.info("This is a log message")
    #   end
    #
    # @example Rails example
    #   class ApplicationController < ActionController::Base
    #     around_filter :capture_user_context
    #     private
    #       def capture_user_context
    #         if current_user
    #           user_context = Timber::Contexts::User.new(id: current_user.id,
    #             name: current_user.name, email: current_user.email)
    #           Timber::CurrentContext.with(user_context) { yield }
    #         else
    #           yield
    #         end
    #       end
    #   end
    class User < Context
      @keyspace = :user

      attr_reader :id, :name, :email

      def initialize(attributes)
        @id = attributes[:id]
        @name = attributes[:name]
        @email = attributes[:email]
      end

      def as_json(_options = {})
        {id: Timber::Util::Object.try(id, :to_s), name: name, email: email}
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
timber-2.0.0 lib/timber/contexts/user.rb
timber-1.1.14 lib/timber/contexts/user.rb
timber-1.1.13 lib/timber/contexts/user.rb
timber-1.1.12 lib/timber/contexts/user.rb
timber-1.1.11 lib/timber/contexts/user.rb
timber-1.1.10 lib/timber/contexts/user.rb
timber-1.1.9 lib/timber/contexts/user.rb
timber-1.1.8 lib/timber/contexts/user.rb
timber-1.1.7 lib/timber/contexts/user.rb
timber-1.1.6 lib/timber/contexts/user.rb
timber-1.1.5 lib/timber/contexts/user.rb