active_admin_scoped_collection_actions 2.0.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1170fafbeb26fcb0da73fd784d5e2718f0e0bd6d82b9f30cb2dd0945851b3906
4
- data.tar.gz: 81fc10db07fd91bf1a18271e6ece37a6cf63f6f73d9c1472ce314fbaeaafac34
3
+ metadata.gz: 42b89c377c3731b575446f6e1f81f476933cc6eb81f8ecede26029acb82430c2
4
+ data.tar.gz: 72fbb62b5ccd5ee2ee3a137c35aa858a0563a1ec9809aa456ae50cdc7504ee72
5
5
  SHA512:
6
- metadata.gz: 306ff67913daff3fac2e014f53770580c9a5d462390d4ed7b2ccbd89ebf70618a2d6a40c1bb09067b8e9e6699a850e3d3ce9f7dd4eaa6134e3177e680eb5b420
7
- data.tar.gz: f04a536138d89517dd532862ff708ace7ae330f86e8d78584c21481eaa706362560c7b1a5356a855a7c448529aea46e7a91d20d23326bb587441a22cc771d23c
6
+ metadata.gz: 1e65c11a26b58d814bbac206865f9b7142ad6cd84855d0df983482435bb2f089b044972a735e8337a27a4cf491f499e87f05d292bf0fab921296f3f33ad79057
7
+ data.tar.gz: 7969a98d345001dc589ee738c8d26ddd442b31b71b903e1fe88da45ac448fc975c92c4e4e051d5c9677bd9aaef9bd2f17031ab43dff5be35fb3599ab5aa919c2
data/CHANGELOG.md CHANGED
@@ -1,3 +1,27 @@
1
+ ## ActiveAdmin Scoped Collection Actions 2.2.0 (November 27, 2026) ##
2
+
3
+ * ActiveAdmin 4 support: the dialog is a Flowbite modal in an ES module pinned into `ActiveAdmin.importmap`;
4
+ ActiveAdmin 3 keeps the jQuery UI dialog
5
+ * `data-scoped-collection-action` on the sidebar buttons
6
+
7
+ ## ActiveAdmin Scoped Collection Actions 2.1.0 (October 02, 2026) ##
8
+
9
+ * New action option `:confirm_submit` - turns the fields dialog into two steps, the second one replaces the
10
+ fields with amount of affected records and the changes to be applied
11
+ * New action option `:confirm_summary` - tells the user how many records the action affects
12
+ * Form field can be defined as `{type: 'text', class: 'my-widget'}` to render input with own class
13
+ * **The required Ruby version is now 3.3** — Ruby 3.1 and 3.2 are EOL; stay on 2.0.0 for those
14
+ * Tested against Ruby 3.3, 3.4 and 4.0 with Rails 8.0 and 8.1; Rails 7.1/7.2 left the CI matrix but are not excluded by any constraint
15
+ * The packaged gem ships only `lib`, `vendor`, `config` and the docs — no spec suite, no screenshots
16
+
17
+ ## ActiveAdmin Scoped Collection Actions 2.0.0 (April 07, 2026) ##
18
+
19
+ * compatibility with ActiveAdmin 3.5, including its jQuery UI dialog changes
20
+ * fixed the Sprockets manifest error on Rails 8.0+
21
+ * New action option `:if` to conditionally hide a collection action (@qoqa #48)
22
+ * `:confirm` accepts a proc (@qoqa #47)
23
+ * dropped EOL Ruby and Rails versions
24
+
1
25
  ## ActiveAdmin Scoped Collection Actions 1.0.1 (March 02, 2024) ##
2
26
 
3
27
  * compatibility with ActiveAdmin 3.x (@oskarpearson #43)
@@ -10,12 +34,10 @@
10
34
 
11
35
  * Swedish locale (@buren #27)
12
36
 
13
-
14
37
  ## ActiveAdmin Scoped Collection Actions 0.3.3 (July 17, 2017) ##
15
38
 
16
39
  * Fixed compatibility with Rails 5.1
17
40
 
18
-
19
41
  ## ActiveAdmin Scoped Collection Actions 0.3.2 (July 17, 2017) ##
20
42
 
21
43
  * Added I18n support.
data/README.md CHANGED
@@ -1,20 +1,42 @@
1
1
  [![Build Status](https://github.com/activeadmin-plugins/active_admin_scoped_collection_actions/actions/workflows/ci.yml/badge.svg)](https://github.com/activeadmin-plugins/active_admin_scoped_collection_actions/actions/workflows/ci.yml)
2
2
  ![Coverage](https://img.shields.io/endpoint?url=https://activeadmin-plugins.github.io/active_admin_scoped_collection_actions/badge.json)
3
3
 
4
-
5
4
  # ActiveAdmin Scoped Collection Actions
5
+
6
6
  Plugin for ActiveAdmin. Provides batch Update and Delete for scoped_collection (Filters + Scope) across all pages.
7
7
 
8
- ![Step 1](/screenshots/sidebar.png)
8
+ ![Collection actions in the ActiveAdmin sidebar](/screenshots/sidebar.png)
9
+
10
+ ![The confirmation dialog with the update form](/screenshots/pupup.png)
9
11
 
10
- ![Step 1](/screenshots/pupup.png)
12
+ ## Contents
11
13
 
14
+ - [Description](#description)
15
+ - [Installation](#installation)
16
+ - [ActiveAdmin 3](#activeadmin-3)
17
+ - [ActiveAdmin 4](#activeadmin-4)
18
+ - [Usage](#usage)
19
+ - [Update action](#update-action)
20
+ - [Custom actions](#custom-actions)
21
+ - [Details and settings](#details-and-settings)
22
+ - [Why don't I see the sidebar with collection actions?](#why-dont-i-see-the-sidebar-with-collection-actions)
23
+ - [Can I use my own handler for the update and delete actions?](#can-i-use-my-own-handler-for-the-update-and-delete-actions)
24
+ - [How can I rename a button?](#how-can-i-rename-a-button)
25
+ - [How can I change the modal dialog title?](#how-can-i-change-the-modal-dialog-title)
26
+ - [How can I tell the user how many records are affected?](#how-can-i-tell-the-user-how-many-records-are-affected)
27
+ - [How can I ask for an extra confirmation before the form is submitted?](#how-can-i-ask-for-an-extra-confirmation-before-the-form-is-submitted)
28
+ - [Can I replace the pop-up form with my own?](#can-i-replace-the-pop-up-form-with-my-own)
29
+ - [How do I notify the user about success and errors?](#how-do-i-notify-the-user-about-success-and-errors)
30
+ - [Can I perform an action only on selected items?](#can-i-perform-an-action-only-on-selected-items)
31
+ - [License](#license)
12
32
 
13
- # Description
33
+ ## Description
14
34
 
15
- This gem give you ability to perform various batch actions on any filtered (or scoped) resource. Action applies to all records across all pages. It is similar to ActiveAdmin batch action, but affects all filtered records. This is usefull if you want to delete or update a lot of records in one click.
35
+ This gem gives you the ability to perform various batch actions on any filtered (or scoped) resource. An action
36
+ applies to all records across all pages. It is similar to an ActiveAdmin batch action, but affects all filtered
37
+ records. This is useful when you want to delete or update a lot of records in one click.
16
38
 
17
- # Install
39
+ ## Installation
18
40
 
19
41
  Add this line to your application's Gemfile:
20
42
 
@@ -27,23 +49,68 @@ gem 'active_admin_scoped_collection_actions', github: 'activeadmin-plugins/activ
27
49
 
28
50
  And then execute:
29
51
 
52
+ ```sh
53
+ bundle
30
54
  ```
31
- $ bundle
32
- ```
33
55
 
34
- Add the following line at the end of "app/assets/javascript/active_admin.js":
56
+ ### ActiveAdmin 3
57
+
58
+ Add the following line at the end of `app/assets/javascript/active_admin.js`:
35
59
 
36
60
  ```javascript
37
61
  //= require active_admin_scoped_collection_actions
38
62
  ```
39
63
 
40
- Also include CSS in "app/assets/stylesheets/active_admin.css.scss"
64
+ Also include the CSS in `app/assets/stylesheets/active_admin.css.scss`:
41
65
 
42
- ```css
66
+ ```scss
43
67
  @import "active_admin_scoped_collection_actions";
44
68
  ```
45
69
 
46
- # Usage
70
+ ### ActiveAdmin 4
71
+
72
+ The gem pins its dialog module, `active_admin_scoped_collection_actions`, into `ActiveAdmin.importmap`. The dialog is
73
+ a Flowbite modal, the one ActiveAdmin 4 ships with.
74
+
75
+ Import the module on ActiveAdmin pages:
76
+
77
+ - With an ActiveAdmin that has the `javascript_modules` setting
78
+ ([activeadmin/activeadmin#9148](https://github.com/activeadmin/activeadmin/pull/9148)), the gem adds it there and
79
+ there is nothing to do.
80
+ - With active_admin_theme 4, add it to the theme's list in `config/initializers/active_admin.rb`:
81
+
82
+ ```ruby
83
+ ActiveAdminTheme.javascripts += %w[active_admin_scoped_collection_actions]
84
+ ```
85
+
86
+ - Otherwise copy `app/views/active_admin/_html_head.html.erb` from ActiveAdmin into your app and add at its end:
87
+
88
+ ```erb
89
+ <%= javascript_import_module_tag "active_admin_scoped_collection_actions" %>
90
+ ```
91
+
92
+ The sidebar and the modal are styled with Tailwind classes, so Tailwind has to scan the gem. Add it to `content` in
93
+ `tailwind-active_admin.config.js`:
94
+
95
+ ```javascript
96
+ const scopedCollectionActionsPath = execSync('bundle show active_admin_scoped_collection_actions', { encoding: 'utf-8' }).trim().split(/\r?\n/).pop();
97
+
98
+ export default {
99
+ content: [
100
+ // ...
101
+ `${scopedCollectionActionsPath}/app/javascript/**/*.js`,
102
+ `${scopedCollectionActionsPath}/lib/**/*.rb`
103
+ ],
104
+ // ...
105
+ }
106
+ ```
107
+
108
+ Differences from ActiveAdmin 3:
109
+
110
+ - `datepicker` fields are native date inputs, as everywhere in ActiveAdmin 4.
111
+ - The sidebar section has no titled panel, because ActiveAdmin 4 renders sidebar sections without one.
112
+
113
+ ## Usage
47
114
 
48
115
  Usually you need two standard actions: Delete and Update.
49
116
 
@@ -67,37 +134,65 @@ ActiveAdmin.register Post do
67
134
  end
68
135
  ```
69
136
 
70
- **Important**: Visit Posts page with your browser and you will see no changes. Now, perform any filter with the Filters sidebar. Only after you filter will you see a delete button. It will be in sidebar under Filters.
137
+ > **Important**
138
+ > Visit the Posts page with your browser and you will see no changes. Now, perform any filter with the Filters
139
+ > sidebar. Only after you filter will you see a delete button. It will be in the sidebar under Filters.
71
140
 
72
141
  ### Update action
73
142
 
74
- Update is second standard action and is more complex. It has "form" hash wrapped in Proc:
143
+ Update is the second standard action and is more complex. It has a `form` hash wrapped in a Proc:
75
144
 
76
145
  ```ruby
77
- scoped_collection_action :scoped_collection_update, form: -> do
78
- { name: 'text',
79
- diagonal: 'text',
80
- manufactured_at: 'datepicker',
81
- vendor_id: Vendor.all.map { |region| [region.name, region.id] },
82
- has_3g: [['Yes', 't'], ['No', 'f']]
83
- }
84
- end
146
+ scoped_collection_action :scoped_collection_update, form: -> do
147
+ { name: 'text',
148
+ diagonal: 'text',
149
+ manufactured_at: 'datepicker',
150
+ vendor_id: Vendor.all.map { |region| [region.name, region.id] },
151
+ has_3g: [['Yes', 't'], ['No', 'f']] }
152
+ end
85
153
  ```
86
154
 
87
- In this example Phone model has fields:
88
- * name - varchar string
89
- * diagonal - integer(of float)
90
- * manufactured_at - datetime
91
- * vendor_id - association "belongs_to :vendor, class_name: 'Vendor', foreign_key: :vendor_id"
92
- * has_3g - boolean
155
+ In this example the Phone model has fields:
93
156
 
94
- Parameter "form" is a proc object which returns Hash. It defines what fields you want to be able to update. Hash keys are column names in database. Hash values are a types of HTML inputs. We support only "text", "datepicker" and "selectbox". If you want something more complex - you can build your own forms.
157
+ - `name` — varchar string
158
+ - `diagonal` — integer (or float)
159
+ - `manufactured_at` — datetime
160
+ - `vendor_id` — association `belongs_to :vendor, class_name: 'Vendor', foreign_key: :vendor_id`
161
+ - `has_3g` — boolean
95
162
 
96
- # Custom Actions
163
+ The `form` parameter is a Proc which returns a Hash. It defines which fields you want to be able to update. Hash
164
+ keys are column names in the database, hash values are types of HTML inputs. Supported values are `text`,
165
+ `checkbox`, `datepicker`, and an Array of options for a selectbox. If you want something more complex, you can
166
+ build your own forms.
97
167
 
98
- Example: We have Phone resource and it has column "manufactured_at". We need an action which will erase this date.
168
+ A field value can also be a Hash, `{type: 'text', class: 'my-widget'}`. Such a field is rendered as a plain input
169
+ with your own class on it, so you can turn it into any widget (datetime picker, autocomplete, etc.) with your own
170
+ JavaScript. The dialog triggers the `mass_update_modal_dialog:after_open` event on `body` with the form as an
171
+ argument — this is the place to initialize your widgets:
99
172
 
100
- In ActiveAdmin resource:
173
+ ```javascript
174
+ $(document).on('mass_update_modal_dialog:after_open', function (event, form) {
175
+ $(form).find('input.my-widget').myWidget();
176
+ });
177
+ ```
178
+
179
+ On ActiveAdmin 4 the same event is a bubbling DOM `CustomEvent` with the form in `event.detail.form`.
180
+ `mass_update_modal_dialog:before_open` is dispatched the same way, before the modal is shown:
181
+
182
+ ```javascript
183
+ document.addEventListener('mass_update_modal_dialog:after_open', (event) => {
184
+ event.detail.form.querySelectorAll('input.my-widget').forEach((input) => myWidget(input));
185
+ });
186
+ ```
187
+
188
+ The button, the modal and its form carry `data-scoped-collection-action`, `data-scoped-collection-action-modal`
189
+ and `data-scoped-collection-action-form`, each set to the action name.
190
+
191
+ ## Custom actions
192
+
193
+ Example: we have a Phone resource with a `manufactured_at` column. We need an action which will erase this date.
194
+
195
+ In the ActiveAdmin resource:
101
196
 
102
197
  ```ruby
103
198
  ActiveAdmin.register Phone do
@@ -113,13 +208,12 @@ ActiveAdmin.register Phone do
113
208
  end
114
209
  ```
115
210
 
116
- This simple code will create a new button "Erase date" in sidebar. After clicking this button, the user will see confirm message "Are you sure?". After confirming, all filtered records will be updated.
117
-
211
+ This simple code will create a new "Erase date" button in the sidebar. After clicking it, the user will see the
212
+ confirmation message "Are you sure?". After confirming, all filtered records will be updated.
118
213
 
119
- # Details and Settings
214
+ ## Details and settings
120
215
 
121
-
122
- ### Why I don't see Sidebar with Collection Actions.
216
+ ### Why don't I see the sidebar with collection actions?
123
217
 
124
218
  Sidebar visibility by default depends on several things.
125
219
 
@@ -129,19 +223,18 @@ First you must set:
129
223
  config.batch_actions = true
130
224
  ```
131
225
 
132
- Actually, inside of this Gem we use "batch_actions". So without them Collection Actions wouldn't work.
226
+ Internally this gem uses `batch_actions`, so without them collection actions will not work.
133
227
 
134
228
  ```ruby
135
229
  scoped_collection_action :something_here
136
230
  ```
137
231
 
138
- You resource should have some collection actions. If it doesn't have any, the sidebar will not appear.
139
-
140
- By default we dont allow perform actions on **all** the records. We want protect you from accidental deleting.
232
+ Your resource should have some collection actions. If it does not have any, the sidebar will not appear.
141
233
 
142
- Sidebar with buttons will appear only after you perform filtering or scopes on resource records.
234
+ By default we do not allow performing actions on **all** the records — this protects you from accidental
235
+ deletion. The sidebar with buttons appears only after you filter or scope the resource records.
143
236
 
144
- And lastly you can manage sidebar visibility by resource config:
237
+ And lastly, you can manage sidebar visibility through the resource config:
145
238
 
146
239
  ```ruby
147
240
  # Always
@@ -151,91 +244,163 @@ config.scoped_collection_actions_if = -> { params[:scope] }
151
244
  # etc.
152
245
  ```
153
246
 
154
- You can also manage visibility of each action individually:
247
+ You can also manage the visibility of each action individually:
155
248
 
156
249
  ```ruby
157
250
  scoped_collection_action :scoped_collection_destroy, if: proc { can? :destroy, Blog }
158
251
  ```
159
252
 
160
- ### Can I use my handler on update/delete action?
253
+ ### Can I use my own handler for the update and delete actions?
161
254
 
162
- You can pass block to default actions update and delete.
163
- And do custom redirect after it. Use `render` (location: 'something') instead of `redirect_to()`.
255
+ You can pass a block to the default update and delete actions, and do a custom redirect after it. Use `head`
256
+ with a `location:` instead of `redirect_to`.
164
257
 
165
- This example renders form which allows to change `name` field. And after it do redirect to dashboard page.
258
+ This example renders a form which allows changing the `name` field, and after that redirects to the dashboard
259
+ page:
166
260
 
167
261
  ```ruby
168
- scoped_collection_action :scoped_collection_update,
169
- form: -> {
170
- {name: 'text'}
171
- } do
172
- scoped_collection_records.update_all(name: params[:changes][:name])
173
- flash[:notice] = 'Name successfully changed.'
174
- render nothing: true, status: :no_content, location: admin_dashboard_path
175
- end
262
+ scoped_collection_action :scoped_collection_update,
263
+ form: -> {
264
+ { name: 'text' }
265
+ } do
266
+ scoped_collection_records.update_all(name: params[:changes][:name])
267
+ flash[:notice] = 'Name successfully changed.'
268
+ head :no_content, location: admin_dashboard_path
269
+ end
176
270
  ```
177
271
 
272
+ ### How can I rename a button?
178
273
 
179
- ### How can I rename button?
180
-
181
- Every scoped_collection_action has option `:title`.
274
+ Every `scoped_collection_action` has the `:title` option.
182
275
 
183
276
  Example:
184
277
 
185
278
  ```ruby
186
- scoped_collection_action :erase_date, title: 'Nullify' do
187
- scoped_collection_records.update_all(manufactured_at: nil)
188
- end
279
+ scoped_collection_action :erase_date, title: 'Nullify' do
280
+ scoped_collection_records.update_all(manufactured_at: nil)
281
+ end
282
+ ```
283
+
284
+ ### How can I change the modal dialog title?
285
+
286
+ Similar to the button title — use the `:confirm` option:
287
+
288
+ ```ruby
289
+ scoped_collection_action :scoped_collection_destroy, confirm: 'Delete all phones?'
290
+ ```
291
+
292
+ ### How can I tell the user how many records are affected?
293
+
294
+ The `:confirm_summary` option adds a sentence with the number of affected records to the dialog:
295
+
296
+ ```ruby
297
+ scoped_collection_action :scoped_collection_destroy, confirm_summary: true
298
+ ```
299
+
300
+ ```text
301
+ Delete all?
302
+
303
+ You are going to delete 100500 record(s).
304
+
305
+ [ OK ] [ Cancel ]
189
306
  ```
190
307
 
308
+ `true` uses the default text — the `confirm_destroy_summary` locale key for `:scoped_collection_destroy` and
309
+ `confirm_action_summary` for any other action. Pass a String or a Proc to use your own text. `{count}` in the
310
+ text is replaced with the number.
311
+
312
+ The amount is requested from the server with the same filters, scope and checked records the action itself will
313
+ use, so it costs one extra COUNT query. Until the answer arrives the message is not displayed at all — there is
314
+ a spinner in its place and OK is disabled, so nothing can be confirmed before the user sees what they confirm.
315
+
316
+ When that request happens depends on which option is set:
191
317
 
192
- ### How can I modify modal dialog title?
318
+ - neither `:confirm_summary` nor `:confirm_submit` — nothing is counted and no request is made;
319
+ - `:confirm_summary` — once, when the dialog is opened;
320
+ - `:confirm_submit` — once, when OK is pressed and the second step appears, and not at all when the user checked
321
+ no fields, because then there is nothing to confirm and the action runs straight away.
193
322
 
194
- Similar to button title. Use option `:confirm`
323
+ Setting both does **not** make two requests. `:confirm_submit` takes over, and `:confirm_summary` only supplies
324
+ the wording for the sentence on its second step.
325
+
326
+ Both options in one flow — an update action with `confirm_submit:` and the summary it shows, from the sidebar
327
+ button to the result:
328
+
329
+ ![confirm_summary and confirm_submit](/screenshots/example_confirm_summary.png)
330
+
331
+ ### How can I ask for an extra confirmation before the form is submitted?
332
+
333
+ An action without a form is confirmed by the modal dialog itself — it has no fields and the user just presses OK.
334
+ An action with a form applies changes immediately after OK is pressed. Use the `:confirm_submit` option to turn
335
+ such an action into two steps, where the second one replaces the fields with a summary of what is going to
336
+ happen:
195
337
 
196
338
  ```ruby
197
- scoped_collection_action :scoped_collection_destroy, confirm: 'Delete all phones?'
339
+ scoped_collection_action :scoped_collection_update,
340
+ confirm_submit: true,
341
+ form: -> { { body: 'text' } }
198
342
  ```
199
343
 
344
+ ```text
345
+ Are you sure?
200
346
 
201
- ### Can I replace you pop-up form with my own?
347
+ You are going to update 100500 record(s) with:
348
+ Body: Text here...
349
+ Author_id: Jane
202
350
 
203
- Yes. But also you must take care of mandatory parameters passed to the server.
351
+ [ OK ] [ Back ]
352
+ ```
204
353
 
354
+ "Back" returns to the form with everything the user filled in still there. Only the fields the user checked are
355
+ listed, and when nothing is checked there is nothing to confirm — the action is performed right away. The text of
356
+ the sentence is the `confirm_submit_summary` locale key, or the `:confirm_summary` option when you set it.
357
+
358
+ `true` shows the default message ("Are you sure?"). Pass a String or a Proc to use your own:
359
+
360
+ ```ruby
361
+ scoped_collection_action :scoped_collection_update,
362
+ confirm_submit: 'Update all filtered phones?',
363
+ form: -> { { name: 'text' } }
364
+ ```
365
+
366
+ ### Can I replace the pop-up form with my own?
367
+
368
+ Yes, but you must also take care of the mandatory parameters passed to the server.
205
369
 
206
370
  ```ruby
207
- scoped_collection_action :my_pop_action, class: 'my_popup'
371
+ scoped_collection_action :my_pop_action, class: 'my_popup'
208
372
  ```
209
373
 
210
- Now in HTML page, you have button:
374
+ Now in the HTML page you have a button:
211
375
 
212
376
  ```html
213
- <button class="my_popup" data="{&quot;auth_token&quot;:&quot;2a+KLu5u9McQENspCiep0DGZI6D09fCVXAN9inrwRG0=&quot;,&quot;batch_action&quot;:&quot;my_pop_action&quot;,&quot;confirm&quot;:&quot;Are you sure?&quot;}">My pop action</button>
377
+ <button class="my_popup" data="{&quot;auth_token&quot;:&quot;2a+KLu5u9McQENspCiep0DGZI6D09fCVXAN9inrwRG0=&quot;,&quot;batch_action&quot;:&quot;my_pop_action&quot;,&quot;confirm&quot;:&quot;Are you sure?&quot;}">My pop action</button>
214
378
  ```
215
379
 
216
- Without handler, clicking on the button does nothing.
380
+ Without a handler, clicking the button does nothing.
217
381
 
218
382
  You can render the form in any way you want:
219
- - It can be some popup(Fancybox, Simplemodal, etc.), or some inline collapsible form.
220
- - It can even be a separate full-page.
221
-
222
- One thing is important - how you will send data to server. Generally it should be:
223
383
 
224
- POST request
384
+ - it can be a popup (Fancybox, Simplemodal, etc.), or an inline collapsible form;
385
+ - it can even be a separate full page.
225
386
 
226
- URL: /admin/collection_path/batch_action
387
+ One thing is important — how you send the data to the server. It should be:
227
388
 
228
- with GET params identical to current page
389
+ | | |
390
+ | --- | --- |
391
+ | Method | `POST` |
392
+ | URL | `/admin/collection_path/batch_action` |
393
+ | Query string | identical to the current page |
229
394
 
230
- The easiest way to get them is:
395
+ The easiest way to build it is:
231
396
 
232
397
  ```javascript
233
398
  url = window.location.pathname + '/batch_action' + window.location.search
234
399
  ```
235
400
 
236
- And Request body params should be like:
401
+ And the request body params should look like:
237
402
 
238
- ```
403
+ ```text
239
404
  changes[manufactured_at] = "2015-07-21 18:11"
240
405
  changes[diagonal] = "7"
241
406
  changes[some_filed_name]='new value'
@@ -243,69 +408,73 @@ authenticity_token = "2a+KLu5u9McQENspCiep0DGZI6D09fCVXAN9inrwRG0="
243
408
  batch_action = "my_pop_action"
244
409
  ```
245
410
 
246
- ```authenticity_token``` and ```batch_action``` you can get from data-attribute of the Button.
247
-
411
+ `authenticity_token` and `batch_action` can be taken from the data attribute of the button.
248
412
 
249
413
  Example in JavaScript:
250
414
 
251
415
  ```javascript
252
- url = window.location.pathname + '/batch_action' + window.location.search
253
- form_data = {
254
- changes: {"manufactured_at": "2015-07-21 18:11", "diagonal": "7"},
255
- collection_selection: [],
256
- authenticity_token: "2a+KLu5u9McQENspCiep0DGZI6D09fCVXAN9inrwRG0=",
257
- batch_action: "my_pop_action"
258
- }
259
- $.post(url, form_data).always () ->
260
- window.location.reload()
416
+ url = window.location.pathname + '/batch_action' + window.location.search
417
+ form_data = {
418
+ changes: { "manufactured_at": "2015-07-21 18:11", "diagonal": "7" },
419
+ collection_selection: [],
420
+ authenticity_token: "2a+KLu5u9McQENspCiep0DGZI6D09fCVXAN9inrwRG0=",
421
+ batch_action: "my_pop_action"
422
+ }
423
+ $.post(url, form_data).always(function () {
424
+ window.location.reload();
425
+ });
261
426
  ```
262
427
 
263
- ### How notify user about success and error operations?
428
+ ### How do I notify the user about success and errors?
264
429
 
265
- We recommend using Rails Flash messages.
430
+ We recommend using Rails flash messages.
266
431
 
267
- Example with updating phone diagonal attribute. In this case model Phone has validation:
432
+ Example with updating the phone diagonal attribute. In this case the Phone model has a validation:
268
433
 
269
434
  ```ruby
270
- class Phone < ActiveRecord::Base
271
- validates :diagonal, numericality: { only_integer: true }
272
- end
435
+ class Phone < ActiveRecord::Base
436
+ validates :diagonal, numericality: { only_integer: true }
437
+ end
273
438
  ```
274
439
 
275
440
  ```ruby
276
- scoped_collection_action :change_diagonal, form: { diagonal: 'text' } do
277
- errors = []
278
- scoped_collection_records.find_each do |record|
279
- errors << "#{record.errors.full_messages.join('. ')}" unless record.update(diagonal: params[:changes][:diagonal])
280
- end
281
- if errors.empty?
282
- flash[:notice] = 'Diagonal changed successfully'
283
- else
284
- flash[:error] = errors.join('. ')
285
- end
286
- render nothing: true, status: :no_content
441
+ scoped_collection_action :change_diagonal, form: { diagonal: 'text' } do
442
+ errors = []
443
+ scoped_collection_records.find_each do |record|
444
+ errors << record.errors.full_messages.join('. ') unless record.update(diagonal: params[:changes][:diagonal])
445
+ end
446
+ if errors.empty?
447
+ flash[:notice] = 'Diagonal changed successfully'
448
+ else
449
+ flash[:error] = errors.join('. ')
287
450
  end
451
+ head :no_content
452
+ end
288
453
  ```
289
454
 
290
- When you try to update diagonal with "5.6" you will see flash error:
455
+ When you try to update the diagonal with "5.6" you will see a flash error:
291
456
 
292
- ```
457
+ ```text
293
458
  Diagonal must be an integer.
294
459
  ```
295
460
 
296
- But if you use your custom popup, you can show messages with JS.
461
+ But if you use your own custom popup, you can show messages with JS.
297
462
 
463
+ ### Can I perform an action only on selected items?
298
464
 
299
- ### Can I perform action only on selected items?
465
+ The standard index page of a resource with `batch_action` enabled has a selectable column.
300
466
 
301
- Standard index-page of a resource with batch_action enabled has selectable column.
302
-
303
- If you checked some items and parform any Collection Action, the handler will take care of it. If you write custom actions, you should do like this:
467
+ If you have checked some items and perform any collection action, the handler will take care of it. If you write
468
+ custom actions, you should do it like this:
304
469
 
305
470
  ```ruby
306
- scoped_collection_action :do_something do
307
- scoped_collection_records.find_each do |record|
308
- record.update(name: 'x')
309
- end
471
+ scoped_collection_action :do_something do
472
+ scoped_collection_records.find_each do |record|
473
+ record.update(name: 'x')
310
474
  end
475
+ end
311
476
  ```
477
+
478
+ ## License
479
+
480
+ Released under the [MIT License](LICENSE.txt).