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 type | Matches |
|---|---|
:search | Text search across one or more columns. The only type that accepts an array of columns. |
:single_select | One option. Defaults to the model's enum values when the column is an enum. |
:multi_select | Any of the selected options. Used automatically when filter_type is omitted on an enum column. |
:date | A date range. |
:range | A numeric range. |
:string | A text value with operators (contains, is, β¦). |
| Name | Type | Default | Description |
|---|---|---|---|
placeholder | String | depends on type | Placeholder text. |
display_name | String | Column name, titleized | Label of the filter. |
helper_method | Symbol | nil | Method 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_config | Hash, Proc | nil | Fetch select options remotely from a model scope as the user types. See Remote options. |
filter_with | Symbol | nil | Model scope that applies the filter. See Custom filter method. |
active_by_default | Boolean | nil | Show the filter in the bar without the user adding it. |
default_value | Proc | nil | Lambda returning the value applied when the URL has no filter params. |
allow_select_all_option | Boolean | true | Show a (Select All) option in :multi_select filters. UI only: the selected values are sent. |
allow_empty_option | Boolean | true (false with filter_with) | Show an (Empty) option in select filters. |
display_if | Proc | ->(_arg) { true } | Lambda returning whether to show the filter. |
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
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" }
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] }
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.
Relatedβ
- Advanced filters: nested AND/OR conditions with operators
- Saved views: save filters per user
- Global search