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

Fix: Prevent delete category if still used as parent category #876

Merged
merged 4 commits into from
Dec 10, 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
9 changes: 9 additions & 0 deletions app/controllers/admin/categories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ class Admin::CategoriesController < ApplicationController

def index
@categories = _categories

@parents_list = Hash.new { |hash, key| hash[key] = [] }
@categories.each do |category|
category.parentCategory.each do |parent|
@parents_list[parent] << category.acronym
end
end


end

def new
Expand Down
9 changes: 9 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -451,4 +451,13 @@ def categories_select(id: nil, name: nil, selected: 'None')
render Input::SelectComponent.new(id: id, name: name, value: categories_for_select, selected: selected, multiple: true)
end

def category_is_parent?(parents_list, category)
is_parent = parents_list.keys.include?(category.id)
parent_error_message = t('admin.categories.category_used_parent')
parents_list[category.id].each do |c|
parent_error_message = "#{parent_error_message} #{c}"
end
[is_parent,parent_error_message]
end

end
12 changes: 8 additions & 4 deletions app/views/admin/categories/_category.html.haml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
%tr.human{:id => category.id.split('/').last}
- count = category.ontologies&.size || 0
- count = category.ontologies&.size || 0
- is_parent, parent_error_message = category_is_parent?(@parents_list, category)
%td
%div{style: 'width: 250px'}
%div.text-truncate{title: category.name}
Expand All @@ -18,8 +19,11 @@
= link_to_modal(nil, edit_admin_category_path(category.id.split('/').last), data: {show_modal_title_value: category.name}) do
= t('admin.categories.edit_button')
%span
- if count.zero?
= button_to t('admin.categories.delete'), CGI.unescape(admin_category_path(category.id.split('/').last)), method: :delete, class: 'btn btn-link', form: {data: { turbo: true, turbo_confirm: t('admin.categories.turbo_confirm'), turbo_frame: '_top'}}
- else
- if !count.zero?
%span{data: { controller: 'tooltip' }, title: t('admin.categories.info_error_delete')}
= link_to t('admin.categories.delete'), "", class: 'btn btn-link disabled'
- elsif is_parent
%span{data: { controller: 'tooltip' }, title: parent_error_message}
= link_to t('admin.categories.delete'), "", class: 'btn btn-link disabled'
- else
= button_to t('admin.categories.delete'), CGI.unescape(admin_category_path(category.id.split('/').last)), method: :delete, class: 'btn btn-link', form: {data: { turbo: true, turbo_confirm: t('admin.categories.turbo_confirm'), turbo_frame: '_top'}}
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ en:
problem_of_updating: Problem updating the category - %{message}
category_deleted_successfully: Category successfully deleted in %{time}s
problem_of_deleting: Problem deleting the category - %{message}
category_used_parent: "Can't delete category, used as a parent category for:"
edit_button: "Edit"
delete: Delete
info_error_delete: Can't delete this category because still used
Expand Down
1 change: 1 addition & 0 deletions config/locales/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ fr:
problem_of_updating: Problème lors de la mise à jour de la catégorie - %{message}
category_deleted_successfully: Catégorie supprimée avec succès en %{time}s
problem_of_deleting: Problème lors de la suppression de la catégorie - %{message}
category_used_parent: "Impossible de supprimer la catégorie, utilisée comme catégorie parente pour:"
edit_button: Éditer
delete: Supprimer
info_error_delete: Impossible de supprimer cette catégorie car elle est encore utilisée
Expand Down
Loading