Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add confirm_message to toggle-bool-switch #498

Merged
merged 6 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Unreleased

#### Added

* Add confirm_message to toggle-bool-switch

### 2.0.0.beta-3

#### Added
Expand Down
14 changes: 14 additions & 0 deletions app/javascript/activeadmin_addons/addons/toggle_bool.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@ var initializer = function() {
$('.toggle-bool-switch').click(function(e) {
var boolSwitch = $(e.target);

var value = boolSwitch.data('value');
var confirmMessage = boolSwitch.data('confirm_message');
var confirmTrigger = boolSwitch.data('confirm_message_trigger') || 'both';

if (confirmMessage) {
var shouldConfirm =
(confirmTrigger === 'both') ||
(confirmTrigger === 'on' && !value) ||
(confirmTrigger === 'off' && value);

if (shouldConfirm && !confirm(confirmMessage)) {
return false;
}
}
var objectId = boolSwitch.data('object_id');
var model = boolSwitch.data('model');
var field = boolSwitch.data('field');
Expand Down
15 changes: 14 additions & 1 deletion docs/toggle_bool.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ The value update is done through the default update route, so you must check you
`toggle_bool_column :paid, if: proc { |item| item.price.present? }`

`toggle_bool_column :paid, unless: proc { |item| item.is_free? }`

### Confirmation message

Optionally, you can provide a confirmation message to the user before updating the value.

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:
- `both` (default),
- `on`
- `off`

`toggle_bool_column :paid, confirm_message: 'Are you sure you want to toggle this switch?', confirm_message_trigger: 'on'`

If the user cancels the confirmation, the update action will be aborted.

### Success message

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

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

If the update fails for any reason, an "Error: Update Failed" alert will be prompted (this can't be disabled nor customized)
If the update fails for any reason, an "Error: Update Failed" alert will be prompted (this can't be disabled nor customized)
4 changes: 3 additions & 1 deletion lib/activeadmin_addons/addons/toggle_bool_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ def toggle
'data-field' => attribute,
'data-value' => data,
'data-url' => resource_url,
'data-success_message' => options[:success_message]
'data-success_message' => options[:success_message],
'data-confirm_message' => options[:confirm_message],
'data-confirm_message_trigger' => options[:confirm_message_trigger]
)
end

Expand Down