From 4a7b4b9860b8ed4fb3fb5724acd6867539dd7519 Mon Sep 17 00:00:00 2001 From: Guewen Baconnier Date: Tue, 4 Feb 2025 08:37:02 +0100 Subject: [PATCH] Add if option to conditionally hide a collection action Closes #41 --- README.md | 6 ++++++ .../resource_extension.rb | 3 +++ spec/authors_actions_spec.rb | 5 ++++- spec/support/admin.rb | 9 +++++++++ 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f7c74cc..aadf380 100644 --- a/README.md +++ b/README.md @@ -149,6 +149,12 @@ config.scoped_collection_actions_if = -> { params[:scope] } # etc. ``` +You can also manage visibility of each action individually: + +```ruby +scoped_collection_action :scoped_collection_destroy, if: proc { can? :destroy, Blog } +``` + ### Can I use my handler on update/delete action? You can pass block to default actions update and delete. diff --git a/lib/active_admin_scoped_collection_actions/resource_extension.rb b/lib/active_admin_scoped_collection_actions/resource_extension.rb index 77f305b..03d5ed6 100644 --- a/lib/active_admin_scoped_collection_actions/resource_extension.rb +++ b/lib/active_admin_scoped_collection_actions/resource_extension.rb @@ -1,5 +1,6 @@ module ActiveAdminScopedCollectionActions module ResourceExtension + include MethodOrProcHelper def initialize(*) super @@ -54,6 +55,8 @@ def scoped_collection_actions_sidebar_section div I18n.t('active_admin_scoped_collection_actions.sidebar_msg') active_admin_config.scoped_collection_actions.each do |key, options={}| + next if options.key?(:if) && !call_method_or_exec_proc(options[:if]) + b_title = options.fetch(:title, ::ActiveSupport::Inflector.humanize(key)) b_options = {} b_options[:class] = options[:class] if options[:class].present? diff --git a/spec/authors_actions_spec.rb b/spec/authors_actions_spec.rb index 4cfcb1a..9d1cfcc 100644 --- a/spec/authors_actions_spec.rb +++ b/spec/authors_actions_spec.rb @@ -24,6 +24,10 @@ .to have_css('button', text: 'Update') expect(page.find('#collection_actions_sidebar_section')) .to have_css('button', text: 'Delete') + expect(page.find('#collection_actions_sidebar_section')) + .to have_css('button', text: 'Visible Action') + expect(page.find('#collection_actions_sidebar_section')) + .not_to have_css('button', text: 'Hidden Action') end end @@ -77,7 +81,6 @@ end end - context 'perform Delete-action when cheked only one item' do let(:delete_author) { Author.first } diff --git a/spec/support/admin.rb b/spec/support/admin.rb index f74f694..c26f6e2 100644 --- a/spec/support/admin.rb +++ b/spec/support/admin.rb @@ -14,6 +14,15 @@ def add_author_resource(options = {}, &block) scoped_collection_action :scoped_collection_destroy, title: 'Delete', confirm: 'Delete all?' + scoped_collection_action :scoped_collection_custom_visible, + if: proc { true }, + title: 'Visible Action' do + flash[:notice] = 'Visible action executed' + end + scoped_collection_action :scoped_collection_custom_hidden, + if: proc { false }, + title: 'Hidden Action' + end Rails.application.reload_routes!