Class: Compass::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/compass/middleware.rb

Overview

Rack middleware that establishes the request-scoped Compass context.

It builds the configured Compass::Context once per request and memoizes it in the Rack env under Compass::Context::ENV_KEY, so controllers and components reuse the same instance via Compass::Context.from(env).

It also runs per-request Compass features with that context — currently breadcrumb tracking, which uses the configured breadcrumb strategy to save breadcrumb items for requests that should be tracked.

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



13
14
15
# File 'lib/compass/middleware.rb', line 13

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/compass/middleware.rb', line 17

def call(env)
  request = ActionDispatch::Request.new(env)
  # bearer:disable ruby_rails_sql_injection
  context = Compass::Context.from(env)

  save_breadcrumb!(context, request) if should_track?(context, request)
rescue => e
  # Silently handle errors - don't break the request
  Compass.logger&.error "Compass middleware error: #{e.message}"
ensure
  return @app.call(env)
end