diff --git a/CHANGELOG.md b/CHANGELOG.md index bacb2a8..72fedb1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,18 @@ # Changelog -## Unreleased +## 1.1.0 (2026-12-01) ### Added +- `SelectFilter#select_option` and `Form::SelectField#fill` take `search:`, the text a select widget types when it differs from the option label. It reaches `SelectControl#choose_option` only when given, so a widget module that defines `choose_option(scope, label)` keeps working; the native `` for `SelectFilter` and `Form::Field`. For a widget on some selects, declare your own class: see [Hidden selects](#hidden-selects). A gem that renders a JavaScript select widget for every select prepends a module into `SelectControl` instead. It overrides the private methods `choose_option(scope, label)`, `option_offered?(scope, label)`, `control_disabled?(scope)` and `chosen_item?(scope, label)`, and `native_select_visibility` for the methods that still read the hidden `` for `SelectFilter` and `Form::Field`. For a widget on some selects, declare your own class: see [Hidden selects](#hidden-selects). A gem that renders a JavaScript select widget for every select prepends a module into `SelectControl` instead. It overrides the private methods `choose_option(scope, label, search: nil)`, `option_offered?(scope, label)`, `control_disabled?(scope)` and `chosen_item?(scope, label)`, and `native_select_visibility` for the methods that still read the hidden `` ignores it. + ## Themes and plugins A theme or plugin ships its own page objects in its own repository, with one entry point. The rule: diff --git a/lib/active_admin_page_objects/batch_actions.rb b/lib/active_admin_page_objects/batch_actions.rb index 0f938b8..ef407d8 100644 --- a/lib/active_admin_page_objects/batch_actions.rb +++ b/lib/active_admin_page_objects/batch_actions.rb @@ -20,13 +20,23 @@ def select_all # dialog by field name (`status: "pending"` picks the option of a select, # or types into an input) and clicks OK. def apply(label, inputs = {}) - button = find(:active_admin, :batch_actions_button) - # The button toggles the menu, so leave an open menu open. - button.click unless has_css?(sel(:batch_actions_menu), wait: false) - item = action_item(label) - opens_dialog = !item[:"data-confirm"].to_s.empty? - item.click - confirm_dialog(inputs) if opens_dialog + item = open_menu_item(label) + return item.click if item[:"data-confirm"].to_s.empty? + + dialog = click_for_dialog(item) + inputs.each { |name, value| dialog.fill(name, with: value) } + dialog.confirm + end + + # Opens the menu, clicks the action labelled or named +label+ and returns + # its modal dialog, unconfirmed, to check or fill it. + # @raise [ArgumentError] when the action has no `confirm:` or `form:` + # @return [ModalDialog] + def open_dialog(label) + item = open_menu_item(label) + raise ArgumentError, "batch action #{label.inspect} opens no dialog" if item[:"data-confirm"].to_s.empty? + + click_for_dialog(item) end # True when the menu has an action labelled +label+ or named +label+. @@ -41,11 +51,17 @@ def has_no_action?(label, **options) private + # The button toggles the menu, so leave an open menu open. + def open_menu_item(label) + button = find(:active_admin, :batch_actions_button) + button.click unless has_css?(sel(:batch_actions_menu), wait: false) + action_item(label) + end + # The dialog is appended to , outside this section. - def confirm_dialog(inputs) - dialog = ModalDialog.new(self, capybara_session.find(:active_admin, :modal_dialog)) - inputs.each { |name, value| dialog.fill(name, with: value) } - dialog.confirm + def click_for_dialog(item) + item.click + ModalDialog.new(self, capybara_session.find(:active_admin, :modal_dialog)) end def action_items diff --git a/lib/active_admin_page_objects/filters/select_filter.rb b/lib/active_admin_page_objects/filters/select_filter.rb index 9576823..dfe9a99 100644 --- a/lib/active_admin_page_objects/filters/select_filter.rb +++ b/lib/active_admin_page_objects/filters/select_filter.rb @@ -6,9 +6,10 @@ module ActiveAdminPageObjects class SelectFilter < Filter include SelectControl - # Chooses the option labelled +label+. - def select_option(label) - choose_option(root_element, label) + # Chooses the option labelled +label+. +search+ is the text a select + # widget types to find it; the native ignores it. + def fill(label, search: nil) + choose_option(root_element, label.to_s, **{ search: search }.compact) end # The option texts, without the blank option. @@ -16,14 +17,40 @@ def options option_texts(option_nodes) end - # The text of the selected option, or nil when the blank option is selected. + # The text of the selected option, or nil when the blank option is + # selected. A multiple select: the first one; see #selected_options. # @return [String, nil] def selected - option_texts(option_nodes.select(&:selected?)).first + selected_options.first + end + + # The texts of the selected options, for a multiple select. + # @return [Array] + def selected_options + option_texts(option_nodes.select(&:selected?)) + end + + # True when exactly +labels+ are selected, in any order: one label for a + # single select, several for a multiple select. + # @raise [ArgumentError] when no labels are given + def has_selected?(*labels) + selected_option?(root_element, selection(labels)) + end + + # Waits until the selected options are no longer +labels+. + # @raise [ArgumentError] when no labels are given + def has_no_selected?(*labels) + not_selected_option?(root_element, selection(labels)) end private + def selection(labels) + raise ArgumentError, "no option labels given" if labels.empty? + + labels.size == 1 ? labels.first : labels + end + def option_nodes native_select(root_element).all("option", visible: :all, wait: false) end diff --git a/lib/active_admin_page_objects/has_many.rb b/lib/active_admin_page_objects/has_many.rb index b9c16f2..1b73a00 100644 --- a/lib/active_admin_page_objects/has_many.rb +++ b/lib/active_admin_page_objects/has_many.rb @@ -37,6 +37,18 @@ def remove end end + # True when the fieldset has what #remove uses: the remove link of a + # new record or the delete checkbox of a saved one. + def has_remove?(**options) + has_css?(remove_selector, **options) + end + + # Waits until the fieldset has neither, as for a saved record of a + # has_many without `allow_destroy`. + def has_no_remove?(**options) + has_no_css?(remove_selector, **options) + end + # True when the delete checkbox of a saved record is ticked. def marked_for_destroy? has_css?(sel(:has_many_delete), wait: false) && find(:active_admin, :has_many_delete).checked? @@ -44,6 +56,10 @@ def marked_for_destroy? private + def remove_selector + "#{sel(:has_many_remove)}, #{sel(:has_many_delete)}" + end + # Formtastic ids a field ``, so the prefix of # `customer[orders_attributes][0][reference]` is `customer_orders_attributes_0`. # A check_boxes or multiple select name ends in `[]` after the attribute. diff --git a/lib/active_admin_page_objects/modal_dialog.rb b/lib/active_admin_page_objects/modal_dialog.rb index 64f3a4e..8e3d5b7 100644 --- a/lib/active_admin_page_objects/modal_dialog.rb +++ b/lib/active_admin_page_objects/modal_dialog.rb @@ -4,6 +4,8 @@ module ActiveAdminPageObjects # The modal dialog ActiveAdmin opens for batch actions that ask to confirm or # for input. Needs a JavaScript driver. Reached with `page.modal_dialog`. class ModalDialog < Section + include SelectControl + selector_key :modal_dialog # The dialog title text. @@ -12,12 +14,16 @@ def title find(:active_admin, :modal_dialog_title).text.strip end - # Sets the field named +name+: types into an input, or picks the option - # labelled +with+ in a select. + # Sets the field named +name+: types into an input, picks the option + # labelled +with+ in a select, or types a date (a Date or an ISO string) + # into a datepicker input. A select goes through SelectControl, so a + # select widget module drives it here too. def fill(name, with:) field = find(:active_admin, :modal_dialog_form).find(:css, field_selector(name)) if field.tag_name == "select" - field.select(with.to_s) + choose_option(field.find(:xpath, ".."), with.to_s) + elsif class_names(field).include?("datepicker") + type_date(field, iso_date(with)) else field.set(with) end diff --git a/lib/active_admin_page_objects/section.rb b/lib/active_admin_page_objects/section.rb index 645199c..ab3844d 100644 --- a/lib/active_admin_page_objects/section.rb +++ b/lib/active_admin_page_objects/section.rb @@ -48,11 +48,58 @@ def href_attribute(link) link[:href].to_s end + # Hides the datepicker and blurs its input, so no later focus event of the + # typing reopens it. True when jQuery UI was there to do it. + HIDE_DATEPICKER = <<~JS + (function() { + var $ = window.jQuery; + if (!$ || !$.datepicker) return false; + $.datepicker._hideDatepicker(); + var active = document.activeElement; + if (active && $(active).hasClass("hasDatepicker")) active.blur(); + return true; + })() + JS + + # True while jQuery shows the datepicker or runs one of its animations. + DATEPICKER_BUSY = <<~JS + (function() { + var $ = window.jQuery; + if (!$) return false; + var picker = $("#ui-datepicker-div"); + return picker.is(":visible") || picker.is(":animated"); + })() + JS + private_constant :HIDE_DATEPICKER, :DATEPICKER_BUSY + # Types +text+ into a date input. jQuery UI opens the datepicker when the - # input gets focus, over the controls below; Escape closes it. + # input gets focus, over the controls below. Its fade-out keeps it there + # for a moment after it closes, so wait until it is hidden. jQuery UI + # keeps the hidden node, so the lookups ask for a visible one whatever + # `Capybara.ignore_hidden_elements` says. def type_date(input, text) input.set(text) - input.send_keys(:escape) if class_names(input).include?("hasDatepicker") + return unless class_names(input).include?("hasDatepicker") + + close_datepicker(input.session) + wait_until_datepicker_closed(input.session) + end + + def wait_until_datepicker_closed(session) + session.document.assert_no_selector(:active_admin, :datepicker, visible: true) + session.document.synchronize do + raise Capybara::ExpectationNotMet, "the datepicker is still open" if session.evaluate_script(DATEPICKER_BUSY) + end + end + + # Closes the picker through jQuery UI. Escape is the fallback, sent only + # while the picker shows: once it is closed, Escape reaches an enclosing + # jQuery UI dialog and closes the dialog. + def close_datepicker(session) + return if session.evaluate_script(HIDE_DATEPICKER) + + picker = session.document.first(:active_admin, :datepicker, visible: true, minimum: 0, wait: false) + session.send_keys(:escape) if picker end # Retries the block until it is truthy or the wait time runs out. diff --git a/lib/active_admin_page_objects/select_control.rb b/lib/active_admin_page_objects/select_control.rb index 420282f..91f21f6 100644 --- a/lib/active_admin_page_objects/select_control.rb +++ b/lib/active_admin_page_objects/select_control.rb @@ -37,7 +37,9 @@ def native_select(scope) scope.find("select", **native_select_lookup) { |select| native_select_shown?(select) } end - def choose_option(scope, label) + # Picks +label+. A widget module reads +search+, the text to type when it + # differs from the label; callers pass it only when a spec gives one. + def choose_option(scope, label, search: nil) # rubocop:disable Lint/UnusedMethodArgument native_select(scope).select(label, **native_select_lookup) end diff --git a/lib/active_admin_page_objects/selectors.rb b/lib/active_admin_page_objects/selectors.rb index b644103..24e675e 100644 --- a/lib/active_admin_page_objects/selectors.rb +++ b/lib/active_admin_page_objects/selectors.rb @@ -37,6 +37,7 @@ module Selectors flash_alert: ".flash.flash_alert", flash_error: ".flash.flash_error", flash_warning: ".flash.flash_warning", + flash_of_type: ".flash.flash_%s", main_menu: "#header #tabs", main_menu_selected: "li.current", @@ -51,6 +52,7 @@ module Selectors table_row_by_id: 'tbody > tr[id$="_%s"]', table_cell: "td.col-%s", table_row_checkbox: "input.collection_selection", + table_footer: "tfoot > tr", blank_slate: ".blank_slate_container", attributes_table: ".attributes_table", @@ -77,6 +79,8 @@ module Selectors filter_value: "input", filter_range_from: 'input[name*="_gteq"]', filter_range_to: 'input[name*="_lteq"]', + # jQuery UI renders one datepicker for the page, at the end of . + datepicker: "#ui-datepicker-div", filters_footer: "form.filter_form .buttons", filters_submit: "input[type=submit]", filters_clear: "a.clear_filters_btn", diff --git a/lib/active_admin_page_objects/table.rb b/lib/active_admin_page_objects/table.rb index 7e24afd..aae133d 100644 --- a/lib/active_admin_page_objects/table.rb +++ b/lib/active_admin_page_objects/table.rb @@ -24,19 +24,13 @@ def column(key, label, visible: nil) SORT_DIRECTIONS = %i[asc desc].freeze - # One `` of the table body. `status_tag(:status)`, + # A `` whose cells are read by column key. `status_tag(:status)`, # `status_tags(:status)` and `has_status_tag?` read the cell of a column: # see StatusTag::Matchers. - class Row < Section + class BaseRow < Section include Declarations::Lookup include StatusTag::Matchers - # The record id from the row's `id="_"`. - # @return [String] - def record_id - root_element[:id].to_s.split("_").last - end - # The `` of column +key+ (`:external_id` or `"External ID"`). A # hidden cell is found with `visible: :all`. # @return [Capybara::Node::Element] @@ -54,11 +48,6 @@ def has_no_cell?(key, **options) has_no_css?(cell_selector(key), **visibility_of(key), **options) end - # Ticks the row's batch-action checkbox. - def select - find(:active_admin, :table_row_checkbox).check - end - private # The columns are declared on the table the row belongs to. @@ -72,7 +61,26 @@ def cell_selector(key) end end + # One `` of the table body. + class Row < BaseRow + # The record id from the row's `id="_"`. + # @return [String] + def record_id + root_element[:id].to_s.split("_").last + end + + # Ticks the row's batch-action checkbox. + def select + find(:active_admin, :table_row_checkbox).check + end + end + + # The `` row, such as the totals row of activeadmin_table_footer. + # Its cells are read by column key, as in a body row. + class Footer < BaseRow; end + sections :rows, Row, :active_admin, :table_row + section :footer, Footer, :active_admin, :table_footer # The row of the record with +record_id+. # @raise [ArgumentError] when +record_id+ is not an Integer or a non-empty String @@ -162,8 +170,13 @@ def sorted_by end # True when column +key+ has a sort link. - def has_sortable_column?(key) - has_css?(sel(:table_sortable_header, slug(key)), **visibility_of(key)) + def has_sortable_column?(key, **options) + has_css?(sel(:table_sortable_header, slug(key)), **visibility_of(key), **options) + end + + # Waits until column +key+ has no sort link. + def has_no_sortable_column?(key, **options) + has_no_css?(sel(:table_sortable_header, slug(key)), **visibility_of(key), **options) end private diff --git a/lib/active_admin_page_objects/version.rb b/lib/active_admin_page_objects/version.rb index 2c18208..dfa328d 100644 --- a/lib/active_admin_page_objects/version.rb +++ b/lib/active_admin_page_objects/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module ActiveAdminPageObjects - VERSION = "1.0.0" + VERSION = "1.1.0" end diff --git a/spec/active_admin_page_objects/batch_actions_spec.rb b/spec/active_admin_page_objects/batch_actions_spec.rb index 3380750..3b6948a 100644 --- a/spec/active_admin_page_objects/batch_actions_spec.rb +++ b/spec/active_admin_page_objects/batch_actions_spec.rb @@ -51,6 +51,16 @@ expect(index.flashes).to have_flash(:notice, "Set pending on 3 customers") end + it "opens the dialog of an action without confirming it" do + batch_actions.select_all + dialog = batch_actions.open_dialog("Set Status Selected") + expect(dialog.title).to eq("Are you sure you want to do this?") + expect(dialog).to have_field("status") + dialog.cancel + expect(index.flashes).to have_no_flash(:notice) + expect { batch_actions.open_dialog("Flag Selected") }.to raise_error(ArgumentError, /opens no dialog/) + end + it "applies the default destroy action" do index.table.row(customers.first.id).select batch_actions.apply("Delete Selected") diff --git a/spec/active_admin_page_objects/extension_spec.rb b/spec/active_admin_page_objects/extension_spec.rb index 6a19647..7f9299f 100644 --- a/spec/active_admin_page_objects/extension_spec.rb +++ b/spec/active_admin_page_objects/extension_spec.rb @@ -11,8 +11,10 @@ def chosen end end - def choose_option(scope, label) - ChooseOptionSpy.chosen << label if ChooseOptionSpy.active + # Records the keywords too: none unless the spec gave a search, so a + # widget module written as `choose_option(scope, label)` keeps working. + def choose_option(scope, label, **options) + ChooseOptionSpy.chosen << [label, options] if ChooseOptionSpy.active super end @@ -77,13 +79,29 @@ def paragraphs index = Pages::CustomerIndex.new index.load index.filters.filter(:status).select_option("pending") - expect(ChooseOptionSpy.chosen).to eq(["pending"]) + expect(ChooseOptionSpy.chosen).to eq([["pending", {}]]) form_page = Pages::CustomerForm.new form_page.load(action: "new") form_page.form.field(:status).fill("closed") - expect(ChooseOptionSpy.chosen).to eq(%w[pending closed]) + form_page.form.field(:category).fill("Retail", search: "Ret") + expect(ChooseOptionSpy.chosen).to eq([["pending", {}], ["closed", {}], ["Retail", { search: "Ret" }]]) expect(form_page.form.field(:status)).to have_select(selected: "closed") + expect(form_page.form.field(:category)).to have_select(selected: "Retail") + ensure + ChooseOptionSpy.active = false + ChooseOptionSpy.chosen.clear + end + + it "drives a modal dialog select through SelectControl" do + ChooseOptionSpy.active = true + create_customers(2) + index = Pages::CustomerIndex.new + index.load + index.batch_actions.select_all + index.batch_actions.apply("Set Status Selected", status: "pending") + expect(ChooseOptionSpy.chosen).to eq([["pending", {}]]) + expect(index.flashes).to have_flash(:notice, "Set pending on 2 customers") ensure ChooseOptionSpy.active = false ChooseOptionSpy.chosen.clear diff --git a/spec/active_admin_page_objects/filters_spec.rb b/spec/active_admin_page_objects/filters_spec.rb index 0c38577..3c96823 100644 --- a/spec/active_admin_page_objects/filters_spec.rb +++ b/spec/active_admin_page_objects/filters_spec.rb @@ -351,9 +351,9 @@ def declared_filters(&) expect(filters.filter(:status)).to have_no_item("active") end - it "filters by an association" do + it "filters by an association, and the native select ignores search:" do index.load - filters.filter(:category).select_option("Retail") + filters.filter(:category).select_option("Retail", search: "Ret") filters.submit expect(names).to eq(["Alpha"]) end @@ -503,6 +503,22 @@ def native_select_visibility = :all expect { range.set("yesterday") }.to raise_error(Date::Error) end + it "closes the datepicker before it returns" do + index.load + filters.filter(:created_at).set("2026-02-01", "2026-04-01") + expect(page).to have_no_css("#ui-datepicker-div", visible: true, wait: 0) + end + + # jQuery UI hides the picker and keeps the node, which such a suite finds. + it "closes the datepicker when the suite finds hidden elements" do + index.load + Capybara.ignore_hidden_elements = false + filters.filter(:created_at).set("2026-02-01", "2026-04-01") + expect(page).to have_no_css("#ui-datepicker-div", visible: true, wait: 0) + ensure + Capybara.ignore_hidden_elements = true + end + it "leaves an open end" do index.load filters.filter(:created_at).set("2026-02-01") diff --git a/spec/active_admin_page_objects/flashes_spec.rb b/spec/active_admin_page_objects/flashes_spec.rb index d345b38..f3e7d64 100644 --- a/spec/active_admin_page_objects/flashes_spec.rb +++ b/spec/active_admin_page_objects/flashes_spec.rb @@ -18,13 +18,21 @@ end it "lists the messages" do - expect(index.flashes.messages).to eq(["Report is not ready", "Report failed"]) + expect(index.flashes.messages).to eq(["Report is not ready", "Report failed", "Report queued"]) expect(index.flashes.messages(:error)).to eq(["Report failed"]) end - it "rejects a type outside TYPES" do - expect { index.flashes.has_flash?(:info) }.to raise_error(ArgumentError, /flash type/) - expect { index.flashes.has_flash?(nil) }.to raise_error(ArgumentError, /flash type must be one of .*got nil/) + it "checks a type the app adds" do + expect(index.flashes).to have_flash(:ok_message, "Report queued") + expect(index.flashes).to have_flash("ok_message") + expect(index.flashes).to have_no_flash(:authorize_error) + expect(index.flashes.messages(:ok_message)).to eq(["Report queued"]) + end + + it "rejects a type that is not a name" do + expect { index.flashes.has_flash?("") }.to raise_error(ArgumentError, /flash type/) + expect { index.flashes.has_flash?(nil) } + .to raise_error(ArgumentError, /flash type must be a non-empty Symbol or String, got nil/) expect { index.flashes.has_no_flash?(nil) }.to raise_error(ArgumentError, /flash type/) expect { index.flashes.messages(42) }.to raise_error(ArgumentError, /got 42/) end diff --git a/spec/active_admin_page_objects/form_spec.rb b/spec/active_admin_page_objects/form_spec.rb index 74c4302..9e42315 100644 --- a/spec/active_admin_page_objects/form_spec.rb +++ b/spec/active_admin_page_objects/form_spec.rb @@ -237,6 +237,23 @@ def declared_form(&) expect(status.selected).to eq("closed") end + it "reads and checks the options of a multiple select" do + form_page.load(action: "new", query: { channels_select: 1 }) + channels = ActiveAdminPageObjects::Form::SelectField.new(form, find("#customer_channels_input")) + expect(channels.selected_options).to eq([]) + expect(channels.selected).to be_nil + channels.fill("Email") + channels.fill("Post") + expect(channels.selected_options).to eq(%w[Email Post]) + expect(channels.selected).to eq("Email") + expect(channels).to have_selected("Post", "Email") + expect(channels).to have_no_selected("Email") + expect(form.field(:status)).to have_no_selected("pending") + form.field(:status).fill("pending") + expect(form.field(:status)).to have_selected("pending") + expect { channels.has_selected? }.to raise_error(ArgumentError, /no option labels/) + end + it "returns a BooleanField for a boolean" do active = form.field(:active) expect(active).to be_an_instance_of(ActiveAdminPageObjects::Form::BooleanField) diff --git a/spec/active_admin_page_objects/has_many_spec.rb b/spec/active_admin_page_objects/has_many_spec.rb index cafad09..70b96cb 100644 --- a/spec/active_admin_page_objects/has_many_spec.rb +++ b/spec/active_admin_page_objects/has_many_spec.rb @@ -157,12 +157,15 @@ def declared_orders(&) it "removes a new fieldset with its remove link" do orders.add expect(orders).to have_fieldsets(count: 3) + expect(orders.fieldset(2)).to have_remove orders.remove(2) expect(orders).to have_fieldsets(count: 2) end it "raises when a fieldset has neither a remove link nor a delete checkbox" do + expect(orders.fieldset(0)).to have_remove page.execute_script("document.querySelectorAll('li.has_many_delete').forEach((node) => node.remove())") + expect(orders.fieldset(0)).to have_no_remove expect { orders.remove(0) }.to raise_error(ArgumentError, /no remove link and no delete checkbox/) expect(orders).to have_fieldsets(count: 2) end diff --git a/spec/active_admin_page_objects/modal_dialog_spec.rb b/spec/active_admin_page_objects/modal_dialog_spec.rb index dd6cf04..730f1df 100644 --- a/spec/active_admin_page_objects/modal_dialog_spec.rb +++ b/spec/active_admin_page_objects/modal_dialog_spec.rb @@ -27,6 +27,19 @@ def open_batch_action(label) expect(index.flashes).to have_flash(:notice, "Set pending on 3 customers") end + # Typing twice reaches a closed picker: an Escape then would close the dialog. + it "types a date and keeps the dialog open" do + open_batch_action("Schedule Selected") + dialog.fill("on", with: Date.new(2026, 1, 15)) + dialog.fill("on", with: "2026-02-01") + + expect(index).to have_modal_dialog(wait: 0) + expect(page).to have_no_css("#ui-datepicker-div", visible: true, wait: 0) + dialog.confirm + + expect(index.flashes).to have_flash(:notice, "Scheduled 3 customers on 2026-02-01") + end + it "closes on cancel without running the action" do open_batch_action("Set Status Selected") expect(dialog.title).to eq("Are you sure you want to do this?") diff --git a/spec/active_admin_page_objects/rspec_matchers_spec.rb b/spec/active_admin_page_objects/rspec_matchers_spec.rb index 19d69e1..a69d9ed 100644 --- a/spec/active_admin_page_objects/rspec_matchers_spec.rb +++ b/spec/active_admin_page_objects/rspec_matchers_spec.rb @@ -133,8 +133,8 @@ def have_flash_message(text, options = {}) it "is the predicate matcher for any object" do expect(index).to have_page_title(text: "Customers") expect(index.table.row(customer.id)).not_to have_status_tag(:status, "Active") - expect { expect(page).to have_footer } - .to raise_error(RSpec::Expectations::ExpectationNotMetError, /to respond to `has_footer\?`/) + expect { expect(page).to have_side_panel } + .to raise_error(RSpec::Expectations::ExpectationNotMetError, /to respond to `has_side_panel\?`/) end it "uses a matcher of the same name that Capybara or SitePrism defines" do diff --git a/spec/active_admin_page_objects/table_spec.rb b/spec/active_admin_page_objects/table_spec.rb index 227684f..392c32d 100644 --- a/spec/active_admin_page_objects/table_spec.rb +++ b/spec/active_admin_page_objects/table_spec.rb @@ -33,6 +33,17 @@ def declared_table(&) expect(table.exact_mismatches).to eq(missing: ["notes"], unexpected: [], labels: {}) end + it "reads the footer cells by column" do + expect(table).to have_no_footer + index.load(query: { with_totals: 1 }) + expect(table).to have_rows(count: 3) + expect(table.footer).to have_cell(:name, exact_text: "Total") + expect(table.footer).to have_cell("Balance", exact_text: "30.0") + expect(table.footer).to have_cell(:email, exact_text: "") + expect(table.footer).to have_no_cell(:balance, exact_text: "10.0") + expect(table.footer.cell(:balance).text).to eq("30.0") + end + it "has an optional column when the page shows it" do index.load(query: { with_notes: 1 }) expect(table).to have_exact_columns @@ -141,6 +152,9 @@ def declared_table(&) expect(table).to have_sortable_column(:name) expect(table).to have_sortable_column("Created") Capybara.using_wait_time(0) { expect(table.has_sortable_column?(:category)).to be(false) } + expect(table).to have_no_sortable_column(:category) + expect(table.has_no_sortable_column?(:name, wait: 0)).to be(false) + expect(table.has_sortable_column?(:category, wait: 0)).to be(false) end it "sorts by clicking a header" do diff --git a/spec/active_admin_page_objects/text_spec.rb b/spec/active_admin_page_objects/text_spec.rb index ea3896a..e0e55b9 100644 --- a/spec/active_admin_page_objects/text_spec.rb +++ b/spec/active_admin_page_objects/text_spec.rb @@ -59,6 +59,6 @@ end it "has a version" do - expect(ActiveAdminPageObjects::VERSION).to eq("1.0.0") + expect(ActiveAdminPageObjects::VERSION).to eq("1.1.0") end end diff --git a/spec/dummy/app/admin/customers.rb b/spec/dummy/app/admin/customers.rb index 98e7420..d6ebdde 100644 --- a/spec/dummy/app/admin/customers.rb +++ b/spec/dummy/app/admin/customers.rb @@ -34,6 +34,10 @@ redirect_to collection_path, notice: "Set #{inputs['status']} on #{ids.size} customers" end + batch_action :schedule, form: { on: :datepicker } do |ids, inputs| + redirect_to collection_path, notice: "Scheduled #{ids.size} customers on #{inputs['on']}" + end + action_item :report, only: :index do link_to "Report", report_admin_customers_path, class: "action-item-button" end @@ -61,17 +65,20 @@ collection_action :report do flash[:alert] = "Report is not ready" flash[:error] = "Report failed" + flash[:ok_message] = "Report queued" redirect_to collection_path end index do selectable_column id_column - column :name + # `?with_totals=1` adds the activeadmin_table_footer totals row. + totals = params[:with_totals].present? + column :name, **(totals ? { footer: "Total" } : {}) column :email column :external_id column(:status) { |customer| status_tag(customer.status) } - column :balance + column :balance, **(totals ? { footer: :sum } : {}) column :active column :category column "Created", :created_at @@ -121,7 +128,12 @@ f.input :phone, as: :phone f.input :password, as: :password f.input :tier, as: :radio, collection: Customer::TIERS - f.input :channels, as: :check_boxes, collection: Customer::CHANNELS + # `?channels_select=1` renders channels as a multiple select. + if params[:channels_select] + f.input :channels, as: :select, collection: Customer::CHANNELS, multiple: true + else + f.input :channels, as: :check_boxes, collection: Customer::CHANNELS + end f.input :signed_on, as: :datepicker end f.has_many :orders, heading: "Orders", allow_destroy: true do |o| diff --git a/spec/dummy/config/application.rb b/spec/dummy/config/application.rb index 8ea0a6e..d30a459 100644 --- a/spec/dummy/config/application.rb +++ b/spec/dummy/config/application.rb @@ -8,6 +8,7 @@ require "action_view/railtie" require "sprockets/railtie" require "activeadmin" +require "activeadmin_table_footer" module Dummy class Application < Rails::Application