Skip to main content

Filters

filter adds a filter to the filter bar of an index page or a dashboard.

Quick example​

index do
filter %i[name email], :search, placeholder: "Search"
filter :status, :multi_select, helper_method: :select_options_for_status
filter :user_id, :single_select, helper_method: :select_options_for_user
filter :created_at, :date
filter :age, :range
filter :email, :string
end

Options​

filter(db_column_name, filter_type = nil, options = {})
Filter typeMatches
:searchText search across one or more columns. The only type that accepts an array of columns.
:single_selectOne option. Defaults to the model's enum values when the column is an enum.
:multi_selectAny of the selected options. Used automatically when filter_type is omitted on an enum column.
:dateA date range.
:rangeA numeric range.
:stringA text value with operators (contains, is, …).
NameTypeDefaultDescription
placeholderStringdepends on typePlaceholder text.
display_nameStringColumn name, titleizedLabel of the filter.
helper_methodSymbolnilMethod in Catalyst::CustomHelper, called with no arguments, returning the select options. Use the built-in :ct_boolean_filter_options for Yes/No filters; enum columns get their options automatically.
fetch_configHash, ProcnilFetch select options remotely from a model scope as the user types. See Remote options.
filter_withSymbolnilModel scope that applies the filter. See Custom filter method.
active_by_defaultBooleannilShow the filter in the bar without the user adding it.
default_valueProcnilLambda returning the value applied when the URL has no filter params.
allow_select_all_optionBooleantrueShow a (Select All) option in :multi_select filters. UI only: the selected values are sent.
allow_empty_optionBooleantrue (false with filter_with)Show an (Empty) option in select filters.
display_ifProc->(_arg) { true }Lambda returning whether to show the filter.
note

The collection option was removed. Passing it raises an ArgumentError. Return the options from a helper method instead, so models are not loaded when filters are defined.

Recipes​

Default filter values​

filter :status, :single_select, helper_method: :select_options_for_status, default_value: -> { "draft" }
filter :category, :multi_select, helper_method: :select_options_for_category, default_value: -> { ["A", "B"] }
filter :created_at, :date, default_value: -> { "#{30.days.ago.to_date} to #{Date.today}" }
filter :user_id, :single_select, helper_method: :user_collection, default_value: -> { Current.user.id }

Options from a helper​

filter :user_id, :single_select, helper_method: :select_options_for_user
app/helpers/catalyst/custom_helper.rb
def select_options_for_user
User.order(:first_name).map { |user| [user.full_name, user.id] }
end

Remote options​

fetch_config queries a scope on another model for the options, so large tables don't have to render every option up front:

filter :user_id, :single_select, fetch_config: { model: "User", scope_name: "catalyst_search" }
app/models/user.rb
scope :catalyst_search, ->(search:) {
where("first_name ILIKE :q OR last_name ILIKE :q OR email ILIKE :q", q: "%#{search}%")
}

dependent_fields names other filters' db_column_names; the scope gets one keyword per entry holding the other filter's current selection β€” a scalar for :single_select, an array for :multi_select β€” and when it changes this filter clears its selection and reloads the options:

filter :state_id, :single_select, fetch_config: { model: "State", scope_name: "catalyst_search" }
filter :city_id, :single_select, fetch_config: { model: "City", scope_name: "catalyst_search",
dependent_fields: [:state_id] }
app/models/city.rb
scope :catalyst_search, ->(search:, state_id: nil) {
cities = where("name ILIKE ?", "%#{search}%")
state_id.present? ? cities.where(state_id:) : cities.none
}

On filters the scope receives search: plus the dependent selections β€” no object_class:/object_id: (those are form-field only). Typed text is also applied through the fetch model's index :search filters; override_search: true leaves matching to the scope alone.

A lambda returns the config per user β€” it is called with Current.user:

filter :user_id, :single_select,
fetch_config: ->(user) { { model: "User", scope_name: user.admin? ? "catalyst_search" : "catalyst_search_team" } }

See Remote options for all the config keys.

Show filters without the user adding them​

active_by_default pins the filter into the bar on first load:

filter :status, :multi_select, active_by_default: true
filter :user_id, :multi_select, helper_method: :select_options_for_user,
display_name: "Leave Applicant", active_by_default: true

Ranges and dates​

filter :salary, :range # numeric min/max inputs
filter :start_date, :date # date range picker
filter :reason, :string, placeholder: "Search reason"

Date bounds cover whole days: the from date starts at 00:00 and the to date runs through 23:59, so filter :created_at, :date with 2024-06-15 to 2024-06-16 includes records created at any time on the 16th. Bounds that already carry a time are used as given.

Custom filter logic​

filter :status, :single_select, helper_method: :select_options_for_status, filter_with: :by_ticket_status
filter :currently_on_leave, :single_select, helper_method: :ct_boolean_filter_options,
filter_with: :currently_on_leave

See Custom filter method for the scope signature.