Skip to content

Add if option to conditionally hide a collection action #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module ActiveAdminScopedCollectionActions
module ResourceExtension
include MethodOrProcHelper

def initialize(*)
super
Expand Down Expand Up @@ -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?
Expand Down
5 changes: 4 additions & 1 deletion spec/authors_actions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -77,7 +81,6 @@
end
end


context 'perform Delete-action when cheked only one item' do
let(:delete_author) { Author.first }

Expand Down
9 changes: 9 additions & 0 deletions spec/support/admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down