Skip to content

Commit afa8ee1

Browse files
authored
Merge pull request #498 from mario-amazing/master
Add confirm_message to toggle-bool-switch
2 parents 34afc36 + 38e108f commit afa8ee1

4 files changed

Lines changed: 35 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).
44

55
### Unreleased
66

7+
#### Added
8+
9+
* Add confirm_message to toggle-bool-switch
10+
711
### 2.0.0.beta-3
812

913
#### Added

app/javascript/activeadmin_addons/addons/toggle_bool.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@ var initializer = function() {
22
$('.toggle-bool-switch').click(function(e) {
33
var boolSwitch = $(e.target);
44

5+
var value = boolSwitch.data('value');
6+
var confirmMessage = boolSwitch.data('confirm_message');
7+
var confirmTrigger = boolSwitch.data('confirm_message_trigger') || 'both';
8+
9+
if (confirmMessage) {
10+
var shouldConfirm =
11+
(confirmTrigger === 'both') ||
12+
(confirmTrigger === 'on' && !value) ||
13+
(confirmTrigger === 'off' && value);
14+
15+
if (shouldConfirm && !confirm(confirmMessage)) {
16+
return false;
17+
}
18+
}
519
var objectId = boolSwitch.data('object_id');
620
var model = boolSwitch.data('model');
721
var field = boolSwitch.data('field');

docs/toggle_bool.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ The value update is done through the default update route, so you must check you
3939
`toggle_bool_column :paid, if: proc { |item| item.price.present? }`
4040

4141
`toggle_bool_column :paid, unless: proc { |item| item.is_free? }`
42+
43+
### Confirmation message
44+
45+
Optionally, you can provide a confirmation message to the user before updating the value.
46+
47+
This is disabled by default, but can be enabled by adding the `confirm_message` option. Optionally, you can add the `confirm_message_trigger` option to choose when to toggle the confirm message, possible values are:
48+
- `both` (default),
49+
- `on`
50+
- `off`
51+
52+
`toggle_bool_column :paid, confirm_message: 'Are you sure you want to toggle this switch?', confirm_message_trigger: 'on'`
53+
54+
If the user cancels the confirmation, the update action will be aborted.
4255

4356
### Success message
4457

@@ -48,4 +61,4 @@ The value update is done through the default update route, so you must check you
4861

4962
`toggle_bool_column :paid, success_message: 'Item Updated Successfully!'`
5063

51-
If the update fails for any reason, an "Error: Update Failed" alert will be prompted (this can't be disabled nor customized)
64+
If the update fails for any reason, an "Error: Update Failed" alert will be prompted (this can't be disabled nor customized)

lib/activeadmin_addons/addons/toggle_bool_builder.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ def toggle
2424
'data-field' => attribute,
2525
'data-value' => data,
2626
'data-url' => resource_url,
27-
'data-success_message' => options[:success_message]
27+
'data-success_message' => options[:success_message],
28+
'data-confirm_message' => options[:confirm_message],
29+
'data-confirm_message_trigger' => options[:confirm_message_trigger]
2830
)
2931
end
3032

0 commit comments

Comments
 (0)