Update archives_controller.rb

fix widget not sorted
This commit is contained in:
chiu 2019-09-20 13:34:51 +08:00
parent 40be5b1c55
commit b37cd68e24
1 changed files with 29 additions and 23 deletions

View File

@ -1,13 +1,7 @@
class ArchivesController < ApplicationController class ArchivesController < ApplicationController
def index def index
#the first 7 lines are write to avoid the categories to be not in the ArchiveCategory #avoid the categories to be not in the ArchiveCategory
categories_temp = ModuleApp.where(:key => "archive").first.categories check_cat_sort_data()
categories_temp.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
end
if ArchiveSortOrder.count == 0 if ArchiveSortOrder.count == 0
ArchiveSortOrder.new('sort_order' => false).save ArchiveSortOrder.new('sort_order' => false).save
end end
@ -15,11 +9,7 @@ class ArchivesController < ApplicationController
if !params['title'].nil? if !params['title'].nil?
files_by_cateogry = ArchiveFile.where(is_hidden: false).group_by(&:category) files_by_cateogry = ArchiveFile.where(is_hidden: false).group_by(&:category)
categories = files_by_cateogry.keys categories = files_by_cateogry.keys
all_categories_with_nil = ArchiveCategory.all.in(sort_number: nil) all_categories_id = get_all_categories_id_sorted
all_categories_no_nil = ArchiveCategory.all.not_in(sort_number: nil).order_by(sort_number: 'desc')
all_categories_id = all_categories_no_nil.concat(all_categories_with_nil).collect do |cat|
cat.category_id.to_s
end
categories_sort = categories.select{|category| all_categories_id.include? category.id.to_s} categories_sort = categories.select{|category| all_categories_id.include? category.id.to_s}
cats_last = [] cats_last = []
categories_sort.each do |category| categories_sort.each do |category|
@ -80,11 +70,7 @@ class ArchivesController < ApplicationController
files_by_cateogry = ArchiveFile.where(is_hidden: false).filter_by_categories.filter_by_tags.group_by(&:category) files_by_cateogry = ArchiveFile.where(is_hidden: false).filter_by_categories.filter_by_tags.group_by(&:category)
each_data_count = [] each_data_count = []
categories = files_by_cateogry.keys categories = files_by_cateogry.keys
all_categories_with_nil = ArchiveCategory.all.in(sort_number: nil) all_categories_id = get_all_categories_id_sorted
all_categories_no_nil = ArchiveCategory.all.not_in(sort_number: nil).order_by(sort_number: 'desc')
all_categories_id = all_categories_no_nil.concat(all_categories_with_nil).collect do |cat|
cat.category_id.to_s
end
categories_sort = categories.select{|category| all_categories_id.include? category.id.to_s} categories_sort = categories.select{|category| all_categories_id.include? category.id.to_s}
cats = categories_sort.collect do |category| cats = categories_sort.collect do |category|
url_to_edit = OrbitHelper.user_has_cateogry?(category) ? "/admin/archive_files?filters[category][]=#{category.id.to_s}" : "" url_to_edit = OrbitHelper.user_has_cateogry?(category) ? "/admin/archive_files?filters[category][]=#{category.id.to_s}" : ""
@ -267,23 +253,26 @@ F cats = files_by_cateogry.keys.collect do |cat|
end end
def widget def widget
check_cat_sort_data()
if ArchiveSortOrder.count == 0 if ArchiveSortOrder.count == 0
ArchiveSortOrder.new('sort_order' => false).save ArchiveSortOrder.new('sort_order' => false).save
end end
page_data_count = OrbitHelper.widget_data_count page_data_count = OrbitHelper.widget_data_count
categories = OrbitHelper.widget_categories categories = OrbitHelper.widget_categories #data are categories' ids or 'all'
puts categories.inspect
@categories = [] @categories = []
all_categories_id = get_all_categories_id_sorted
if categories.first == "all" if categories.first == "all"
module_app = OrbitHelper.widget_module_app categories = OrbitHelper.widget_module_app.categories
@categories = module_app.categories.collect do |cat| categories_sort = categories.select{|category| all_categories_id.include? category.id.to_s}
@categories = categories_sort.collect do |cat|
{ {
"title" => cat.title, "title" => cat.title,
"id" => cat.id.to_s "id" => cat.id.to_s
} }
end end
else else
categories.each do |cat| categories_sort = categories.select{|category| all_categories_id.include? category}
categories_sort.each do |cat|
c = Category.find(cat) c = Category.find(cat)
@categories << {"title" => c.title, "id" => c.id.to_s} @categories << {"title" => c.title, "id" => c.id.to_s}
end end
@ -336,4 +325,21 @@ F cats = files_by_cateogry.keys.collect do |cat|
def self.get_total_pages def self.get_total_pages
@@total_pages @@total_pages
end end
private
def get_all_categories_id_sorted
all_categories_with_nil = ArchiveCategory.all.in(sort_number: nil)
all_categories_no_nil = ArchiveCategory.all.not_in(sort_number: nil).order_by(sort_number: 'desc')
all_categories_no_nil.concat(all_categories_with_nil).collect do |cat|
cat.category_id.to_s
end
end
def check_cat_sort_data
categories_temp = ModuleApp.where(:key => "archive").first.categories
categories_temp.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
end
end
end end