improve admin categories_order speed

This commit is contained in:
chiu 2024-04-22 01:58:05 +00:00
parent 8f738acce6
commit 303f431aa7
1 changed files with 10 additions and 9 deletions

View File

@ -69,16 +69,17 @@ class Admin::ArchiveFilesController < OrbitAdminController
end end
def categories_order def categories_order
categories = @module_app.categories categories = @module_app.categories
categories.each do |category| cat_id_cat_maps = categories.collect{|cat| [cat.id.to_s, cat]}.to_h
archive_cat = ArchiveCategory.all.select{ |value| value.category_id.to_s == category.id.to_s} cat_id_sort_number_maps = ArchiveCategory.all.order_by(sort_number: 'desc').pluck(:category_id, :sort_number).to_h
if archive_cat.length == 0 no_archive_cat_ids = cat_id_cat_maps.keys - cat_id_sort_number_maps.keys
ArchiveCategory.create(category_id: category.id.to_s) no_archive_cat_ids.each do |cat_id|
end ArchiveCategory.create(category_id: cat_id)
cat_id_sort_number_maps[cat_id] = nil
end end
cats_with_nil = ArchiveCategory.all.in(sort_number: nil) @cats = cat_id_sort_number_maps.collect do |cat_id_sort_number_entry|
cats_no_nil = ArchiveCategory.all.not_in(sort_number: nil).order_by(sort_number: 'desc') cat_id = cat_id_sort_number_entry[0]
@cats = cats_no_nil.concat(cats_with_nil).collect do |cat| sort_number = cat_id_sort_number_entry[1]
[categories.where(:_id => cat.category_id).first,cat.sort_number.to_s] [cat_id_cat_maps[cat_id], sort_number.to_s]
end end
render :partial => "categories_order" if request.xhr? render :partial => "categories_order" if request.xhr?
end end