From 8a8de1f5b6d7f4dab6dca1e4869d862fc243eb55 Mon Sep 17 00:00:00 2001 From: bohung Date: Tue, 26 Jul 2022 19:07:22 +0800 Subject: [PATCH] Update sort. --- app/controllers/admin/honors_controller.rb | 2 +- app/controllers/personal_honors_controller.rb | 4 ++-- app/models/honor.rb | 8 +++++--- app/views/plugin/personal_honor/_profile.html.erb | 4 ++-- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/app/controllers/admin/honors_controller.rb b/app/controllers/admin/honors_controller.rb index 54322c1..031ab31 100644 --- a/app/controllers/admin/honors_controller.rb +++ b/app/controllers/admin/honors_controller.rb @@ -10,7 +10,7 @@ class Admin::HonorsController < OrbitMemberController before_action :allow_admin_only, :only => [:index, :setting] def index - @honors = Honor.order_by(:year=>'desc').page(params[:page]).per(10) + @honors = Honor.sort_year_date.page(params[:page]).per(10) end def new diff --git a/app/controllers/personal_honors_controller.rb b/app/controllers/personal_honors_controller.rb index 23cbbe9..d50abd5 100644 --- a/app/controllers/personal_honors_controller.rb +++ b/app/controllers/personal_honors_controller.rb @@ -5,9 +5,9 @@ class PersonalHonorsController < ApplicationController honors = nil if filter_value.nil? || filter_value == t("personal_honor.honor_types_all") || filter_value.empty? - honors = Honor.where(:award_name.ne => nil).or(:award_name.ne => "").sort_for_frontend.page(OrbitHelper.params[:page_no]).per(OrbitHelper.page_data_count) + honors = Honor.where(:award_name.nin => [nil, ""]).sort_for_frontend.page(OrbitHelper.params[:page_no]).per(OrbitHelper.page_data_count) else - honor_type_id = HonorType.or({ 'title.en' => filter_value }, { 'title.zh_tw' => filter_value }).first.id + honor_type_id = HonorType.any_of({ 'title.en' => filter_value }, { 'title.zh_tw' => filter_value }).first.id honors = Honor.where(honor_type_id: honor_type_id).sort_for_frontend.page(OrbitHelper.params[:page_no]).per(OrbitHelper.page_data_count) end diff --git a/app/models/honor.rb b/app/models/honor.rb index 4cf93c6..dcd30e2 100644 --- a/app/models/honor.rb +++ b/app/models/honor.rb @@ -24,7 +24,9 @@ class Honor before_validation :add_http - scope :sort_for_frontend, ->{ where(:is_hidden=>false).order_by(:year => "desc",:award_date => "desc") } + index({year: -1, :award_date => -1, _id: -1}, { unique: false, background: false }) + scope :sort_year_date, ->{ order_by(:year => "desc",:award_date => "desc", :id => "desc") } + scope :sort_for_frontend, ->{ where(:is_hidden=>false).order_by(:year => "desc",:award_date => "desc", :id => "desc") } def slug_title self.award_name+' '+self.awarding_unit rescue "" @@ -71,7 +73,7 @@ class Honor fields_to_show = fields_to_show - fields_to_remove - plugin_datas = datas.sort_for_frontend.collect.with_index do |p,index| + plugin_datas = datas.sort_for_frontend.collect.with_index do |p,idx| pd_data = [] fields_to_show.collect do |t| if t == "award_name" @@ -89,7 +91,7 @@ class Honor { "pd_datas" => pd_data, "type-sort" => (p.honor_type.sort_position.to_i rescue 1000), - "sort-index" => index + "sort-index" => idx } end diff --git a/app/views/plugin/personal_honor/_profile.html.erb b/app/views/plugin/personal_honor/_profile.html.erb index b8e2c7e..a7b4bf1 100644 --- a/app/views/plugin/personal_honor/_profile.html.erb +++ b/app/views/plugin/personal_honor/_profile.html.erb @@ -7,9 +7,9 @@ <% if has_access? - @honors = Honor.where(member_profile_id: @member.id).desc(:year).page(params[:page]).per(10) + @honors = Honor.where(member_profile_id: @member.id).sort_year_date.page(params[:page]).per(10) else - @honors = Honor.where(is_hidden: false, member_profile_id: @member.id).desc(:year).page(params[:page]).per(10) + @honors = Honor.where(is_hidden: false, member_profile_id: @member.id).sort_year_date.page(params[:page]).per(10) end %>