Module: Philiprehberger::AuditTrail::Queryable
- Included in:
- Tracker
- Defined in:
- lib/philiprehberger/audit_trail/queryable.rb
Overview
Query builder for filtering audit events by multiple criteria.
Constant Summary collapse
- COUNT_BY_ALLOWED_FIELDS =
%i[entity_id entity_type action changes actor metadata timestamp].freeze
Instance Method Summary collapse
-
#count_by(field, **filters) ⇒ Hash{Object => Integer}
Aggregate event counts grouped by the value of the given field.
-
#query(**filters) ⇒ Array<Event>
Filter events by actor, action, entity_id, after, and before.
Instance Method Details
#count_by(field, **filters) ⇒ Hash{Object => Integer}
Aggregate event counts grouped by the value of the given field.
Iterates every event in the current filtered set (all stored events when no filters are supplied, otherwise the result of #query) and returns a ‘=> count` Hash. Keys are ordered by insertion. Events whose accessor returns `nil` are grouped under `nil`.
30 31 32 33 34 35 36 37 38 |
# File 'lib/philiprehberger/audit_trail/queryable.rb', line 30 def count_by(field, **filters) validate_count_by_field!(field) accessor = field.to_sym events = filters.empty? ? @store.all : query(**filters) events.each_with_object({}) do |event, counts| key = event.public_send(accessor) counts[key] = (counts[key] || 0) + 1 end end |
#query(**filters) ⇒ Array<Event>
Filter events by actor, action, entity_id, after, and before.
13 14 15 16 17 |
# File 'lib/philiprehberger/audit_trail/queryable.rb', line 13 def query(**filters) @store.select do |event| matches_filters?(event, filters) end end |