diff --git a/app/controllers/archives_controller.rb b/app/controllers/archives_controller.rb index e3ae340..ce3105d 100644 --- a/app/controllers/archives_controller.rb +++ b/app/controllers/archives_controller.rb @@ -1,5 +1,11 @@ class ArchivesController < ApplicationController #avoid the categories to be not in the ArchiveCategory + before_action :set_archive_sort_order, only: [:index, :widget] + def set_archive_sort_order + if ArchiveSortOrder.count == 0 + ArchiveSortOrder.new('sort_order' => false).save + end + end def serve_cmap serve_path=File.expand_path("../../assets/javascripts/archive/pdf/bcmaps/#{params[:file_name]}.#{params[:extension]}",__FILE__) if Dir.glob(serve_path).length != 0 @@ -9,9 +15,6 @@ class ArchivesController < ApplicationController end end def index - if ArchiveSortOrder.count == 0 - ArchiveSortOrder.new('sort_order' => false).save - end params = OrbitHelper.params if !params['title'].nil? files_by_category = ArchiveFile.where(is_hidden: false).order_by(:created_at => "desc").group_by(&:category) @@ -118,15 +121,6 @@ class ArchivesController < ApplicationController "url_to_edit" => url_to_edit } end - if ArchiveSortOrder.first['sort_order'] #Order with ascending - cats = cats.collect do |cat| - Hash[cat.map{|k,v| [k,k=='archives' ? (v.sort_by{|tp| [(tp['sort_number'].nil? ? Float::INFINITY : tp['sort_number'].to_i),-tp['created_at']]}) : v] }] - end - else - cats = cats.collect do |cat| - Hash[cat.map{|k,v| [k,k=='archives' ? (v.sort_by{|tp| [(tp['sort_number'].nil? ? Float::INFINITY : -tp['sort_number'].to_i),-tp['created_at']]}) : v] }] - end - end if params[:data_count].to_i <=0 page_data_count = 0 else @@ -271,9 +265,6 @@ class ArchivesController < ApplicationController end def widget - if ArchiveSortOrder.count == 0 - ArchiveSortOrder.new('sort_order' => false).save - end page_data_count = OrbitHelper.widget_data_count categories = OrbitHelper.widget_categories #data are categories' ids or 'all' @categories = [] @@ -296,7 +287,7 @@ class ArchivesController < ApplicationController end end cats = @categories.collect do |cat| - if ArchiveSortOrder.first['sort_order'] #Order with ascending + if ArchiveSortOrder.first.sort_order #Order with ascending archives_all = ArchiveFile.can_display.where(:category_id => cat["id"]).filter_by_tags(OrbitHelper.widget_tags) temp_with_nil = archives_all.in(sort_number: nil).order_by(created_at: 'desc') temp_no_nil = archives_all.not_in(sort_number: nil).order_by(sort_number: 'asc',created_at: 'desc') @@ -342,7 +333,8 @@ class ArchivesController < ApplicationController private def get_sorted_cat_with_filter(categories,cat_type) all_categories_with_nil = ArchiveCategory.all.in(sort_number: nil).uniq{|c| c.category_id }.to_a rescue [] - all_categories_no_nil = ArchiveCategory.all.not_in(sort_number: nil).order_by(sort_number: 'desc').uniq{|c| c.category_id }.to_a rescue [] + sort_method = ArchiveSortOrder.first.sort_order ? 'asc' : 'desc' + all_categories_no_nil = ArchiveCategory.all.not_in(sort_number: nil).order_by(sort_number: sort_method).uniq{|c| c.category_id }.to_a rescue [] all_categories_id = all_categories_no_nil.concat(all_categories_with_nil).collect do |cat| cat.category_id.to_s end