diff --git a/app/controllers/archives_controller.rb b/app/controllers/archives_controller.rb index 574c467..7fa56b1 100644 --- a/app/controllers/archives_controller.rb +++ b/app/controllers/archives_controller.rb @@ -1,13 +1,7 @@ class ArchivesController < ApplicationController def index - #the first 7 lines are write to avoid the categories to be not in the ArchiveCategory - 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 + #avoid the categories to be not in the ArchiveCategory + check_cat_sort_data() if ArchiveSortOrder.count == 0 ArchiveSortOrder.new('sort_order' => false).save end @@ -15,11 +9,7 @@ class ArchivesController < ApplicationController if !params['title'].nil? files_by_cateogry = ArchiveFile.where(is_hidden: false).group_by(&:category) categories = files_by_cateogry.keys - 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_id = all_categories_no_nil.concat(all_categories_with_nil).collect do |cat| - cat.category_id.to_s - end + all_categories_id = get_all_categories_id_sorted categories_sort = categories.select{|category| all_categories_id.include? category.id.to_s} cats_last = [] 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) each_data_count = [] categories = files_by_cateogry.keys - 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_id = all_categories_no_nil.concat(all_categories_with_nil).collect do |cat| - cat.category_id.to_s - end + all_categories_id = get_all_categories_id_sorted categories_sort = categories.select{|category| all_categories_id.include? category.id.to_s} cats = categories_sort.collect do |category| 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 def widget + check_cat_sort_data() if ArchiveSortOrder.count == 0 ArchiveSortOrder.new('sort_order' => false).save end page_data_count = OrbitHelper.widget_data_count - categories = OrbitHelper.widget_categories - puts categories.inspect + categories = OrbitHelper.widget_categories #data are categories' ids or 'all' @categories = [] + all_categories_id = get_all_categories_id_sorted if categories.first == "all" - module_app = OrbitHelper.widget_module_app - @categories = module_app.categories.collect do |cat| + categories = OrbitHelper.widget_module_app.categories + categories_sort = categories.select{|category| all_categories_id.include? category.id.to_s} + @categories = categories_sort.collect do |cat| { "title" => cat.title, "id" => cat.id.to_s } end else - categories.each do |cat| + categories_sort = categories.select{|category| all_categories_id.include? category} + categories_sort.each do |cat| c = Category.find(cat) @categories << {"title" => c.title, "id" => c.id.to_s} end @@ -336,4 +325,21 @@ F cats = files_by_cateogry.keys.collect do |cat| def self.get_total_pages @@total_pages 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 \ No newline at end of file