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 1 commit
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
14 changes: 11 additions & 3 deletions app/views/admin/categories/_category.html.haml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
%tr.human{:id => category.id.split('/').last}
- count = category.ontologies&.size || 0
- is_parent = @parents_list.keys.include?(category.id)
Bilelkihal marked this conversation as resolved.
Show resolved Hide resolved
- parent_error_message = "Can't delete category, used as a parent category for: "
Bilelkihal marked this conversation as resolved.
Show resolved Hide resolved
- @parents_list[category.id].each do |c|
- parent_error_message = "#{parent_error_message} #{c}"
%td
%div{style: 'width: 250px'}
%div.text-truncate{title: category.name}
Expand All @@ -18,8 +22,12 @@
= 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'}}

Loading