Skip to content

Conversation

@harunkumars
Copy link
Owner

Fixes Issue activeadmin#3157 - Display Enum labels for Active Filters in Current Search Status

ActiveAdmin::Filters::ActiveFilter#values controls the displayed value for active filters.
This currently does not have any special handling for Enums.

For e.g. with an enum definition like enum status: [:inactive, :active] , when filtering for :inactive status, the Current Search Status widget shows "Status is 0"

This PR, uses ActiveRecord::Enum.defined_enums to translate the display value for the filter to the associated label for enums. The Current Search Status widget will now show "Status is Inactive"

If a custom collection is provided when registering the ActiveAdmin resource e.g. filter :status, as: :select, collection: [[:not_active, 0], [:active, 1]] which specifies dropdown labels that are different from the enum labels, then the dropdown label from the custom collection is used. The Current Search Status widget will now show "Status is Not Active"

@javierjulio javierjulio force-pushed the 3157-use-enum-labels-in-current-filters-search-status branch 4 times, most recently from ff3c597 to 0221992 Compare March 19, 2023 01:39
@npgretz
Copy link

npgretz commented Mar 6, 2024

Just wanted to add this is very much still a desired feature for me! I explained another work around with Enumerize in my comment on this closed thread: activeadmin#3157 (comment)

@harunkumars harunkumars force-pushed the 3157-use-enum-labels-in-current-filters-search-status branch from 0221992 to 396d75b Compare June 27, 2025 04:13
@harunkumars harunkumars requested a review from Copilot July 2, 2025 04:12
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR ensures that ActiveAdmin’s Current Search Status widget shows human-readable enum labels instead of raw enum values (and respects any custom filter collections).

  • Enhanced ActiveFilter#values to detect enums and map values to their labels (with fallbacks for custom collections).
  • Added unit tests around enum label display (including custom array/hash collections and multi-selection).
  • Updated test model, migration, and ActiveAdmin registration templates to include review_status and reject_reason enums.

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
spec/unit/filters/active_filter_spec.rb New specs for enum label resolution and custom collection fallback
spec/support/templates/models/store.rb Added review_status and reject_reason enum definitions
spec/support/templates/migrations/create_stores.tt Added integer columns for new enums in test migration
spec/support/templates/admin/stores.rb Permits and filters by the new enum attributes in ActiveAdmin
lib/active_admin/filters/active_filter.rb Updated values logic to humanize enum and custom collection values
Comments suppressed due to low confidence (1)

spec/unit/filters/active_filter_spec.rb:308

  • Add a similar multi-selection spec for native enum filters (e.g., review_status_in) to ensure multiple enum values are humanized correctly.
    context "filtering by multiple selections" do

end

def enum_attribute
resource_class.defined_enums[name]
Copy link

Copilot AI Jul 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defined_enums keys are strings, but name is a symbol. Change to resource_class.defined_enums[name.to_s] so enum lookups actually fire.

Suggested change
resource_class.defined_enums[name]
resource_class.defined_enums[name.to_s]

Copilot uses AI. Check for mistakes.
Comment on lines +130 to +133
condition_values.map do |value|
# value(filter form's search params) will be a string,
# so let us transform the lookup object (e.g. for integer backed enums)
(hash.transform_values(&:to_s).key(value) || value).to_s.humanize
Copy link

Copilot AI Jul 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider moving hash.transform_values(&:to_s) outside the loop to avoid repeated transformations when mapping multiple values.

Suggested change
condition_values.map do |value|
# value(filter form's search params) will be a string,
# so let us transform the lookup object (e.g. for integer backed enums)
(hash.transform_values(&:to_s).key(value) || value).to_s.humanize
transformed_hash = hash.transform_values(&:to_s)
condition_values.map do |value|
# value(filter form's search params) will be a string,
# so let us transform the lookup object (e.g. for integer backed enums)
(transformed_hash.key(value) || value).to_s.humanize

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants