Skip to content

Commit d57eeb9

Browse files
committed
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.
1 parent 0c9d6a0 commit d57eeb9

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

lib/active_admin_scoped_collection_actions/resource_extension.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ def scoped_collection_actions_sidebar_section
6565
b_data[:inputs] = options[:form].is_a?(Proc) ? options[:form].call : options[:form]
6666
end
6767
b_data[:confirm] = options.fetch(:confirm, I18n.t('active_admin_scoped_collection_actions.confirm_action_message'))
68+
b_data[:confirm] = b_data[:confirm].call if b_data[:confirm].is_a?(Proc)
6869
b_options[:data] = b_data.to_json
6970
button b_title, b_options
7071
end

spec/posts_actions_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,22 @@
3939
expect(Post.all.map(&:body).uniq).to match_array([new_body_text])
4040
expect(Post.all.map(&:author).uniq).to match_array([new_author])
4141
end
42+
43+
it 'calls proc when confirm is a proc' do
44+
page.find('#collection_actions_sidebar_section button', text: 'Delete').click
45+
end
46+
end
47+
48+
context 'scoped collection action DELETE' do
49+
before do
50+
page.find('#collection_actions_sidebar_section button', text: 'Delete').click
51+
end
52+
53+
context 'title' do
54+
it 'has predefined confirmation title' do
55+
expect(page).to have_css('.active_admin_dialog_mass_update_by_filter', text: 'Custom text for confirm delete all?')
56+
end
57+
end
4258
end
4359

4460
end

spec/support/admin.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ def add_post_resource(options = {}, &block)
3636
author_id: Author.all.map { |author| [author.name, author.id] }
3737
}
3838
}
39+
scoped_collection_action :scoped_collection_destroy,
40+
title: 'Delete',
41+
confirm: -> { 'Custom text for confirm delete all?' }
3942
end
4043

4144
Rails.application.reload_routes!

0 commit comments

Comments
 (0)