Class: Athar::Dashboard::FilterSet

Inherits:
Object
  • Object
show all
Defined in:
lib/athar/dashboard/filter_set.rb

Overview

Immutable value object holding the parsed query-param state for a dashboard request. Exposed by DashboardController#index as @filters and consumed by FeedQuery, KpiCalculator, ActorOptions, and the partials.

Constant Summary collapse

TIMES =
{ "24h" => 24.hours, "7d" => 7.days, "30d" => 30.days, "all" => nil }.freeze
MODES =
%w[all identity only snapshot].freeze
KINDS =
%w[all delete truncate].freeze
DEFAULT_TIME =
"30d"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model:, time:, mode:, kind:, actor:, query:, page:, expanded:) ⇒ FilterSet

rubocop:disable Metrics/ParameterLists



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/athar/dashboard/filter_set.rb', line 29

def initialize(model:, time:, mode:, kind:, actor:, query:, page:, expanded:) # rubocop:disable Metrics/ParameterLists
  @model = model
  @time = time
  @mode = mode
  @kind = kind
  @actor = actor
  @query = query
  @page = page
  @expanded = expanded

  freeze
end

Instance Attribute Details

#actorObject (readonly)

Returns the value of attribute actor.



14
15
16
# File 'lib/athar/dashboard/filter_set.rb', line 14

def actor
  @actor
end

#expandedObject (readonly)

Returns the value of attribute expanded.



14
15
16
# File 'lib/athar/dashboard/filter_set.rb', line 14

def expanded
  @expanded
end

#kindObject (readonly)

Returns the value of attribute kind.



14
15
16
# File 'lib/athar/dashboard/filter_set.rb', line 14

def kind
  @kind
end

#modeObject (readonly)

Returns the value of attribute mode.



14
15
16
# File 'lib/athar/dashboard/filter_set.rb', line 14

def mode
  @mode
end

#modelObject (readonly)

Returns the value of attribute model.



14
15
16
# File 'lib/athar/dashboard/filter_set.rb', line 14

def model
  @model
end

#pageObject (readonly)

Returns the value of attribute page.



14
15
16
# File 'lib/athar/dashboard/filter_set.rb', line 14

def page
  @page
end

#queryObject (readonly)

Returns the value of attribute query.



14
15
16
# File 'lib/athar/dashboard/filter_set.rb', line 14

def query
  @query
end

#timeObject (readonly)

Returns the value of attribute time.



14
15
16
# File 'lib/athar/dashboard/filter_set.rb', line 14

def time
  @time
end

Class Method Details

.from_params(params) ⇒ Object

rubocop:disable Metrics/AbcSize



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/athar/dashboard/filter_set.rb', line 16

def self.from_params(params) # rubocop:disable Metrics/AbcSize
  new(
    model: params[:model].presence,
    time: TIMES.key?(params[:time]) ? params[:time] : DEFAULT_TIME,
    mode: MODES.include?(params[:mode]) ? params[:mode] : "all",
    kind: KINDS.include?(params[:kind]) ? params[:kind] : "all",
    actor: params[:actor].presence || "all",
    query: params[:q].to_s,
    page: [params[:page].to_i, 1].max,
    expanded: params[:expanded].presence
  )
end

Instance Method Details

#actor_filterObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/athar/dashboard/filter_set.rb', line 47

def actor_filter
  return nil if actor == "all"

  if actor == "anon"
    { kind: :anon }
  elsif actor.start_with?("user:")
    _, type, id = actor.split(":", 3)
    return nil if id.blank?

    { kind: :user, type:, id: }
  elsif actor.start_with?("sys:")
    { kind: :sys, name: actor.sub("sys:", "") }
  end
end

#time_cutoff(now = Time.current) ⇒ Object



42
43
44
45
# File 'lib/athar/dashboard/filter_set.rb', line 42

def time_cutoff(now = Time.current)
  delta = TIMES[time]
  delta ? now - delta : nil
end