Skip to content

Commit 94d92f0

Browse files
committed
Merge pull request #15 from workgena/redirect
Optional redirect_to(url) after action
2 parents e09f432 + b7d9576 commit 94d92f0

File tree

4 files changed

+40
-36
lines changed

4 files changed

+40
-36
lines changed

README.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,18 @@ config.scoped_collection_actions_if = -> { params[:scope] }
146146
### Can I use my handler on update/delete action?
147147

148148
You can pass block to default actions update and delete.
149+
And do custom redirect after it. Use render(location: 'somethin') instead of redirect_to().
150+
151+
This example renders form which allows to change "name" field. And after it do redirect to dashboard page.
149152

150153
```ruby
151-
# Delte absolutely all
152-
scoped_collection_action :scoped_collection_destroy do
153-
Phone.all.delete_all
154-
render nothing: true, status: :no_content
154+
scoped_collection_action :scoped_collection_update,
155+
form: -> {
156+
{name: 'text'}
157+
} do
158+
scoped_collection_records.update_all(name: params[:changes][:name])
159+
flash[:notice] = 'Name successfully changed.'
160+
render nothing: true, status: :no_content, location: admin_dashboard_path
155161
end
156162
```
157163

@@ -163,7 +169,7 @@ Example:
163169

164170
```ruby
165171
scoped_collection_action :erase_date, title: 'Nullify' do
166-
batch_action_collection.update_all(manufactured_at: nil)
172+
scoped_collection_records.update_all(manufactured_at: nil)
167173
end
168174
```
169175

@@ -255,7 +261,7 @@ Example with erasing phone diagonal. In this case you Phone-model has validation
255261
```ruby
256262
scoped_collection_action :change_diagonal, form: { diagonal: 'text' } do
257263
errors = []
258-
batch_action_collection.find_each do |record|
264+
scoped_collection_records.find_each do |record|
259265
errors << "#{record.errors.full_messages.join('. ')}" unless record.update(diagonal: params[:changes][:diagonal])
260266
end
261267
if errors.empty?

lib/active_admin_scoped_collection_actions/dsl.rb

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,22 @@ def add_scoped_collection_action_default_update(options, &block)
2626
render nothing: true, status: :no_content and next
2727
end
2828
permitted_changes = params.require(:changes).permit(*(options[:form].call.keys))
29-
errors = []
3029
if block_given?
31-
begin
32-
instance_eval &block
33-
rescue Exception => e
34-
errors << e
35-
end
30+
instance_eval &block
3631
else
32+
errors = []
3733
scoped_collection_records.find_each do |record|
38-
errors << "#{record.attributes[resource_class.primary_key]} | #{record.errors.full_messages.join('. ')}" unless update_resource(record, [permitted_changes])
34+
unless update_resource(record, [permitted_changes])
35+
errors << "#{record.attributes[resource_class.primary_key]} | #{record.errors.full_messages.join('. ')}"
36+
end
3937
end
38+
if errors.empty?
39+
flash[:notice] = 'Batch update done'
40+
else
41+
flash[:error] = errors.join(". ")
42+
end
43+
render nothing: true, status: :no_content
4044
end
41-
if errors.empty?
42-
flash[:notice] = 'Batch update done'
43-
else
44-
flash[:error] = errors.join(". ")
45-
end
46-
render nothing: true, status: :no_content
4745
end
4846
end
4947

@@ -54,28 +52,25 @@ def add_scoped_collection_action_default_destroy(_, &block)
5452
flash[:error] = 'Access denied'
5553
render nothing: true, status: :no_content and next
5654
end
57-
errors = []
5855
if block_given?
59-
begin
60-
instance_eval &block
61-
rescue Exception => e
62-
errors << e
63-
end
56+
instance_eval &block
6457
else
58+
errors = []
6559
scoped_collection_records.find_each do |record|
66-
errors << "#{record.attributes[resource_class.primary_key]} | Cant be destroyed}" unless destroy_resource(record)
60+
unless destroy_resource(record)
61+
errors << "#{record.attributes[resource_class.primary_key]} | Cant be destroyed}"
62+
end
6763
end
64+
if errors.empty?
65+
flash[:notice] = 'Batch destroy done'
66+
else
67+
flash[:error] = errors.join(". ")
68+
end
69+
render nothing: true, status: :no_content
6870
end
69-
if errors.empty?
70-
flash[:notice] = 'Batch destroy done'
71-
else
72-
flash[:error] = errors.join(". ")
73-
end
74-
render nothing: true, status: :no_content
7571
end
7672
end
7773

7874

79-
8075
end
8176
end
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module ActiveAdminScopedCollectionActions
2-
VERSION = "0.1.0"
2+
VERSION = "0.2.0"
33
end

vendor/assets/javascripts/active_admin_scoped_collection_actions.js.coffee

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,8 @@ $(document).ready ->
1818
$('.paginated_collection').find('input.collection_selection:checked').each (i, el) ->
1919
form_data["collection_selection"].push($(el).val())
2020

21-
$.post(url, form_data).always () ->
22-
window.location.reload()
21+
$.post(url, form_data).always (data, textStatus, jqXHR) ->
22+
if jqXHR.getResponseHeader('Location')
23+
window.location.assign jqXHR.getResponseHeader('Location')
24+
else
25+
window.location.reload()

0 commit comments

Comments
 (0)