Class: SourceMonitor::SourcesFilterPresenter

Inherits:
Object
  • Object
show all
Defined in:
app/presenters/source_monitor/sources_filter_presenter.rb

Overview

Encapsulates filter state logic for the sources index view. Extracts filter detection, label generation, and active-filter tracking from the template into a testable object.

Constant Summary collapse

%w[
  active_eq
  health_status_eq
  feed_format_eq
  scraper_adapter_eq
  scraping_enabled_eq
  avg_feed_words_lt
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(search_params:, search_term:, fetch_interval_filter:, adapter_options: []) ⇒ SourcesFilterPresenter

Returns a new instance of SourcesFilterPresenter.

Parameters:

  • search_params (Hash)

    sanitized Ransack search params

  • search_term (String)

    current text search term

  • fetch_interval_filter (Hash, nil)

    active fetch interval filter

  • adapter_options (Array<String>) (defaults to: [])

    distinct scraper adapter values



23
24
25
26
27
28
# File 'app/presenters/source_monitor/sources_filter_presenter.rb', line 23

def initialize(search_params:, search_term:, fetch_interval_filter:, adapter_options: [])
  @search_params = search_params || {}
  @search_term = search_term.to_s
  @fetch_interval_filter = fetch_interval_filter
  @adapter_options = adapter_options
end

Instance Attribute Details

#adapter_optionsObject (readonly)

Returns the value of attribute adapter_options.



17
18
19
# File 'app/presenters/source_monitor/sources_filter_presenter.rb', line 17

def adapter_options
  @adapter_options
end

#fetch_interval_filterObject (readonly)

Returns the value of attribute fetch_interval_filter.



17
18
19
# File 'app/presenters/source_monitor/sources_filter_presenter.rb', line 17

def fetch_interval_filter
  @fetch_interval_filter
end

#search_paramsObject (readonly)

Returns the value of attribute search_params.



17
18
19
# File 'app/presenters/source_monitor/sources_filter_presenter.rb', line 17

def search_params
  @search_params
end

#search_termObject (readonly)

Returns the value of attribute search_term.



17
18
19
# File 'app/presenters/source_monitor/sources_filter_presenter.rb', line 17

def search_term
  @search_term
end

Instance Method Details

#active_filter_keysObject

Returns the subset of DROPDOWN_FILTER_KEYS that have present values



35
36
37
# File 'app/presenters/source_monitor/sources_filter_presenter.rb', line 35

def active_filter_keys
  DROPDOWN_FILTER_KEYS.select { |k| @search_params[k].present? }
end

#filter_labelsObject

Returns a hash mapping active filter keys to human-readable labels



40
41
42
43
44
45
46
47
48
49
# File 'app/presenters/source_monitor/sources_filter_presenter.rb', line 40

def filter_labels
  {
    "active_eq" => status_label,
    "health_status_eq" => "Health: #{@search_params['health_status_eq']&.titleize}",
    "feed_format_eq" => "Format: #{@search_params['feed_format_eq']&.upcase}",
    "scraper_adapter_eq" => "Adapter: #{@search_params['scraper_adapter_eq']&.titleize}",
    "scraping_enabled_eq" => scraping_label,
    "avg_feed_words_lt" => "Avg Feed Words: < #{@search_params['avg_feed_words_lt']}"
  }
end

#has_any_filter?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'app/presenters/source_monitor/sources_filter_presenter.rb', line 30

def has_any_filter?
  @search_term.present? || @fetch_interval_filter.present? || active_filter_keys.any?
end