Class: Compass::Context

Inherits:
Object
  • Object
show all
Includes:
MustImplement
Defined in:
lib/compass/context.rb

Overview

The request-scoped Compass context.

An application subclasses this and assigns the subclass to Compass.config.context. Compass::Middleware instantiates it once per request with the Rack env and stores it in env[Compass::Context::ENV_KEY], so the same instance is reused for authentication, identification, caching, and client authentication across the whole request. It also acts as the context passed to Menu, Search, and Notification providers.

Use Compass::Context.from(env) to retrieve the context for a request.

I.e.:

class CompassContext < Compass::Context
def authenticate!
  current_user.present?
end

def context_id     = current_user&.id
def modified_at    = current_user&.updated_at
def auth_token     = current_user&.api_token
def auth_expires_at = current_user&.token_expires_at

# Exposed to providers and view locals.
def to_hash = { current_user: current_user }

def current_user
  @current_user ||= User.find_by(token: request.authorization.to_s.remove("Bearer "))
end
end

Compass.configure do |config|
config.context = CompassContext
end

Constant Summary collapse

ENV_KEY =

Rack env key under which the request context is memoized.

"compass.context".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MustImplement

#must_implement

Constructor Details

#initialize(env, request = ActionDispatch::Request.new(env)) ⇒ Context

Returns a new instance of Context.

Parameters:

  • env (Hash)

    the Rack environment



61
62
63
64
# File 'lib/compass/context.rb', line 61

def initialize(env, request = ActionDispatch::Request.new(env))
  @env = env
  @request = request
end

Instance Attribute Details

#envHash (readonly)

The Rack environment the context was built from.

Returns:

  • (Hash)


55
56
57
# File 'lib/compass/context.rb', line 55

def env
  @env
end

#requestObject (readonly)

The request object wrapping the request env



58
59
60
# File 'lib/compass/context.rb', line 58

def request
  @request
end

Class Method Details

.from(env) ⇒ Compass::Context

The context for the given Rack env. Builds the configured context and memoizes it in the env when absent, guaranteeing a single context per request.

Parameters:

  • env (Hash)

    the Rack environment

Returns:



49
50
51
# File 'lib/compass/context.rb', line 49

def self.from(env)
  env[ENV_KEY] ||= Compass.config.context.new(env)
end

Instance Method Details

#auth_expires_atTime?

Expiration time for the auth token.

Returns:

  • (Time, nil)


85
# File 'lib/compass/context.rb', line 85

def auth_expires_at = nil

#auth_tokenString?

Bearer token exposed through the client auth endpoint.

Returns:

  • (String, nil)


81
# File 'lib/compass/context.rb', line 81

def auth_token = nil

#authenticate!Boolean

Authenticates the current request.

Returns:

  • (Boolean)

    whether the request is authenticated.



68
# File 'lib/compass/context.rb', line 68

def authenticate! = false

#context_idString, ...

The context identifier. Used to namespace the Compass API paths and as an ETag source.

Returns:

  • (String, Integer, nil)


73
# File 'lib/compass/context.rb', line 73

def context_id = must_implement(:context_id)

#modified_atTime?

Timestamp used for HTTP ETag caching.

Returns:

  • (Time, nil)


77
# File 'lib/compass/context.rb', line 77

def modified_at = must_implement(:modified_at)

#to_hashHash

Hash representation of the context. It is splatted into Menu, Search, and Notification providers as well as into Search view locals, so applications should override it to expose the data providers depend on.

Returns:

  • (Hash)


91
# File 'lib/compass/context.rb', line 91

def to_hash = {}