diff --git a/Gemfile b/Gemfile index a492f25..34cdaf1 100644 --- a/Gemfile +++ b/Gemfile @@ -27,7 +27,7 @@ group :development, :test do gem 'rspec-expectations' # Drives the dummy app in the tests of active_admin_theme/page_objects. Not on # RubyGems yet, so pinned to its tag. - gem 'active_admin_page_objects', github: 'activeadmin-plugins/active_admin_page_objects', tag: 'v2.0.0.beta2' + gem 'active_admin_page_objects', github: 'activeadmin-plugins/active_admin_page_objects', tag: 'v2.0.0.beta3' # Tests the searchable select with its None and Present filter options. Not on RubyGems, so pinned to its tag. gem 'active_admin_filter_extensions', github: 'activeadmin-plugins/active_admin_filter_extensions', tag: 'v1.3.0' end diff --git a/README.md b/README.md index 00da90f..4ebec78 100644 --- a/README.md +++ b/README.md @@ -132,6 +132,7 @@ ActiveAdmin pages import the modules listed in `ActiveAdminTheme.javascripts`. W active_admin_theme/tabs active_admin_theme/dropdown_menu active_admin_theme/table_crisscross + active_admin_theme/column_chooser active_admin_theme/searchable_select active_admin_theme/confirm_dialog active_admin_theme/panel_toggle @@ -366,7 +367,9 @@ The dialog has a title bar. Its buttons sit at the left, the confirm button firs Field types are `:string`, `:text`, `:number`, `:date`, `:select`, and `:boolean`. More options: - `scope:` nests every field name, so `reason` becomes `reject[reason]`, and `meta[source]` becomes `reject[meta][source]`. -- A select field takes `multiple: true`. It sends `name[]`, and `value:` is an array of the selected values. +- A select field takes `multiple: true`. It sends `name[]`, and `value:` is an array of the selected values. `include_blank: false` drops the empty first option. +- A boolean field sends `"1"` when ticked and `"0"` when not. `checked_value:` and `unchecked_value:` change them, and `unchecked_value: nil` sends nothing. +- `form_html:` adds attributes to the dialog's form. - A field takes `input_html:`, attributes for its control, such as `{min: 0, step: "0.01"}` or `{maxlength: 20}`. The attributes the component sets, such as `name` and `id`, win, and `class` is joined. - `params:` adds hidden fields. Nested hashes and arrays become Rails' bracketed names. - `class:` adds classes to the button, and `trigger_html:` other attributes. Inside the action items the button looks like the other action items. @@ -394,6 +397,53 @@ document.addEventListener("aat:confirm-dialog:open", ({detail: {dialog, trigger} }) ``` +### Batch action forms + +ActiveAdmin 4 dropped the `form:` option of `batch_action`, which asked for values in a dialog before the action ran. The theme brings it back in the ActiveAdmin 3 format, so these actions need no change: + +```ruby +batch_action :flag, confirm: "Flag the selected orders?", form: { + "Reason" => :text, + notes: :textarea, + count: :number, + due_on: :datepicker, + notify: :checkbox, + kind: [["Fraud", "fraud"], ["Other", "other"]] +} do |ids, inputs| + # inputs => {"Reason" => "Spam", "notes" => "", "count" => "3", "due_on" => "2026-12-24", "notify" => "on", "kind" => "fraud"} +end + +batch_action :set_status, form: -> { {status: Order::STATUSES} } do |ids, inputs| +end +``` + +The fields open in a `confirm_dialog` titled like the menu item, and `confirm:` becomes its message. The types are `text`, `number`, `textarea`, `datepicker` (a native date input that sends `YYYY-MM-DD`) and `checkbox`, plus an Array for a select with no blank option. As in ActiveAdmin 3, each key is both the input name and the label with its first letter capitalized, a ticked checkbox sends `"on"` and an unticked one sends nothing. A Proc runs in the view. ActiveAdmin 4's own batch script submits the dialog, so `partial:` actions keep working beside these. + +The dialog opens from the `active_admin_theme/confirm_dialog` script, which loads by default. Before it opens, the menu item dispatches a bubbling, cancelable `aat:batch-form:open` event with `detail` `{action, ids, dialog, open}`. To check the selection on the server first, cancel the event and call `open()` once the check passes: + +```js +document.addEventListener("aat:batch-form:open", async (event) => { + if (event.detail.action !== "flag") return + event.preventDefault() + const response = await fetch(`${location.pathname}/flag_validation?selected_ids=${event.detail.ids.join(",")}`) + if (response.ok) event.detail.open() +}) +``` + +After it opens, the dialog dispatches `aat:confirm-dialog:open` with `detail` `{dialog, trigger}`, where `trigger.dataset.action` names the batch action. + +### `ActiveAdmin.ModalDialog` + +The opt-in `active_admin_theme/modal_dialog` script brings back ActiveAdmin 3's `ActiveAdmin.ModalDialog(message, inputs, callback)`, which ActiveAdmin 4 dropped with jQuery UI. Add it to `ActiveAdminTheme.javascripts`. It sets `window.ActiveAdmin.ModalDialog` and is also the module's default export. + +```js +ActiveAdmin.ModalDialog("Complete order?", {"Billing Cycles": "number", Period: [["Month", "1m"], ["Year", "1y"]], Refill: "checkbox"}, (inputs) => { + // inputs => {"Billing Cycles": "3", "Period": "1y", "Refill": "on"} +}) +``` + +The input types are those of [batch action forms](#batch-action-forms), and `{}` asks for confirmation only. The dialog has the `confirm_dialog` look, with **Confirm** and **Cancel** buttons. It is removed when it closes, and the function returns its form. Before and after it opens, the form dispatches bubbling `aat:modal-dialog:before-open` and `aat:modal-dialog:open` events with `detail` `{form, title}`. With jQuery on the page, `body` also gets ActiveAdmin 3's `modal_dialog:before_open` and `modal_dialog:after_open` jQuery events, with the form as their argument. + ### `lazy_load` `lazy_load url: path` is a placeholder that fetches an HTML fragment when it scrolls into view (a hidden tab loads when opened) and replaces its content with it. Needs the opt-in `active_admin_theme/lazy_load` script. @@ -525,6 +575,24 @@ ActiveAdminTheme.row_click_selects = false The setting needs the `active_admin_theme/table_crisscross` script, which is on by default. +### Columns chooser + +A columns button, an icon at the right of the table toolbar, opens a menu that hides index columns, and the browser remembers the choice for each index in `localStorage`. The selection column, the id column and columns without a title always show. It is off by default. To turn it on for every index, set this in an initializer: + +```ruby +ActiveAdminTheme.column_chooser = true +``` + +The `column_chooser:` option of `index` turns it on or off for one index: + +```ruby +index column_chooser: true do + # columns +end +``` + +The button's tooltip and accessible name, "Columns", come from `active_admin_theme.column_chooser.label` in your locale files. + ### Sticky first column Add the `with-sticky-rows` class to a wide table, and its first column stays in place while the table scrolls sideways: @@ -577,6 +645,17 @@ f.input :kind, as: :select, include_blank: "None", tom_select: {allowEmptyOption f.input :tags, as: :select, multiple: true, tom_select: {addPlugins: ["checkbox_options"], removePlugins: ["remove_button"]} ``` +The hash also takes three switches of the theme's own: + +- `sortable: true` lets you drag the items of a multiple select. The form sends the values in the order of the items, and on page load the items follow the order of the selected options, so list the saved order first. It uses Tom Select's `drag_drop` plugin, which needs no jQuery; touch screens may not drag. +- `clear: false` drops the remove button of a single select. +- `search: false` drops the search box. + +```ruby +f.input :codec_ids, as: :select, multiple: true, collection: codecs_in_saved_order, tom_select: {sortable: true} +f.input :kind, as: :select, collection: kinds, tom_select: {clear: false, search: false} +``` + The option renders the `data-tom-select` attribute, which you can also write yourself: `"false"`, `"true"` or the settings as JSON. ```ruby @@ -588,6 +667,7 @@ select_tag :color, options_for_select(colors), data: {tom_select: {create: true} ```ruby ActiveAdminTheme.searchable_select_all = true # upgrade every select ActiveAdminTheme.searchable_select_min_chars = 3 # characters to type before an AJAX select searches +ActiveAdminTheme.searchable_select_search_min_options = 5 # fewer options show no search box; nil (default) always shows it ``` The texts come from I18n, with these defaults: @@ -635,6 +715,8 @@ By default it searches once three characters are typed, loads the next page as t | `depends_param:` | `depends_param: "q[region_id_eq]"` | Query param that carries the parent's value, `q[_eq]` by default. With several parents, a Hash of parent to param: `depends_param: {country_id: "q[country_id_in]", insured: "insured"}`. A String raises `ArgumentError`. | | `depends_placeholder:` | `depends_placeholder: "Select a country first"` | Text shown while the parent is empty. | | `depends_optional:` | `depends_optional: true` | The select works with an empty parent, which sends nothing, instead of waiting for it. | +| `depends_empty_param:` | `depends_empty_param: "q[country_id_null]"` | The select works with an empty parent, and sends this param as `true` instead. With several parents, a Hash of parent to param. | +| `depends_keep_value:` | `depends_keep_value: true` | When the parent changes, the select keeps its value if the new options still have it. Needs `load_all: true`. | | `auto_select_single:` | `auto_select_single: true` | With `load_all:`, selects the option when the endpoint offers only one and nothing is selected. | | `resource:` | `resource: Account` | Model or ActiveAdmin resource that has the endpoint, when the input is not an association. | | `options_name:` | `options_name: :external_id` | Which `searchable_select_options` of the resource to use, `:all` by default. | @@ -687,6 +769,8 @@ end | `id_search:` | off | `id_search: true` finds `id:123`; `id_search: "#"` finds `#123` | | `additional_payload:` | none | `additional_payload: ->(customer) { {email: customer.email} }` | +A payload key `attrs`, a Hash of attribute name to value, becomes attributes of the `