From 6b0445484c0691b5e690de685c1567179d33d6c2 Mon Sep 17 00:00:00 2001 From: Guewen Baconnier Date: Mon, 3 Feb 2025 18:22:24 +0100 Subject: [PATCH] Support proc in confirm option Which allows to defer the content of the message, such as: scoped_collection_action :scoped_collection_export_csv, title: I18n.t('active_admin.actions.offers.export_csv'), confirm: lambda { I18n.t('active_admin.actions.offers.collection_export.confirm', offer_limit: Setting.offer_file_export_count_limit) } do In this example, Settings needs access to the database, and would be executed at startup if not using a proc. --- .../resource_extension.rb | 1 + spec/posts_actions_spec.rb | 12 ++++++++++++ spec/support/admin.rb | 3 +++ 3 files changed, 16 insertions(+) diff --git a/lib/active_admin_scoped_collection_actions/resource_extension.rb b/lib/active_admin_scoped_collection_actions/resource_extension.rb index 77f305b..4958333 100644 --- a/lib/active_admin_scoped_collection_actions/resource_extension.rb +++ b/lib/active_admin_scoped_collection_actions/resource_extension.rb @@ -65,6 +65,7 @@ def scoped_collection_actions_sidebar_section b_data[:inputs] = options[:form].is_a?(Proc) ? options[:form].call : options[:form] end b_data[:confirm] = options.fetch(:confirm, I18n.t('active_admin_scoped_collection_actions.confirm_action_message')) + b_data[:confirm] = b_data[:confirm].call if b_data[:confirm].is_a?(Proc) b_options[:data] = b_data.to_json button b_title, b_options end diff --git a/spec/posts_actions_spec.rb b/spec/posts_actions_spec.rb index 706186c..a5ea305 100644 --- a/spec/posts_actions_spec.rb +++ b/spec/posts_actions_spec.rb @@ -41,4 +41,16 @@ end end + context 'scoped collection action DELETE' do + before do + page.find('#collection_actions_sidebar_section button', text: 'Delete').click + end + + context 'title' do + it 'has predefined confirmation title' do + expect(page).to have_css('.active_admin_dialog_mass_update_by_filter', text: 'Custom text for confirm delete all?') + end + end + end + end diff --git a/spec/support/admin.rb b/spec/support/admin.rb index f74f694..c69debc 100644 --- a/spec/support/admin.rb +++ b/spec/support/admin.rb @@ -36,6 +36,9 @@ def add_post_resource(options = {}, &block) author_id: Author.all.map { |author| [author.name, author.id] } } } + scoped_collection_action :scoped_collection_destroy, + title: 'Delete', + confirm: -> { 'Custom text for confirm delete all?' } end Rails.application.reload_routes!