Class: Acta::Web::EventsQuery

Inherits:
Object
  • Object
show all
Defined in:
lib/acta/web/events_query.rb

Overview

Builds the filtered Acta::Record scope that drives the event log admin UI. Extracted from EventsController so it can be unit-tested without a Rails-app fixture, and reused if other admin surfaces want the same filter semantics.

All filter values are user-supplied. String LIKE values are passed through ActiveRecord::Base.sanitize_sql_like to neutralise %/_ wildcards in user input.

Constant Summary collapse

FILTER_KEYS =

Names of params accepted; used by callers to build current-filter views and by tests to check the param surface.

%i[event_type stream_type actor_id stream_key q].freeze

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ EventsQuery

Returns a new instance of EventsQuery.



18
19
20
21
22
23
24
# File 'lib/acta/web/events_query.rb', line 18

def initialize(params = {})
  @event_type  = presence(params[:event_type])
  @stream_type = presence(params[:stream_type])
  @actor_id    = presence(params[:actor_id])
  @stream_key  = presence(params[:stream_key])
  @q           = presence(params[:q])
end

Instance Method Details

#active_filtersObject

Returns only the filters that are actually present, with symbol keys. Useful to build filter-chip UIs.



41
42
43
44
45
46
47
48
49
# File 'lib/acta/web/events_query.rb', line 41

def active_filters
  {
    event_type: @event_type,
    stream_type: @stream_type,
    actor_id: @actor_id,
    stream_key: @stream_key,
    q: @q
  }.compact
end

#scopeObject

Returns an unloaded ActiveRecord::Relation over Acta::Record with the configured filters applied. Caller is responsible for ordering, offset, limit, and any further scope chaining.



29
30
31
32
33
34
35
36
37
# File 'lib/acta/web/events_query.rb', line 29

def scope
  scope = Acta::Record.all
  scope = scope.where(event_type: @event_type)   if @event_type
  scope = scope.where(stream_type: @stream_type) if @stream_type
  scope = scope.where(actor_id: @actor_id)       if @actor_id
  scope = apply_stream_key(scope)                if @stream_key
  scope = apply_q(scope)                         if @q
  scope
end