Skip to content

Add a None option to select filters - #11

Merged
senid231 merged 5 commits into
mainfrom
none-filter-option
Dec 1, 2026
Merged

senid231 merged 5 commits into
mainfrom
none-filter-option

Conversation

@senid231

@senid231 senid231 commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

Adds "None" and "Present" choices to filters, for records whose attribute is empty or set:

filter :account, as: :select, extensions: { none: true, present: true }   # labels from i18n
filter :status, as: :select, multiple: true, extensions: { none: { label: "No status", class: "italic" } }
filter :title, extensions: { none: true, present: "Any title" }            # predicate dropdown
filter :position, as: :numeric, extensions: { none: true }

A select submits a marker (__none__ or __present__) under its own key. Only the search sees it, as _null or _not_null. Defaults, required:, the policies and the redirect read the request as submitted. Each marker option carries filter-extensions-option filter-extensions-none|present and data-filter-extensions-option, plus any class: given, for styling a plain select or a Tom Select.

Decisions a reviewer can't see in the diff:

  • None beside other values is a Ransack OR grouping. Present beside values searches _not_null only. None plus Present searches no condition.
  • String and numeric filters get the choices in Active Admin's predicate dropdown (_blank/_present, _null/_not_null). Active Admin submits no empty box, so the gem now ships a small script that fills the box with 1 and hides it. Apps add it with //= require active_admin_filter_extensions on Active Admin 3. On Active Admin 4 it is pinned into ActiveAdmin.importmap.
  • Other filters with none: or present: raise ArgumentError.
  • Current Filters reads "Account equals None" and "Account equals Present".

Verified: the suite passes on Active Admin 3.5 and 4.0.0.beta23 (31 unit and 246 integration examples each).

extensions: { none: true } adds a None choice that searches the attribute's _null. Only the search sees the rewrite, so defaults, policies and the redirect still read the request as submitted.

Co-Authored-By: Clanker
@senid231

senid231 commented Nov 28, 2026 •

Copy link
Copy Markdown
Member Author

add _not_null in same way with default visible value "Present" customized via extensions: { present: "Present" }

current filters should read it as "Account equals Present"

@senid231

Copy link
Copy Markdown
Member Author

also should be possible for none and has_any options to set custom styles (for raw select and for tomselect as well)

@senid231 senid231 self-assigned this Nov 28, 2026
@senid231

Copy link
Copy Markdown
Member Author

also user should be able to add none / present predicates for string / numeric filters with predicate

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Restoring false-valued predicates overwrites their answers with 1 in both Ruby and JavaScript, so resubmitting the form can silently reverse the search.

1 open issue
What changed in this PR

Adds None and Present choices to the gem’s Active Admin filters, integrating empty/set searches with existing defaults, policies, and Current Filters labels.

Changes:

  • Adds select markers and string/numeric predicates, with configurable labels and styling hooks.
  • Adds JavaScript and asset integration to submit predicates that require no typed value.
  • Documents setup and adds unit, integration, and browser coverage.
File Description
spec/​support/​rails_template.rb Adds filter fixtures, nullable associations, and script loading.
spec/​spec_helper.rb Loads marker support into unit-test helpers.
spec/​marker_options_spec.rb Tests public marker rewriting behavior.
spec/​integration/​present_option_spec.rb Covers Present selections, policies, and labels.
spec/​integration/​predicate_option_spec.rb Covers predicate dropdowns and browser submission.
spec/​integration/​none_option_spec.rb Covers None selections, defaults, and grouped searches.
README.md Documents options, script setup, styling, and dependent selects.
lib/​active_admin_filter_extensions/​search_keys.rb Allows input construction with a supplied form builder.
lib/​active_admin_filter_extensions/​policies.rb Exempts declared select markers from value policies.
lib/​active_admin_filter_extensions/​marker_options.rb Implements marker rewriting, configuration validation, and form options.
lib/​active_admin_filter_extensions/​filters_form.rb Adds predicate rendering and Current Filters labels.
lib/​active_admin_filter_extensions/​filter_options.rb Registers the new extension keys.
lib/​active_admin_filter_extensions/​filter_defaults.rb Exempts default marker values from value policies.
lib/​active_admin_filter_extensions/​engine.rb Integrates script assets and Active Admin’s import map.
lib/​active_admin_filter_extensions/​data_access.rb Rewrites markers before searching and attaches labels.
lib/​active_admin_filter_extensions.rb Exposes marker configuration and installs integrations.
config/​locales/​en.yml Adds default English option labels.
config/​importmap.rb Pins the JavaScript module.
app/​assets/​javascripts/​active_admin_filter_extensions.js Synchronizes predicate input names, values, and visibility.

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Default None/Present predicates still undergo attribute value validation, causing valid defaults to raise ArgumentError on a bare visit even though equivalent submitted predicates bypass validation.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Skip None/Present predicates during default sanitization and validation

lib/​active_admin_filter_extensions/​filter_defaults.rb:206

A None/Present predicate used as a default still reaches the attribute's value policies because this exemption only recognizes select markers. For example, filter :title, extensions: { none: true, default: { blank: "1" }, validate: ->(v) { "Too short" if v.size < 3 } } raises ArgumentError on a bare visit when the validator receives "1", although submitting title_blank=1 bypasses it. Skip declared None/Present predicate keys before default sanitization and validation too.

1 resolved since last review

Review effort: Balanced

A submitted title_blank=1 is not handed to validate: or sanitize:, but
the same predicate as a default was, and a strict validator raised at
boot. Specs and a README table now show which of "john", "", " " and
NULL None and Present return on a select and in the dropdown.

Co-Authored-By: Clanker
@senid231

senid231 commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

The Copilot finding from the latest review is fixed in 0e250af: a default None or Present predicate went through sanitize: and validate:. Defaults now skip the same boolean predicates that a submitted request skips, unless the filter derives that key itself. So default: { blank: "1" } with a strict validate: no longer raises. A new spec fails without the fix. The same rule also covers older defaults such as default: { null: true }, which failed the same way before this PR.

Both suites pass on Active Admin 3.5 and 4.0.0.beta23: 31 unit and 246 integration examples each.

@senid231
senid231 merged commit 02a8cc5 into main Dec 1, 2026
12 checks passed
@senid231
senid231 deleted the none-filter-option branch December 1, 2026 17:58
senid231 added a commit that referenced this pull request Dec 1, 2026
Minor: #9 adds Active Admin 4 support and keeps Active Admin 3
unchanged, apart from hiding the empty Current Filters panel after a
blank submit. #11 adds opt-in None and Present options. Their string
and numeric predicates need the gem's script, which Active Admin 3
apps add with `//= require active_admin_filter_extensions`.

Co-Authored-By: Clanker
@senid231 senid231 mentioned this pull request Dec 1, 2026
senid231 added a commit that referenced this pull request Dec 1, 2026
Minor: #9 adds Active Admin 4 support and keeps Active Admin 3
unchanged, apart from hiding the empty Current Filters panel after a
blank submit. #11 adds opt-in None and Present options. Their string
and numeric predicates need the gem's script, which Active Admin 3
apps add with `//= require active_admin_filter_extensions`.

Co-Authored-By: Clanker
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.

2 participants