Skip to content

Commit 4a7b4b9

Browse files
committed
Add if option to conditionally hide a collection action
Closes #41
1 parent 0c9d6a0 commit 4a7b4b9

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,12 @@ config.scoped_collection_actions_if = -> { params[:scope] }
149149
# etc.
150150
```
151151

152+
You can also manage visibility of each action individually:
153+
154+
```ruby
155+
scoped_collection_action :scoped_collection_destroy, if: proc { can? :destroy, Blog }
156+
```
157+
152158
### Can I use my handler on update/delete action?
153159

154160
You can pass block to default actions update and delete.

lib/active_admin_scoped_collection_actions/resource_extension.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module ActiveAdminScopedCollectionActions
22
module ResourceExtension
3+
include MethodOrProcHelper
34

45
def initialize(*)
56
super
@@ -54,6 +55,8 @@ def scoped_collection_actions_sidebar_section
5455
div I18n.t('active_admin_scoped_collection_actions.sidebar_msg')
5556

5657
active_admin_config.scoped_collection_actions.each do |key, options={}|
58+
next if options.key?(:if) && !call_method_or_exec_proc(options[:if])
59+
5760
b_title = options.fetch(:title, ::ActiveSupport::Inflector.humanize(key))
5861
b_options = {}
5962
b_options[:class] = options[:class] if options[:class].present?

spec/authors_actions_spec.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
.to have_css('button', text: 'Update')
2525
expect(page.find('#collection_actions_sidebar_section'))
2626
.to have_css('button', text: 'Delete')
27+
expect(page.find('#collection_actions_sidebar_section'))
28+
.to have_css('button', text: 'Visible Action')
29+
expect(page.find('#collection_actions_sidebar_section'))
30+
.not_to have_css('button', text: 'Hidden Action')
2731
end
2832

2933
end
@@ -77,7 +81,6 @@
7781
end
7882
end
7983

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

spec/support/admin.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ def add_author_resource(options = {}, &block)
1414
scoped_collection_action :scoped_collection_destroy,
1515
title: 'Delete',
1616
confirm: 'Delete all?'
17+
scoped_collection_action :scoped_collection_custom_visible,
18+
if: proc { true },
19+
title: 'Visible Action' do
20+
flash[:notice] = 'Visible action executed'
21+
end
22+
scoped_collection_action :scoped_collection_custom_hidden,
23+
if: proc { false },
24+
title: 'Hidden Action'
25+
1726
end
1827

1928
Rails.application.reload_routes!

0 commit comments

Comments
 (0)