Class: Compass::Context
- Inherits:
-
Object
- Object
- Compass::Context
- 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..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
-
#env ⇒ Hash
readonly
The Rack environment the context was built from.
-
#request ⇒ Object
readonly
The request object wrapping the request env.
Class Method Summary collapse
-
.from(env) ⇒ Compass::Context
The context for the given Rack env.
Instance Method Summary collapse
-
#auth_expires_at ⇒ Time?
Expiration time for the auth token.
-
#auth_token ⇒ String?
Bearer token exposed through the client auth endpoint.
-
#authenticate! ⇒ Boolean
Authenticates the current request.
-
#context_id ⇒ String, ...
The context identifier.
-
#initialize(env, request = ActionDispatch::Request.new(env)) ⇒ Context
constructor
A new instance of Context.
-
#modified_at ⇒ Time?
Timestamp used for HTTP ETag caching.
-
#to_hash ⇒ Hash
Hash representation of the context.
Methods included from MustImplement
Constructor Details
#initialize(env, request = ActionDispatch::Request.new(env)) ⇒ Context
Returns a new instance of Context.
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
#env ⇒ Hash (readonly)
The Rack environment the context was built from.
55 56 57 |
# File 'lib/compass/context.rb', line 55 def env @env end |
#request ⇒ Object (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.
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_at ⇒ Time?
Expiration time for the auth token.
85 |
# File 'lib/compass/context.rb', line 85 def auth_expires_at = nil |
#auth_token ⇒ String?
Bearer token exposed through the client auth endpoint.
81 |
# File 'lib/compass/context.rb', line 81 def auth_token = nil |
#authenticate! ⇒ Boolean
Authenticates the current request.
68 |
# File 'lib/compass/context.rb', line 68 def authenticate! = false |
#context_id ⇒ String, ...
The context identifier. Used to namespace the Compass API paths and as an ETag source.
73 |
# File 'lib/compass/context.rb', line 73 def context_id = must_implement(:context_id) |
#modified_at ⇒ Time?
Timestamp used for HTTP ETag caching.
77 |
# File 'lib/compass/context.rb', line 77 def modified_at = must_implement(:modified_at) |
#to_hash ⇒ Hash
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.
91 |
# File 'lib/compass/context.rb', line 91 def to_hash = {} |