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