diff --git a/README.md b/README.md index 744264c..3717c08 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,9 @@ filter :created_at, as: :date_time_range, floors on two-ended filters. * [`message:`](#value-policies-sanitize-and-validate) and plain-i18n defaults for every violation text. +* [`none:` / `present:`](#none-and-present-options) - None and Present options on a select + filter, and None and Present predicates on a string or numeric one, for the records whose + attribute is empty and the ones where it is not. * [`default_filters_notice`](#telling-the-admin-why) - the flash that tells the admin why the page shows a slice; [`require_any_filter`](#resource-level-guard) - an index that waits to be asked. @@ -336,6 +339,138 @@ lands in `flash[:error]` (and rides the redirect), and the filter's declared `de any - refills through the ordinary merge. The defaults notice stays quiet on that render: the error already explains why the page shows the default slice. +## None and Present options + +A select filter cannot ask for the records whose attribute is empty, or for the ones where it +holds anything at all: no value of its collection means either. `none:` and `present:` add the +options, first in the list after the blank one, None before Present: + +```ruby +filter :account, as: :select, collection: proc { Account.order(:name) }, + extensions: { none: true, present: true } +filter :status, as: :select, multiple: true, collection: %w[draft published], + extensions: { none: "No status" } # the option's label +``` + +`true` labels the option from the i18n keys `active_admin_filter_extensions.none` ("None") and +`active_admin_filter_extensions.present` ("Present"); a String is the label itself. None +searches `_null` on the filter's attribute, Present searches `_not_null`. On a multiple select: + +* None beside other values searches "any of these, or empty". +* Present beside other values searches Present alone: every value is a present one. +* None and Present together search every record, so no condition is applied. + +The select submits a marker under its own key - `q[account_id_eq]=__none__` or +`q[account_id_eq]=__present__` - and only the search is handed the condition. The URL, the form +and the other extensions read the request as submitted: `required:` counts either option as an +answer, `sanitize:` and `validate:` are not handed the marker, and +`default: -> { ActiveAdminFilterExtensions.none_value }` opens the index on the empty records. +The Current Filters panel shows the label: "Account equals None", "Account equals Present". +Move a marker where a real value could spell the same thing: + +```ruby +ActiveAdminFilterExtensions.none_value = "__empty__" +ActiveAdminFilterExtensions.present_value = "__filled__" +``` + +### On a string or numeric filter + +A `:string` or `:numeric` filter that picks its predicate from a dropdown takes the same two +options, as two more predicates closing the list: + +```ruby +filter :title, extensions: { none: true, present: true } +filter :position, as: :numeric, extensions: { none: "No position" } +``` + +A string filter searches `_blank` and `_present`, so an empty string counts as none; a numeric +one searches `_null` and `_not_null`. These are Ransack's own predicates and the URL says them +as they are searched: `q[title_blank]=1`. `required:` counts them as an answer, `sanitize:` and +`validate:` are not handed the `1`, and the Current Filters panel reads "Title equals None". +A default such as `default: { blank: "1" }` is not handed to them either. + +So the same string column splits differently on a select and in the dropdown. Ransack does not +trim, so a value of only spaces counts as present: + +| `title` | Select None (`_null`) | Select Present (`_not_null`) | Dropdown None (`_blank`) | Dropdown Present (`_present`) | +| --- | --- | --- | --- | --- | +| `"john"` | | yes | | yes | +| `""` | | yes | yes | | +| `" "` | | yes | | yes | +| `NULL` | yes | | yes | | + +Such a predicate has nothing to type, and Active Admin leaves an empty box out of the submit. +The gem's script fills the box and hides it. Without the script the two predicates filter +nothing, so load it on every page with a filters form. + +Active Admin 3 (Sprockets), in `app/assets/javascripts/active_admin.js`: + +```javascript +//= require active_admin_filter_extensions +``` + +Active Admin 4 (import map): the gem pins the module `active_admin_filter_extensions` into +`ActiveAdmin.importmap`. If your Active Admin has the `javascript_modules` setting, the gem adds +itself to it and pages import it automatically. Otherwise override +`app/views/active_admin/_html_head.html.erb` (copy it from Active Admin) and add after the +`javascript_importmap_tags` line: + +```erb +<%= javascript_import_module_tag "active_admin_filter_extensions" %> +``` + +If you use active_admin_theme 4, add the module to its list in an initializer instead: + +```ruby +ActiveAdminTheme.javascripts += %w[active_admin_filter_extensions] +``` + +The script listens on `document`, so it does not depend on the order it is loaded in. For a +filters form the page inserts on its own, call `ActiveAdminFilterExtensions.sync(form)`. + +Any other filter raises on `none:` or `present:`: check boxes, a date range, a select that +submits `_not_eq` or `_not_in`, a string filter named after its predicate (`filter :title_cont`). + +### Styling the options + +The gem ships no CSS. Every option it adds, to a select or to a predicate dropdown, carries +classes and a data attribute to style: + +```html + + +``` + +A Hash gives the option classes of its own beside the label, appended to the gem's: + +```ruby +filter :account, as: :select, + extensions: { none: { label: "No account", class: "text-red-700 italic" }, + present: { class: "font-bold" } } +``` + +```css +.filter-extensions-none { font-style: italic; } +[data-filter-extensions-option] { color: #b91c1c; } +``` + +A browser styles an `