mensa 0.9.8 → 0.10.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 +4 -4
- data/.shipkit.yml +1 -1
- data/CHANGELOG.md +31 -0
- data/Gemfile.lock +1 -1
- data/README.md +117 -0
- data/app/components/mensa/row_action/component.html.erb +1 -1
- data/app/components/mensa/row_action/component.rb +16 -0
- data/app/components/mensa/selection/component_controller.js +194 -31
- data/app/components/mensa/table/component_controller.js +24 -16
- data/app/components/mensa/view/component.css +70 -1
- data/app/components/mensa/view/component.html.erb +19 -3
- data/app/controllers/mensa/tables/batch_actions_controller.rb +73 -10
- data/app/controllers/mensa/tables/row_actions_controller.rb +25 -0
- data/app/models/mensa/batch.rb +54 -0
- data/app/tables/mensa/action.rb +12 -0
- data/app/tables/mensa/base.rb +13 -2
- data/app/tables/mensa/batch_action.rb +33 -1
- data/app/tables/mensa/config/action_dsl.rb +1 -0
- data/app/tables/mensa/config/batch_dsl.rb +6 -0
- data/app/views/mensa/tables/batch_actions/new.html.erb +38 -0
- data/config/locales/en.yml +10 -0
- data/config/locales/nl.yml +10 -0
- data/config/routes.rb +2 -1
- data/lib/mensa/configuration.rb +6 -0
- data/lib/mensa/version.rb +1 -1
- metadata +4 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 642547e019d4d27af7e49926f377a843a4e050096e6c285a766c0fb0c34e0537
|
|
4
|
+
data.tar.gz: ea82571ea8164b3aa1946b10f2573f8d9831bcc8e1afa3b290a6b4edd46a87ea
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ff4060b4b39bb1299e0d437a859a7b5572b8b0e5c58ac277d7e9d90fa71581d4769c34b48fa1e1e2830485e492a7662e081f3aba631956b2857b64ff8ac9c616
|
|
7
|
+
data.tar.gz: 6c984794fe3ba477a4acaf6b4b320263f841e68492fc87d90a53bd08f70110b8ac048234735b911bd64b06d2eb594a348b957cad530fa37aa62c8610f6088802
|
data/.shipkit.yml
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
---
|
|
2
|
-
previous_version: 0.9.
|
|
2
|
+
previous_version: 0.9.9
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,36 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.10.0
|
|
4
|
+
|
|
5
|
+
- Batch actions can work on every row matching the current search and filters:
|
|
6
|
+
once the whole page is checked, the batch bar offers "Select all (N)", and
|
|
7
|
+
then "Only this page" to go back. Mensa finds those rows again from
|
|
8
|
+
the table's state, like exports do. An action without a form asks to
|
|
9
|
+
confirm before it runs on all of them.
|
|
10
|
+
- The records a batch action gets are always limited to the table's scope.
|
|
11
|
+
Before, they were looked up by the submitted ids in the whole model, so a
|
|
12
|
+
tampered request could reach records the table doesn't show.
|
|
13
|
+
- A batch action can have a `form` partial, which Mensa shows in a dialog
|
|
14
|
+
before running the action, rendered with the app's form builder. Its
|
|
15
|
+
`process` can take a second argument, `batch`, with the submitted fields
|
|
16
|
+
(`batch.params`), `errors` that keep the dialog open, and
|
|
17
|
+
`redirect_to(url, notice:)`. Such a block gets the records as a relation;
|
|
18
|
+
one that only takes the records still gets an Array.
|
|
19
|
+
- Batch actions can `confirm` before running, and `show` decides who sees and
|
|
20
|
+
may run them.
|
|
21
|
+
- `process` blocks of batch actions run in a view context, so route helpers
|
|
22
|
+
work in them.
|
|
23
|
+
|
|
24
|
+
## 0.9.9
|
|
25
|
+
|
|
26
|
+
- A row action can `process` the record instead of linking somewhere, for
|
|
27
|
+
small actions that don't need a controller of their own:
|
|
28
|
+
`action(:activate) { process { |user| user.activate! } }`. It renders as a
|
|
29
|
+
Turbo POST link (so it works in a table inside a host form) to a Mensa
|
|
30
|
+
endpoint, which finds the record through the table's scope and its params,
|
|
31
|
+
runs the block when the action's `show` allows it, and redirects back.
|
|
32
|
+
`link_attributes` still apply, e.g. for a `turbo_confirm`.
|
|
33
|
+
|
|
3
34
|
## 0.9.8
|
|
4
35
|
|
|
5
36
|
- The export, save-view and rename-view dialogs are in the middle of the
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
|
@@ -75,6 +75,18 @@ class UserTable < ApplicationTable
|
|
|
75
75
|
show ->(user) { true }
|
|
76
76
|
end
|
|
77
77
|
|
|
78
|
+
# Or run a bit of code with the record, without a controller of your own.
|
|
79
|
+
# Mensa POSTs to its own endpoint, which finds the record through the table's
|
|
80
|
+
# scope (and only runs the action when `show` allows it), then redirects back.
|
|
81
|
+
action :activate do
|
|
82
|
+
title "Activate"
|
|
83
|
+
icon "fa-regular fa-check"
|
|
84
|
+
show ->(user) { !user.active? }
|
|
85
|
+
process do |user|
|
|
86
|
+
user.activate!
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
78
90
|
link { |user| edit_user_path(user) }
|
|
79
91
|
|
|
80
92
|
show_header true
|
|
@@ -102,6 +114,21 @@ class UserTable < ApplicationTable
|
|
|
102
114
|
end
|
|
103
115
|
end
|
|
104
116
|
|
|
117
|
+
# Give a batch action a form and Mensa shows it in a dialog first, see
|
|
118
|
+
# "Batch actions" below
|
|
119
|
+
batch :archive do
|
|
120
|
+
title "Archive"
|
|
121
|
+
form "users/archive_form"
|
|
122
|
+
show { |table| table.current_user.admin? }
|
|
123
|
+
process do |users, batch|
|
|
124
|
+
reason = batch.params[:reason]
|
|
125
|
+
next batch.errors.add(:reason, :blank) if reason.blank?
|
|
126
|
+
|
|
127
|
+
users.update_all(archived_reason: reason)
|
|
128
|
+
batch.redirect_to users_path, notice: "#{batch.count} users archived"
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
|
|
105
132
|
scope do
|
|
106
133
|
User.all
|
|
107
134
|
end
|
|
@@ -425,6 +452,96 @@ actions and exports (including recurring exports, which store them on the
|
|
|
425
452
|
`Mensa::Export`). Exports and the state the browser remembers (filters, order,
|
|
426
453
|
columns) are kept per table *and* params.
|
|
427
454
|
|
|
455
|
+
### Batch actions
|
|
456
|
+
|
|
457
|
+
A table with batch actions gets a checkbox per row and a bar with one button
|
|
458
|
+
per action. Once every row on the page is checked, the bar offers to select
|
|
459
|
+
all rows matching the current search and filters, not only those on the page,
|
|
460
|
+
and then to go back to only the page. An action without a form asks to confirm
|
|
461
|
+
before it runs on all matching rows, as those are more than the user sees.
|
|
462
|
+
|
|
463
|
+
`process` gets the selected records, always limited to the table's scope: ids
|
|
464
|
+
of records the table doesn't show are ignored.
|
|
465
|
+
|
|
466
|
+
```ruby
|
|
467
|
+
batch :confirm do
|
|
468
|
+
title "Confirm"
|
|
469
|
+
confirm "Confirm the selected users?"
|
|
470
|
+
process do |users|
|
|
471
|
+
users.each(&:confirm!)
|
|
472
|
+
end
|
|
473
|
+
end
|
|
474
|
+
```
|
|
475
|
+
|
|
476
|
+
A block that only takes the records gets them as an Array, as before. One that
|
|
477
|
+
also takes the `batch` gets a relation, so it can use `find_each` or
|
|
478
|
+
`update_all` on thousands of records:
|
|
479
|
+
|
|
480
|
+
```ruby
|
|
481
|
+
process do |users, batch|
|
|
482
|
+
users.update_all(confirmed_at: Time.current)
|
|
483
|
+
end
|
|
484
|
+
```
|
|
485
|
+
|
|
486
|
+
The block runs in a view context, so route helpers such as `users_path` work
|
|
487
|
+
in it, like in links.
|
|
488
|
+
|
|
489
|
+
#### Dialogs
|
|
490
|
+
|
|
491
|
+
Give a batch action a `form` partial and Mensa shows it in a dialog before
|
|
492
|
+
running the action:
|
|
493
|
+
|
|
494
|
+
```ruby
|
|
495
|
+
batch :archive do
|
|
496
|
+
title "Archive"
|
|
497
|
+
form "users/archive_form"
|
|
498
|
+
process do |users, batch|
|
|
499
|
+
reason = batch.params[:reason]
|
|
500
|
+
next batch.errors.add(:reason, :blank) if reason.blank?
|
|
501
|
+
|
|
502
|
+
users.update_all(archived_reason: reason)
|
|
503
|
+
batch.redirect_to users_path, notice: "#{batch.count} users archived"
|
|
504
|
+
end
|
|
505
|
+
end
|
|
506
|
+
```
|
|
507
|
+
|
|
508
|
+
The partial is rendered in Mensa's form with your app's form builder, so you
|
|
509
|
+
can use your own inputs:
|
|
510
|
+
|
|
511
|
+
```erb
|
|
512
|
+
<%# app/views/users/_archive_form.html.erb %>
|
|
513
|
+
<%= f.label :reason %>
|
|
514
|
+
<%= f.text_area :reason %>
|
|
515
|
+
```
|
|
516
|
+
|
|
517
|
+
It gets `f` (scoped to `batch`), `table`, `count` (the number of selected
|
|
518
|
+
records) and `batch` as locals. The dialog is added to the page's `<body>`, so
|
|
519
|
+
it also works for a table inside a host `<form>`.
|
|
520
|
+
|
|
521
|
+
What the form submits is in `batch.params`, which only holds the fields under
|
|
522
|
+
`batch[...]`. The table's own params are in `batch.table.params`.
|
|
523
|
+
|
|
524
|
+
Adding to `batch.errors` keeps the dialog open and shows the errors, with the
|
|
525
|
+
values that were filled in. Without errors, Mensa redirects back to the table,
|
|
526
|
+
or to where the action sends it:
|
|
527
|
+
|
|
528
|
+
```ruby
|
|
529
|
+
batch.redirect_to users_path, notice: "#{batch.count} users archived"
|
|
530
|
+
batch.redirect_to notice: "Done" # back to the table, with a notice
|
|
531
|
+
```
|
|
532
|
+
|
|
533
|
+
`batch` also has `user`, `table`, `count` and `records`.
|
|
534
|
+
|
|
535
|
+
#### Showing batch actions
|
|
536
|
+
|
|
537
|
+
`show` decides who sees a batch action. It's called with the table, in a view
|
|
538
|
+
context. Mensa checks it again when the action is submitted, so it also guards
|
|
539
|
+
the endpoint:
|
|
540
|
+
|
|
541
|
+
```ruby
|
|
542
|
+
show { |table| table.current_user.admin? }
|
|
543
|
+
```
|
|
544
|
+
|
|
428
545
|
### Exports
|
|
429
546
|
|
|
430
547
|
Exporting is built into the table's control bar. Clicking the export button opens
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
<% if action.show.call(row.record) %>
|
|
2
|
-
<%= link_to(
|
|
2
|
+
<%= link_to(href, {class: "action", title: action.title}.deep_merge(attributes)) do %>
|
|
3
3
|
|
|
4
4
|
<% if icon = action.icon(row.record) %>
|
|
5
5
|
<i class="fa <%= icon %>"></i>
|
|
@@ -14,6 +14,22 @@ module Mensa
|
|
|
14
14
|
@row = row
|
|
15
15
|
@action = action
|
|
16
16
|
end
|
|
17
|
+
|
|
18
|
+
def href
|
|
19
|
+
if action.process?
|
|
20
|
+
# A Turbo POST link rather than button_to, a table can be inside a host form
|
|
21
|
+
table.path_with_params(helpers.mensa.table_row_action_path(table.name, action.name, row.record.id))
|
|
22
|
+
elsif action.link
|
|
23
|
+
table.original_view_context.instance_exec(row.record, &action.link)
|
|
24
|
+
else
|
|
25
|
+
""
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def attributes
|
|
30
|
+
attributes = action.link_attributes || {}
|
|
31
|
+
action.process? ? {data: {turbo_method: :post}}.deep_merge(attributes) : attributes
|
|
32
|
+
end
|
|
17
33
|
end
|
|
18
34
|
end
|
|
19
35
|
end
|
|
@@ -8,6 +8,10 @@ import ApplicationController from "mensa/controllers/application_controller";
|
|
|
8
8
|
// absolutely positioned at top:0/left:0 inside that wrapper. Its width is
|
|
9
9
|
// set to the <table>'s offsetWidth so it covers the full table — including
|
|
10
10
|
// any overflow that causes horizontal scrolling.
|
|
11
|
+
//
|
|
12
|
+
// A selection is either the checked rows, or — after "Select all" — every row
|
|
13
|
+
// matching the table's current search and filters, which the server works out
|
|
14
|
+
// again from the table's state.
|
|
11
15
|
export default class SelectionController extends ApplicationController {
|
|
12
16
|
static targets = [
|
|
13
17
|
"headerCheckbox", // select-all checkbox in the header <th>
|
|
@@ -15,14 +19,22 @@ export default class SelectionController extends ApplicationController {
|
|
|
15
19
|
"batchBar", // absolutely-positioned div overlaying the header row
|
|
16
20
|
"selectedCount", // span inside the batch bar showing "N selected"
|
|
17
21
|
"batchAllCheckbox", // checkbox inside the batch bar (clicking deselects all)
|
|
22
|
+
"selectAllButton", // button in the batch bar selecting all matching rows
|
|
23
|
+
"selectAllLabel", // its text, "Select all N" or "Select only this page"
|
|
24
|
+
"selectAllIcon", // its icon when it selects all matching rows
|
|
25
|
+
"selectPageIcon", // its icon when it goes back to this page
|
|
18
26
|
];
|
|
19
27
|
|
|
20
28
|
static values = {
|
|
21
29
|
batchUrl: String, // POST endpoint for batch actions
|
|
30
|
+
newUrl: String, // GET endpoint for a batch action's dialog
|
|
31
|
+
total: Number, // number of rows matching the search and filters
|
|
32
|
+
texts: Object, // translations, with {count} for the number
|
|
22
33
|
};
|
|
23
34
|
|
|
24
35
|
connect() {
|
|
25
36
|
super.connect();
|
|
37
|
+
this.allMatching = false;
|
|
26
38
|
this.resizeHandler = () => this.syncBatchBarWidth();
|
|
27
39
|
window.addEventListener("resize", this.resizeHandler);
|
|
28
40
|
}
|
|
@@ -46,17 +58,27 @@ export default class SelectionController extends ApplicationController {
|
|
|
46
58
|
this.rowCheckboxTargets.forEach((cb) => {
|
|
47
59
|
cb.checked = checked;
|
|
48
60
|
});
|
|
61
|
+
this.allMatching = false;
|
|
49
62
|
this.updateSelectionState();
|
|
50
63
|
}
|
|
51
64
|
|
|
52
65
|
// Called when an individual row checkbox changes.
|
|
53
66
|
toggleRow() {
|
|
67
|
+
this.allMatching = false;
|
|
54
68
|
this.updateSelectionState();
|
|
55
69
|
}
|
|
56
70
|
|
|
57
71
|
// Called when the batch bar's checkbox is clicked — always deselects all.
|
|
58
72
|
deselectAll() {
|
|
59
73
|
this.rowCheckboxTargets.forEach((cb) => (cb.checked = false));
|
|
74
|
+
this.allMatching = false;
|
|
75
|
+
this.updateSelectionState();
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Switches between every row matching the search and filters, and only
|
|
79
|
+
// the rows on this page.
|
|
80
|
+
toggleAllMatching() {
|
|
81
|
+
this.allMatching = !this.allMatching;
|
|
60
82
|
this.updateSelectionState();
|
|
61
83
|
}
|
|
62
84
|
|
|
@@ -66,51 +88,170 @@ export default class SelectionController extends ApplicationController {
|
|
|
66
88
|
event.stopPropagation();
|
|
67
89
|
}
|
|
68
90
|
|
|
69
|
-
//
|
|
70
|
-
//
|
|
91
|
+
// Runs the chosen batch action: opens its dialog when it has a form,
|
|
92
|
+
// otherwise submits it (after confirming, when it asks to). An action on
|
|
93
|
+
// all matching rows always asks first, as those are more than the user
|
|
94
|
+
// sees: the dialog shows how many, otherwise a confirm does.
|
|
71
95
|
executeBatch(event) {
|
|
72
96
|
event.preventDefault();
|
|
73
97
|
|
|
74
|
-
const
|
|
75
|
-
|
|
76
|
-
.filter((cb) => cb.checked)
|
|
77
|
-
.map((cb) => cb.value);
|
|
78
|
-
|
|
79
|
-
if (selectedIds.length === 0) return;
|
|
98
|
+
const { batchAction, form, confirm, title } = event.params;
|
|
99
|
+
if (this.selectionCount === 0) return;
|
|
80
100
|
if (!this.batchUrlValue) return;
|
|
81
101
|
|
|
102
|
+
if (form) {
|
|
103
|
+
this.openDialog(batchAction);
|
|
104
|
+
} else if (this.allMatching && !confirm) {
|
|
105
|
+
this.submitBatch(
|
|
106
|
+
batchAction,
|
|
107
|
+
this.text("confirm_all", this.totalValue).replace("{action}", title),
|
|
108
|
+
);
|
|
109
|
+
} else {
|
|
110
|
+
this.submitBatch(batchAction, confirm);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Builds and submits a hidden form for the batch action, through Turbo so
|
|
115
|
+
// data-turbo-confirm and redirects work as usual.
|
|
116
|
+
submitBatch(actionName, confirm) {
|
|
82
117
|
const form = document.createElement("form");
|
|
83
118
|
form.method = "post";
|
|
84
119
|
form.action = this.batchUrlValue;
|
|
120
|
+
form.hidden = true;
|
|
121
|
+
form.dataset.turbo = "true";
|
|
122
|
+
if (confirm) form.dataset.turboConfirm = confirm;
|
|
85
123
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
csrfInput.value = csrfMeta.getAttribute("content");
|
|
93
|
-
form.appendChild(csrfInput);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
// Batch action name
|
|
97
|
-
const actionInput = document.createElement("input");
|
|
98
|
-
actionInput.type = "hidden";
|
|
99
|
-
actionInput.name = "batch_action_name";
|
|
100
|
-
actionInput.value = actionName;
|
|
101
|
-
form.appendChild(actionInput);
|
|
102
|
-
|
|
103
|
-
// One hidden input per selected record ID
|
|
104
|
-
selectedIds.forEach((id) => {
|
|
124
|
+
const entries = [
|
|
125
|
+
["authenticity_token", this.csrfToken],
|
|
126
|
+
["batch_action_name", actionName],
|
|
127
|
+
...this.selectionEntries(),
|
|
128
|
+
];
|
|
129
|
+
entries.forEach(([name, value]) => {
|
|
105
130
|
const input = document.createElement("input");
|
|
106
131
|
input.type = "hidden";
|
|
107
|
-
input.name =
|
|
108
|
-
input.value =
|
|
132
|
+
input.name = name;
|
|
133
|
+
input.value = value;
|
|
109
134
|
form.appendChild(input);
|
|
110
135
|
});
|
|
111
136
|
|
|
112
137
|
document.body.appendChild(form);
|
|
113
|
-
form.
|
|
138
|
+
form.requestSubmit();
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Opens a dialog with the batch action's form, loaded into a turbo-frame.
|
|
142
|
+
// The dialog is added to <body>, so its form isn't nested in a form the
|
|
143
|
+
// table may be in. The selection is added to what the form submits.
|
|
144
|
+
openDialog(actionName) {
|
|
145
|
+
const url = new URL(this.newUrlValue, window.location.origin);
|
|
146
|
+
url.searchParams.set("batch_action_name", actionName);
|
|
147
|
+
url.searchParams.set("count", this.selectionCount);
|
|
148
|
+
|
|
149
|
+
const entries = this.selectionEntries();
|
|
150
|
+
const dialog = document.createElement("dialog");
|
|
151
|
+
dialog.className = "mensa-batch-dialog";
|
|
152
|
+
|
|
153
|
+
const frame = document.createElement("turbo-frame");
|
|
154
|
+
frame.id = "mensa-batch-dialog";
|
|
155
|
+
frame.setAttribute("src", url.pathname + url.search);
|
|
156
|
+
dialog.appendChild(frame);
|
|
157
|
+
|
|
158
|
+
dialog.addEventListener(
|
|
159
|
+
"formdata",
|
|
160
|
+
(event) =>
|
|
161
|
+
entries.forEach(([name, value]) =>
|
|
162
|
+
event.formData.append(name, value),
|
|
163
|
+
),
|
|
164
|
+
true,
|
|
165
|
+
);
|
|
166
|
+
// A response without the dialog's frame (a redirect after the action
|
|
167
|
+
// ran) is shown as a page
|
|
168
|
+
dialog.addEventListener("turbo:frame-missing", (event) => {
|
|
169
|
+
event.preventDefault();
|
|
170
|
+
dialog.close();
|
|
171
|
+
event.detail.visit(event.detail.response);
|
|
172
|
+
});
|
|
173
|
+
dialog.addEventListener("click", (event) => {
|
|
174
|
+
if (
|
|
175
|
+
event.target === dialog ||
|
|
176
|
+
event.target.closest("[data-mensa-batch-dialog-close]")
|
|
177
|
+
) {
|
|
178
|
+
dialog.close();
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
dialog.addEventListener("close", () => dialog.remove());
|
|
182
|
+
|
|
183
|
+
document.body.appendChild(dialog);
|
|
184
|
+
dialog.showModal();
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// What identifies the selection to the server: the ids, or the table's
|
|
188
|
+
// state to find all matching rows with
|
|
189
|
+
selectionEntries() {
|
|
190
|
+
if (!this.allMatching) {
|
|
191
|
+
return this.checkedIds.map((id) => ["record_ids[]", id]);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const state = this.tableController?.requestState() || {};
|
|
195
|
+
const entries = [["select_all", "1"]];
|
|
196
|
+
if (state.table_view_id) {
|
|
197
|
+
entries.push(["table_view_id", state.table_view_id]);
|
|
198
|
+
}
|
|
199
|
+
if (state.query) entries.push(["query", state.query]);
|
|
200
|
+
// Blank filters means "explicitly none", not the view's
|
|
201
|
+
const filters = state.filters || {};
|
|
202
|
+
if (Object.keys(filters).length === 0) {
|
|
203
|
+
entries.push(["filters", ""]);
|
|
204
|
+
} else {
|
|
205
|
+
this.appendEntries(entries, "filters", filters);
|
|
206
|
+
}
|
|
207
|
+
return entries;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// Flattens a nested object into Rails-style form names
|
|
211
|
+
appendEntries(entries, name, value) {
|
|
212
|
+
if (Array.isArray(value)) {
|
|
213
|
+
value.forEach((item) => this.appendEntries(entries, `${name}[]`, item));
|
|
214
|
+
} else if (value !== null && typeof value === "object") {
|
|
215
|
+
Object.entries(value).forEach(([key, item]) =>
|
|
216
|
+
this.appendEntries(entries, `${name}[${key}]`, item),
|
|
217
|
+
);
|
|
218
|
+
} else if (value !== undefined && value !== null) {
|
|
219
|
+
entries.push([name, String(value)]);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
get checkedIds() {
|
|
224
|
+
return this.rowCheckboxTargets
|
|
225
|
+
.filter((cb) => cb.checked)
|
|
226
|
+
.map((cb) => cb.value);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
get selectionCount() {
|
|
230
|
+
return this.allMatching ? this.totalValue : this.checkedIds.length;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
get tableController() {
|
|
234
|
+
const element = this.element.closest("[data-controller~='mensa-table']");
|
|
235
|
+
if (!element) return null;
|
|
236
|
+
return this.application.getControllerForElementAndIdentifier(
|
|
237
|
+
element,
|
|
238
|
+
"mensa-table",
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
get csrfToken() {
|
|
243
|
+
return (
|
|
244
|
+
document.querySelector('meta[name="csrf-token"]')?.getAttribute("content") ||
|
|
245
|
+
""
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
text(key, count) {
|
|
250
|
+
const template = this.textsValue[key] || `{count} ${key}`;
|
|
251
|
+
const number = Number(count).toLocaleString(
|
|
252
|
+
document.documentElement.lang || undefined,
|
|
253
|
+
);
|
|
254
|
+
return template.replace("{count}", number);
|
|
114
255
|
}
|
|
115
256
|
|
|
116
257
|
// Syncs the header checkbox state and shows/hides the batch bar overlay.
|
|
@@ -141,7 +282,29 @@ export default class SelectionController extends ApplicationController {
|
|
|
141
282
|
|
|
142
283
|
// Update "N selected" label
|
|
143
284
|
if (this.hasSelectedCountTarget) {
|
|
144
|
-
this.selectedCountTarget.textContent =
|
|
285
|
+
this.selectedCountTarget.textContent = this.allMatching
|
|
286
|
+
? this.text("all_selected", this.totalValue)
|
|
287
|
+
: this.text("selected", checked.length);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// Once the whole page is selected, offer to select all matching rows,
|
|
291
|
+
// and then to go back to only this page
|
|
292
|
+
if (this.hasSelectAllButtonTarget) {
|
|
293
|
+
const offer = allSelected && this.totalValue > all.length;
|
|
294
|
+
const key = this.allMatching ? "select_page" : "select_all";
|
|
295
|
+
const count = this.allMatching ? all.length : this.totalValue;
|
|
296
|
+
if (this.hasSelectAllLabelTarget) {
|
|
297
|
+
this.selectAllLabelTarget.textContent = this.text(key, count);
|
|
298
|
+
}
|
|
299
|
+
// Both icons are rendered, as Font Awesome may have replaced them
|
|
300
|
+
// with an <svg>, whose class can't simply be swapped
|
|
301
|
+
if (this.hasSelectAllIconTarget) {
|
|
302
|
+
this.selectAllIconTarget.hidden = this.allMatching;
|
|
303
|
+
}
|
|
304
|
+
if (this.hasSelectPageIconTarget) {
|
|
305
|
+
this.selectPageIconTarget.hidden = !this.allMatching;
|
|
306
|
+
}
|
|
307
|
+
this.selectAllButtonTarget.classList.toggle("hidden", !offer);
|
|
145
308
|
}
|
|
146
309
|
}
|
|
147
310
|
}
|
|
@@ -589,33 +589,41 @@ export default class TableComponentController extends ApplicationController {
|
|
|
589
589
|
? this._exportField("repeat") || ""
|
|
590
590
|
: "";
|
|
591
591
|
|
|
592
|
-
const state = this.currentViewState();
|
|
593
|
-
const nav = this.hasMensaFilterPillListOutlet
|
|
594
|
-
? this.mensaFilterPillListOutlet.currentRequestState()
|
|
595
|
-
: { page: "", query: "", view: "" };
|
|
596
|
-
const view = this.hasMensaFilterPillListOutlet
|
|
597
|
-
? this.mensaFilterPillListOutlet.loadView() || nav.view
|
|
598
|
-
: "";
|
|
599
|
-
|
|
600
592
|
post(this.exportsUrlValue, {
|
|
601
593
|
body: JSON.stringify({
|
|
602
594
|
scope,
|
|
603
595
|
export_format: exportFormat,
|
|
604
596
|
repeat,
|
|
605
|
-
|
|
606
|
-
page: nav.page,
|
|
607
|
-
query: state.query || nav.query,
|
|
608
|
-
filters: state.filters,
|
|
609
|
-
order: state.order,
|
|
610
|
-
column_order: state.column_order,
|
|
611
|
-
hidden_columns: state.hidden_columns,
|
|
612
|
-
group_by: state.group_by,
|
|
597
|
+
...this.requestState(),
|
|
613
598
|
}),
|
|
614
599
|
contentType: "application/json",
|
|
615
600
|
responseKind: "turbo-stream",
|
|
616
601
|
});
|
|
617
602
|
}
|
|
618
603
|
|
|
604
|
+
// The table as the user sees it, for endpoints that rebuild it (exports,
|
|
605
|
+
// batch actions on all matching rows)
|
|
606
|
+
requestState() {
|
|
607
|
+
const state = this.currentViewState();
|
|
608
|
+
const nav = this.hasMensaFilterPillListOutlet
|
|
609
|
+
? this.mensaFilterPillListOutlet.currentRequestState()
|
|
610
|
+
: { page: "", query: "", view: "" };
|
|
611
|
+
const view = this.hasMensaFilterPillListOutlet
|
|
612
|
+
? this.mensaFilterPillListOutlet.loadView() || nav.view
|
|
613
|
+
: "";
|
|
614
|
+
|
|
615
|
+
return {
|
|
616
|
+
table_view_id: view,
|
|
617
|
+
page: nav.page,
|
|
618
|
+
query: state.query || nav.query,
|
|
619
|
+
filters: state.filters,
|
|
620
|
+
order: state.order,
|
|
621
|
+
column_order: state.column_order,
|
|
622
|
+
hidden_columns: state.hidden_columns,
|
|
623
|
+
group_by: state.group_by,
|
|
624
|
+
};
|
|
625
|
+
}
|
|
626
|
+
|
|
619
627
|
deleteExport(event) {
|
|
620
628
|
if (event) event.preventDefault();
|
|
621
629
|
const url = event.currentTarget.dataset.url;
|
|
@@ -17,13 +17,82 @@
|
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
.mensa-batch-bar__count {
|
|
20
|
-
@apply text-xs font-medium text-gray-700 dark:text-gray-200
|
|
20
|
+
@apply text-xs font-medium text-gray-700 dark:text-gray-200;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/* Changes the selection, so it looks different from the action buttons, and
|
|
24
|
+
is set apart from them by a divider. The bar's gap spaces both sides of it
|
|
25
|
+
equally, so neither has a margin. */
|
|
26
|
+
.mensa-batch-bar__select-all {
|
|
27
|
+
@apply inline-flex items-center gap-1 rounded px-3 py-1 text-xs font-medium leading-none text-primary-700 dark:text-primary-200 bg-primary-50 dark:bg-primary-800 ring-1 ring-inset ring-primary-200 dark:ring-primary-600 hover:bg-primary-100 dark:hover:bg-primary-700 focus:outline-none transition-colors cursor-pointer;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
.mensa-batch-bar__divider {
|
|
31
|
+
@apply h-4 w-px bg-gray-300 dark:bg-gray-600;
|
|
21
32
|
}
|
|
22
33
|
|
|
23
34
|
.mensa-batch-bar__button {
|
|
24
35
|
@apply inline-flex items-center gap-1 rounded px-3 py-1 text-xs font-medium text-gray-700 dark:text-gray-200 ring-1 ring-inset ring-gray-300 dark:ring-gray-500 bg-white dark:bg-gray-600 hover:bg-gray-50 dark:hover:bg-gray-500 focus:outline-none transition-colors cursor-pointer leading-none;
|
|
25
36
|
}
|
|
26
37
|
|
|
38
|
+
/* Added to <body> by the selection controller, so not inside .mensa-table */
|
|
39
|
+
.mensa-batch-dialog {
|
|
40
|
+
@apply m-auto w-[34rem] max-w-[92vw] max-h-[85vh] rounded-2xl border-0 p-0 bg-white dark:bg-gray-800 shadow-2xl ring-1 ring-black/5 dark:ring-white/10 text-gray-900 dark:text-gray-100;
|
|
41
|
+
|
|
42
|
+
&::backdrop {
|
|
43
|
+
@apply bg-gray-900/40;
|
|
44
|
+
backdrop-filter: blur(2px);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
.mensa-batch-dialog__panel {
|
|
49
|
+
@apply flex flex-col max-h-[85vh] overflow-hidden;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
.mensa-batch-dialog__header {
|
|
53
|
+
@apply flex items-center justify-between gap-4 px-6 py-4 border-b border-gray-200 dark:border-gray-700;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
.mensa-batch-dialog__title {
|
|
57
|
+
@apply text-lg font-semibold leading-6 text-gray-900 dark:text-gray-100;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
.mensa-batch-dialog__close {
|
|
61
|
+
@apply cursor-pointer text-gray-400 hover:text-gray-600 dark:hover:text-gray-200 focus:outline-none;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
.mensa-batch-dialog__body {
|
|
65
|
+
@apply flex flex-col gap-4 p-6 min-h-0 overflow-y-auto;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
.mensa-batch-dialog__description {
|
|
69
|
+
@apply text-sm text-gray-700 dark:text-gray-300;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
.mensa-batch-dialog__count {
|
|
73
|
+
@apply text-sm text-gray-500 dark:text-gray-400;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
.mensa-batch-dialog__errors {
|
|
77
|
+
@apply list-disc rounded-lg bg-red-50 dark:bg-red-900/30 py-3 pr-3 pl-8 text-sm text-red-700 dark:text-red-300;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
.mensa-batch-dialog__actions {
|
|
81
|
+
@apply flex justify-end gap-2 px-6 py-4 border-t border-gray-200 dark:border-gray-700;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
.mensa-batch-dialog__button {
|
|
85
|
+
@apply inline-flex items-center gap-1.5 rounded-lg px-3.5 py-2 text-sm font-semibold shadow-sm focus:outline-none focus:ring-2 focus:ring-offset-2 dark:focus:ring-offset-gray-800 transition-colors cursor-pointer;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
.mensa-batch-dialog__button--secondary {
|
|
89
|
+
@apply bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100 ring-1 ring-inset ring-gray-300 dark:ring-gray-600 hover:bg-gray-50 dark:hover:bg-gray-600 focus:ring-gray-400;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
.mensa-batch-dialog__button--primary {
|
|
93
|
+
@apply bg-primary-600 text-white hover:bg-primary-500 focus:ring-primary-500;
|
|
94
|
+
}
|
|
95
|
+
|
|
27
96
|
.mensa-table {
|
|
28
97
|
@apply bg-white dark:bg-gray-800 dark:border-gray-800;
|
|
29
98
|
|
|
@@ -4,14 +4,30 @@
|
|
|
4
4
|
<div class="overflow-y-auto relative"
|
|
5
5
|
data-mensa-table-target="scrollContainer"
|
|
6
6
|
data-controller="mensa-selection"
|
|
7
|
-
|
|
7
|
+
<% if table.batch_actions? %>
|
|
8
|
+
data-mensa-selection-batch-url-value="<%= table.path_with_params(helpers.mensa.table_batch_actions_path(table.name)) %>"
|
|
9
|
+
data-mensa-selection-new-url-value="<%= table.path_with_params(helpers.mensa.new_table_batch_action_path(table.name)) %>"
|
|
10
|
+
data-mensa-selection-total-value="<%= table.pagy_details&.count.to_i %>"
|
|
11
|
+
data-mensa-selection-texts-value="<%= %i[selected all_selected select_all select_page confirm_all].to_h { |key| [key, t("mensa.batch_actions.#{key}", count: "{count}", action: "{action}")] }.to_json %>"
|
|
12
|
+
<% end %>>
|
|
8
13
|
<% if table.batch_actions? %>
|
|
9
14
|
<div class="mensa-batch-bar hidden" data-mensa-selection-target="batchBar">
|
|
10
15
|
<div class="mensa-batch-bar__content">
|
|
11
16
|
<input class="mensa-table__select-all" type="checkbox" data-action="click->mensa-selection#deselectAll" data-mensa-selection-target="batchAllCheckbox">
|
|
12
17
|
<span class="mensa-batch-bar__count" data-mensa-selection-target="selectedCount"></span>
|
|
13
|
-
|
|
14
|
-
<
|
|
18
|
+
<button class="mensa-batch-bar__select-all hidden" type="button" data-action="click->mensa-selection#toggleAllMatching" data-mensa-selection-target="selectAllButton">
|
|
19
|
+
<span data-mensa-selection-target="selectAllIcon"><i class="<%= Mensa.config.icons[:batch_select_all] %>"></i></span>
|
|
20
|
+
<span data-mensa-selection-target="selectPageIcon" hidden><i class="<%= Mensa.config.icons[:batch_select_page] %>"></i></span>
|
|
21
|
+
<span data-mensa-selection-target="selectAllLabel"></span>
|
|
22
|
+
</button>
|
|
23
|
+
<span class="mensa-batch-bar__divider" aria-hidden="true"></span>
|
|
24
|
+
<% table.visible_batch_actions.each do |batch_action| %>
|
|
25
|
+
<button class="mensa-batch-bar__button" type="button"
|
|
26
|
+
data-action="click->mensa-selection#executeBatch"
|
|
27
|
+
data-mensa-selection-batch-action-param="<%= batch_action.name %>"
|
|
28
|
+
data-mensa-selection-form-param="<%= batch_action.form? %>"
|
|
29
|
+
data-mensa-selection-title-param="<%= batch_action.title %>"
|
|
30
|
+
<% if batch_action.confirm.present? %>data-mensa-selection-confirm-param="<%= batch_action.confirm %>"<% end %>>
|
|
15
31
|
<%= batch_action.title %>
|
|
16
32
|
</button>
|
|
17
33
|
<% end %>
|
|
@@ -2,22 +2,85 @@
|
|
|
2
2
|
|
|
3
3
|
module Mensa
|
|
4
4
|
module Tables
|
|
5
|
+
# Runs batch actions on the selected records: the checked rows
|
|
6
|
+
# (record_ids[]), or every row matching the table's current search and
|
|
7
|
+
# filters (select_all). Either way only records in the table's scope.
|
|
8
|
+
#
|
|
9
|
+
# Batch actions with a form first show it in a dialog, which is loaded
|
|
10
|
+
# from #new into the mensa-batch-dialog turbo-frame.
|
|
5
11
|
class BatchActionsController < ::ApplicationController
|
|
12
|
+
DIALOG_FRAME = "mensa-batch-dialog"
|
|
13
|
+
|
|
14
|
+
before_action :set_batch_action
|
|
15
|
+
|
|
16
|
+
def new
|
|
17
|
+
return head :not_found unless @batch_action.form?
|
|
18
|
+
|
|
19
|
+
@batch = Mensa::Batch.new(table: @table, action: @batch_action, user: current_mensa_user, count: params[:count].to_i)
|
|
20
|
+
render :new, layout: false
|
|
21
|
+
end
|
|
22
|
+
|
|
6
23
|
def create
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
record_ids = Array(params[:record_ids])
|
|
24
|
+
@batch = Mensa::Batch.new(table: @table, action: @batch_action, records: selected_records, params: batch_params, user: current_mensa_user)
|
|
25
|
+
@batch_action.perform(@batch, helpers)
|
|
10
26
|
|
|
11
|
-
|
|
12
|
-
|
|
27
|
+
if @batch.errors.none? && @batch.redirect_url
|
|
28
|
+
redirect_to @batch.redirect_url, **@batch.flash, status: :see_other
|
|
29
|
+
elsif @batch.errors.none?
|
|
30
|
+
redirect_back_or_to helpers.mensa.table_path(@table.name), **@batch.flash, status: :see_other
|
|
31
|
+
elsif dialog_request?
|
|
32
|
+
render :new, layout: false, status: :unprocessable_entity
|
|
33
|
+
else
|
|
34
|
+
redirect_back_or_to helpers.mensa.table_path(@table.name), alert: @batch.errors.full_messages.to_sentence, status: :see_other
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
private
|
|
39
|
+
|
|
40
|
+
def set_batch_action
|
|
41
|
+
@table = Mensa.for_name(params[:table_id], table_config)
|
|
42
|
+
@table.request = request
|
|
43
|
+
@table.original_view_context = helpers
|
|
13
44
|
|
|
14
|
-
batch_action = table.batch_actions.find { |a| a.name == batch_action_name }
|
|
15
|
-
return head :not_found unless batch_action
|
|
45
|
+
@batch_action = @table.batch_actions.find { |a| a.name == params[:batch_action_name]&.to_sym }
|
|
46
|
+
return head :not_found unless @batch_action
|
|
47
|
+
|
|
48
|
+
head :forbidden unless @batch_action.show?(helpers)
|
|
49
|
+
end
|
|
16
50
|
|
|
17
|
-
|
|
18
|
-
|
|
51
|
+
# The table as the user saw it: the view's configuration, with the
|
|
52
|
+
# search and filters sent along on top (like Mensa::TablesController)
|
|
53
|
+
def table_config
|
|
54
|
+
filters_provided = params.key?(:filters)
|
|
55
|
+
config = params.permit(:query, :table_view_id, filters: {}).to_h
|
|
56
|
+
config[:filters] = {} if filters_provided && !config.key?(:filters)
|
|
57
|
+
config[:params] = Mensa::TableParams.from_request(params)
|
|
58
|
+
|
|
59
|
+
view = Mensa::TableView.find_by(table_name: params[:table_id], id: params[:table_view_id]) if params[:table_view_id].present?
|
|
60
|
+
(view&.config&.deep_symbolize_keys || {}).merge(config.deep_symbolize_keys)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# Selected through the table's scope by id, so joins in it can't repeat
|
|
64
|
+
# records and its select or grouping don't end up in the relation
|
|
65
|
+
def selected_records
|
|
66
|
+
model = @table.model
|
|
67
|
+
key = model.primary_key
|
|
68
|
+
source = params[:select_all].present? ? @table.filtered_scope : @table.scope.where(key => Array(params[:record_ids]))
|
|
69
|
+
|
|
70
|
+
model.where(key => source.unscope(:order, :limit, :offset).reselect(model.arel_table[key]))
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# The dialog's fields, which its form submits under batch[...]
|
|
74
|
+
def batch_params
|
|
75
|
+
params.fetch(:batch, ActionController::Parameters.new)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def dialog_request?
|
|
79
|
+
request.headers["Turbo-Frame"] == DIALOG_FRAME
|
|
80
|
+
end
|
|
19
81
|
|
|
20
|
-
|
|
82
|
+
def current_mensa_user
|
|
83
|
+
Current.user if defined?(Current) && Current.respond_to?(:user)
|
|
21
84
|
end
|
|
22
85
|
end
|
|
23
86
|
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Mensa
|
|
4
|
+
module Tables
|
|
5
|
+
class RowActionsController < ::ApplicationController
|
|
6
|
+
def create
|
|
7
|
+
table_name = params[:table_id]
|
|
8
|
+
|
|
9
|
+
table = Mensa.for_name(table_name, params: Mensa::TableParams.from_request(params))
|
|
10
|
+
table.original_view_context = helpers
|
|
11
|
+
|
|
12
|
+
action = table.actions.find { |a| a.name == params[:name]&.to_sym && a.process? }
|
|
13
|
+
return head :not_found unless action
|
|
14
|
+
|
|
15
|
+
# Only records the table shows, and only when it shows the action
|
|
16
|
+
record = table.scope.find_by(table.model.primary_key => params[:record_id])
|
|
17
|
+
return head :not_found unless record && action.show.call(record)
|
|
18
|
+
|
|
19
|
+
action.process.call(record)
|
|
20
|
+
|
|
21
|
+
redirect_back_or_to helpers.mensa.table_path(table_name), status: :see_other
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Mensa
|
|
4
|
+
# What a batch action's process gets besides the records:
|
|
5
|
+
#
|
|
6
|
+
# process do |users, batch|
|
|
7
|
+
# next batch.errors.add(:reason, :blank) if batch.params[:reason].blank?
|
|
8
|
+
#
|
|
9
|
+
# users.update_all(archived_reason: batch.params[:reason])
|
|
10
|
+
# batch.redirect_to users_path, notice: "#{batch.count} users archived"
|
|
11
|
+
# end
|
|
12
|
+
#
|
|
13
|
+
# It's also the object the dialog's form is built on, so the fields show the
|
|
14
|
+
# submitted values and errors again when the dialog is shown with errors.
|
|
15
|
+
class Batch
|
|
16
|
+
include ActiveModel::Model
|
|
17
|
+
|
|
18
|
+
attr_reader :table, :action, :records, :params, :user, :redirect_url, :flash
|
|
19
|
+
|
|
20
|
+
def initialize(table:, action:, records: nil, params: nil, user: nil, count: nil)
|
|
21
|
+
super()
|
|
22
|
+
@table = table
|
|
23
|
+
@action = action
|
|
24
|
+
@records = records
|
|
25
|
+
@params = params || ActionController::Parameters.new
|
|
26
|
+
@user = user
|
|
27
|
+
@count = count
|
|
28
|
+
@flash = {}
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# The number of selected records
|
|
32
|
+
def count
|
|
33
|
+
@count ||= records ? records.count : 0
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Where to go after the action, with an optional notice or alert. Without
|
|
37
|
+
# a url it goes back to the table.
|
|
38
|
+
def redirect_to(url = nil, notice: nil, alert: nil)
|
|
39
|
+
@redirect_url = url
|
|
40
|
+
@flash = {notice: notice, alert: alert}.compact
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# The submitted values, so form fields show them again
|
|
44
|
+
def respond_to_missing?(name, include_private = false)
|
|
45
|
+
params.key?(name.to_s) || super
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def method_missing(name, *args)
|
|
49
|
+
return params[name.to_s] if args.empty? && params.key?(name.to_s)
|
|
50
|
+
|
|
51
|
+
super
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
data/app/tables/mensa/action.rb
CHANGED
|
@@ -8,6 +8,13 @@ module Mensa
|
|
|
8
8
|
# link_attributes "data-turbo-method" => "delete"
|
|
9
9
|
# icon "fa-xmark"
|
|
10
10
|
# end
|
|
11
|
+
#
|
|
12
|
+
# Or run a bit of code with the record, without a controller of your own:
|
|
13
|
+
#
|
|
14
|
+
# action :activate do
|
|
15
|
+
# process { |user| user.activate! }
|
|
16
|
+
# icon "fa-check"
|
|
17
|
+
# end
|
|
11
18
|
class Action
|
|
12
19
|
include ConfigReaders
|
|
13
20
|
|
|
@@ -33,5 +40,10 @@ module Mensa
|
|
|
33
40
|
config_reader :link, call: false
|
|
34
41
|
config_reader :link_attributes
|
|
35
42
|
config_reader :show, call: false
|
|
43
|
+
config_reader :process, call: false
|
|
44
|
+
|
|
45
|
+
def process?
|
|
46
|
+
process.present?
|
|
47
|
+
end
|
|
36
48
|
end
|
|
37
49
|
end
|
data/app/tables/mensa/base.rb
CHANGED
|
@@ -126,14 +126,25 @@ module Mensa
|
|
|
126
126
|
@actions ||= config[:actions].keys.map { |action_name| Mensa::Action.new(action_name, config: config.dig(:actions, action_name), table: self) }
|
|
127
127
|
end
|
|
128
128
|
|
|
129
|
+
# True when the table has batch actions the user may run
|
|
129
130
|
def batch_actions?
|
|
130
|
-
|
|
131
|
+
visible_batch_actions.present?
|
|
131
132
|
end
|
|
132
133
|
|
|
133
134
|
def batch_actions
|
|
134
135
|
return @batch_actions if @batch_actions
|
|
135
136
|
|
|
136
|
-
@batch_actions ||= config[:batches].keys.map { |batch_name| Mensa::BatchAction.new(batch_name, config: config.dig(:batches, batch_name), table: self) }
|
|
137
|
+
@batch_actions ||= (config[:batches] || {}).keys.map { |batch_name| Mensa::BatchAction.new(batch_name, config: config.dig(:batches, batch_name), table: self) }
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
# The batch actions whose show allows them, remembered once there's a view
|
|
141
|
+
# context to check them in
|
|
142
|
+
def visible_batch_actions
|
|
143
|
+
return @visible_batch_actions if @visible_batch_actions
|
|
144
|
+
|
|
145
|
+
view_context = @original_view_context || component&.original_view_context
|
|
146
|
+
visible = batch_actions.select { |batch_action| batch_action.show?(view_context) }
|
|
147
|
+
view_context ? (@visible_batch_actions = visible) : visible
|
|
137
148
|
end
|
|
138
149
|
|
|
139
150
|
# Returns the current path with configuration
|
|
@@ -5,7 +5,10 @@ module Mensa
|
|
|
5
5
|
#
|
|
6
6
|
# batch :archive do
|
|
7
7
|
# title "Archive"
|
|
8
|
-
#
|
|
8
|
+
# form "users/archive_form"
|
|
9
|
+
# process do |users, batch|
|
|
10
|
+
# users.update_all(archived_reason: batch.params[:reason])
|
|
11
|
+
# end
|
|
9
12
|
# end
|
|
10
13
|
class BatchAction
|
|
11
14
|
include ConfigReaders
|
|
@@ -22,6 +25,35 @@ module Mensa
|
|
|
22
25
|
|
|
23
26
|
config_reader :title
|
|
24
27
|
config_reader :description
|
|
28
|
+
config_reader :form
|
|
29
|
+
config_reader :confirm
|
|
30
|
+
config_reader :show, call: false
|
|
25
31
|
config_reader :process, call: false
|
|
32
|
+
|
|
33
|
+
def form?
|
|
34
|
+
form.present?
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Whether +view_context+'s user sees, and may run, the action
|
|
38
|
+
def show?(view_context = nil)
|
|
39
|
+
evaluate(view_context, show, table) ? true : false
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# Runs the action for +batch+. A block that only takes the records gets
|
|
43
|
+
# them as an Array, as it always did; one that also takes the batch gets a
|
|
44
|
+
# relation.
|
|
45
|
+
def perform(batch, view_context = nil)
|
|
46
|
+
args = (process.arity == 1) ? [batch.records.to_a] : [batch.records, batch]
|
|
47
|
+
evaluate(view_context, process, *args)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
private
|
|
51
|
+
|
|
52
|
+
# Runs +block+ in the view context, so route helpers work in it, like they
|
|
53
|
+
# do in links. Lambdas get only as many arguments as they take.
|
|
54
|
+
def evaluate(view_context, block, *args)
|
|
55
|
+
args = args.first(block.arity) if block.lambda? && block.arity >= 0
|
|
56
|
+
view_context ? view_context.instance_exec(*args, &block) : block.call(*args)
|
|
57
|
+
end
|
|
26
58
|
end
|
|
27
59
|
end
|
|
@@ -6,6 +6,12 @@ module Mensa::Config
|
|
|
6
6
|
|
|
7
7
|
option :title, default: -> { name.to_s.humanize }
|
|
8
8
|
option :description, default: ""
|
|
9
|
+
# Partial with the dialog's fields, rendered in Mensa's form before the action runs
|
|
10
|
+
option :form
|
|
11
|
+
# Message to confirm before the action runs, for actions without a form
|
|
12
|
+
option :confirm
|
|
13
|
+
# Whether the action is shown (and allowed), called with the table
|
|
14
|
+
option :show, default: ->(table) { true }
|
|
9
15
|
option :process, default: ->(records) {}
|
|
10
16
|
|
|
11
17
|
delegate :t, to: :I18n
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
<turbo-frame id="<%= Mensa::Tables::BatchActionsController::DIALOG_FRAME %>">
|
|
2
|
+
<%= form_with model: @batch, scope: :batch, url: @table.path_with_params(mensa.table_batch_actions_path(@table.name)), method: :post, class: "mensa-batch-dialog__panel" do |f| %>
|
|
3
|
+
<%= hidden_field_tag :batch_action_name, @batch_action.name, id: nil %>
|
|
4
|
+
|
|
5
|
+
<header class="mensa-batch-dialog__header">
|
|
6
|
+
<h2 class="mensa-batch-dialog__title"><%= @batch_action.title %></h2>
|
|
7
|
+
<button class="mensa-batch-dialog__close" type="button" data-mensa-batch-dialog-close aria-label="<%= t("mensa.batch_actions.dialog.close") %>">
|
|
8
|
+
<i class="<%= Mensa.config.icons[:batch_dialog_close] %>"></i>
|
|
9
|
+
</button>
|
|
10
|
+
</header>
|
|
11
|
+
|
|
12
|
+
<div class="mensa-batch-dialog__body">
|
|
13
|
+
<% if @batch_action.description.present? %>
|
|
14
|
+
<p class="mensa-batch-dialog__description"><%= @batch_action.description %></p>
|
|
15
|
+
<% end %>
|
|
16
|
+
<p class="mensa-batch-dialog__count"><%= t("mensa.batch_actions.dialog.count", count: number_with_delimiter(@batch.count)) %></p>
|
|
17
|
+
|
|
18
|
+
<% if @batch.errors.any? %>
|
|
19
|
+
<ul class="mensa-batch-dialog__errors" role="alert">
|
|
20
|
+
<% @batch.errors.full_messages.each do |message| %>
|
|
21
|
+
<li><%= message %></li>
|
|
22
|
+
<% end %>
|
|
23
|
+
</ul>
|
|
24
|
+
<% end %>
|
|
25
|
+
|
|
26
|
+
<%= render partial: @batch_action.form, locals: {f: f, table: @table, count: @batch.count, batch: @batch} %>
|
|
27
|
+
</div>
|
|
28
|
+
|
|
29
|
+
<footer class="mensa-batch-dialog__actions">
|
|
30
|
+
<button class="mensa-batch-dialog__button mensa-batch-dialog__button--secondary" type="button" data-mensa-batch-dialog-close>
|
|
31
|
+
<%= t("mensa.batch_actions.dialog.cancel") %>
|
|
32
|
+
</button>
|
|
33
|
+
<button class="mensa-batch-dialog__button mensa-batch-dialog__button--primary" type="submit">
|
|
34
|
+
<%= @batch_action.title %>
|
|
35
|
+
</button>
|
|
36
|
+
</footer>
|
|
37
|
+
<% end %>
|
|
38
|
+
</turbo-frame>
|
data/config/locales/en.yml
CHANGED
|
@@ -38,6 +38,16 @@ en:
|
|
|
38
38
|
export_repeat:
|
|
39
39
|
title: Repeat
|
|
40
40
|
description: Export once, or have the export repeat automatically at a daily, weekly, monthly or other interval.
|
|
41
|
+
batch_actions:
|
|
42
|
+
selected: "%{count} selected"
|
|
43
|
+
all_selected: "All %{count} selected"
|
|
44
|
+
select_all: "Select all (%{count})"
|
|
45
|
+
select_page: "Only this page (%{count})"
|
|
46
|
+
confirm_all: "%{action} all %{count} selected rows?"
|
|
47
|
+
dialog:
|
|
48
|
+
count: "Applies to %{count} selected rows"
|
|
49
|
+
cancel: Cancel
|
|
50
|
+
close: Close
|
|
41
51
|
column_customizer:
|
|
42
52
|
heading: Columns
|
|
43
53
|
group_by:
|
data/config/locales/nl.yml
CHANGED
|
@@ -38,6 +38,16 @@ nl:
|
|
|
38
38
|
export_repeat:
|
|
39
39
|
title: Herhalen
|
|
40
40
|
description: Exporteer eenmalig, of laat de export automatisch herhalen per dag, week, maand of een ander interval.
|
|
41
|
+
batch_actions:
|
|
42
|
+
selected: "%{count} geselecteerd"
|
|
43
|
+
all_selected: "Alle %{count} geselecteerd"
|
|
44
|
+
select_all: "Selecteer alle (%{count})"
|
|
45
|
+
select_page: "Alleen deze pagina (%{count})"
|
|
46
|
+
confirm_all: "%{action}: alle %{count} geselecteerde rijen?"
|
|
47
|
+
dialog:
|
|
48
|
+
count: "Geldt voor %{count} geselecteerde rijen"
|
|
49
|
+
cancel: Annuleren
|
|
50
|
+
close: Sluiten
|
|
41
51
|
column_customizer:
|
|
42
52
|
heading: Kolommen
|
|
43
53
|
group_by:
|
data/config/routes.rb
CHANGED
|
@@ -3,7 +3,8 @@ Mensa::Engine.routes.draw do
|
|
|
3
3
|
scope module: :tables do
|
|
4
4
|
resources :filters
|
|
5
5
|
resources :views, only: [:create, :update, :destroy]
|
|
6
|
-
resources :batch_actions, only: [:create]
|
|
6
|
+
resources :batch_actions, only: [:new, :create]
|
|
7
|
+
post "row_actions/:name/:record_id", to: "row_actions#create", as: :row_action
|
|
7
8
|
resources :exports, only: [:index, :create, :destroy] do
|
|
8
9
|
member do
|
|
9
10
|
get :download
|
data/lib/mensa/configuration.rb
CHANGED
|
@@ -81,6 +81,9 @@ module Mensa
|
|
|
81
81
|
exports_list_delete: "fa-solid fa-trash",
|
|
82
82
|
exports_list_download: "fa-solid fa-download",
|
|
83
83
|
exports_list_processing: "fa-solid fa-spinner fa-spin",
|
|
84
|
+
batch_select_all: "fa-solid fa-check-double",
|
|
85
|
+
batch_select_page: "fa-solid fa-check",
|
|
86
|
+
batch_dialog_close: "fa-solid fa-xmark",
|
|
84
87
|
tables_standard_error: "fa-solid fa-circle-exclamation"
|
|
85
88
|
}
|
|
86
89
|
# PRO
|
|
@@ -117,6 +120,9 @@ module Mensa
|
|
|
117
120
|
# exports_list_delete: "fa-light fa-trash",
|
|
118
121
|
# exports_list_download: "fa-light fa-download",
|
|
119
122
|
# exports_list_processing: "fa-light fa-spinner fa-spin",
|
|
123
|
+
# batch_select_all: "fa-light fa-check-double",
|
|
124
|
+
# batch_select_page: "fa-light fa-check",
|
|
125
|
+
# batch_dialog_close: "fa-light fa-xmark",
|
|
120
126
|
# tables_standard_error: "fa-light fa-circle-exclamation"
|
|
121
127
|
# }
|
|
122
128
|
|
data/lib/mensa/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: mensa
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.10.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Tom de Grunt
|
|
@@ -260,6 +260,7 @@ files:
|
|
|
260
260
|
- app/controllers/mensa/tables/batch_actions_controller.rb
|
|
261
261
|
- app/controllers/mensa/tables/exports_controller.rb
|
|
262
262
|
- app/controllers/mensa/tables/filters_controller.rb
|
|
263
|
+
- app/controllers/mensa/tables/row_actions_controller.rb
|
|
263
264
|
- app/controllers/mensa/tables/views_controller.rb
|
|
264
265
|
- app/controllers/mensa/tables_controller.rb
|
|
265
266
|
- app/helpers/mensa/application_helper.rb
|
|
@@ -275,6 +276,7 @@ files:
|
|
|
275
276
|
- app/jobs/mensa/recurring_exports_job.rb
|
|
276
277
|
- app/models/concerns/.keep
|
|
277
278
|
- app/models/mensa/application_record.rb
|
|
279
|
+
- app/models/mensa/batch.rb
|
|
278
280
|
- app/models/mensa/export.rb
|
|
279
281
|
- app/models/mensa/table_view.rb
|
|
280
282
|
- app/tables/mensa/action.rb
|
|
@@ -304,6 +306,7 @@ files:
|
|
|
304
306
|
- app/views/mensa/exports/_badge.html.erb
|
|
305
307
|
- app/views/mensa/exports/_dialog.html.erb
|
|
306
308
|
- app/views/mensa/exports/_list.html.erb
|
|
309
|
+
- app/views/mensa/tables/batch_actions/new.html.erb
|
|
307
310
|
- app/views/mensa/tables/filters/show.turbo_stream.erb
|
|
308
311
|
- app/views/mensa/tables/show.html.erb
|
|
309
312
|
- app/views/mensa/tables/show.turbo_stream.erb
|