From ee55f63fa8a843cd5d31adb1647d936f2b1722a1 Mon Sep 17 00:00:00 2001 From: bohung Date: Tue, 26 Jul 2022 19:09:38 +0800 Subject: [PATCH] Update sort. --- app/controllers/admin/diplomas_controller.rb | 2 +- app/controllers/personal_diplomas_controller.rb | 2 +- app/models/diploma.rb | 4 +++- app/views/plugin/personal_diploma/_profile.html.erb | 4 ++-- 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/app/controllers/admin/diplomas_controller.rb b/app/controllers/admin/diplomas_controller.rb index 53466f5..102061c 100644 --- a/app/controllers/admin/diplomas_controller.rb +++ b/app/controllers/admin/diplomas_controller.rb @@ -10,7 +10,7 @@ class Admin::DiplomasController < OrbitMemberController before_action :allow_admin_only, :only => [:index, :setting] def index - @diplomas = Diploma.order_by(:end_date=>'desc',:start_date=>'desc').page(params[:page]).per(10) + @diplomas = Diploma.sort_date.page(params[:page]).per(10) end def new diff --git a/app/controllers/personal_diplomas_controller.rb b/app/controllers/personal_diplomas_controller.rb index 7302953..28c6213 100644 --- a/app/controllers/personal_diplomas_controller.rb +++ b/app/controllers/personal_diplomas_controller.rb @@ -1,6 +1,6 @@ class PersonalDiplomasController < ApplicationController def index - diplomas = Diploma.where(:is_hidden=>false).order_by(:end_date=>'desc', :start_date=>'desc').page(OrbitHelper.params[:page_no]).per(OrbitHelper.page_data_count) + diplomas = Diploma.sort_for_frontend.page(OrbitHelper.params[:page_no]).per(OrbitHelper.page_data_count) fields_to_show = Page.where(:page_id => params[:page_id]).first.custom_array_field rescue [] if fields_to_show.blank? fields_to_show = [ diff --git a/app/models/diploma.rb b/app/models/diploma.rb index f7d132e..ce28891 100644 --- a/app/models/diploma.rb +++ b/app/models/diploma.rb @@ -25,7 +25,9 @@ class Diploma before_validation :add_http - scope :sort_for_frontend, ->{ where(:is_hidden=>false).order_by(:end_date => "desc", :start_date => "desc") } + index({end_date: -1, start_date: -1, _id: -1}, { unique: false, background: false }) + scope :sort_date, ->{ order_by(:end_date => "desc", :start_date => "desc", :id => "desc") } + scope :sort_for_frontend, ->{ where(:is_hidden=>false).order_by(:end_date => "desc", :start_date => "desc", :id => "desc") } def duration if !self.start_date.nil? or !self.end_date.nil? diff --git a/app/views/plugin/personal_diploma/_profile.html.erb b/app/views/plugin/personal_diploma/_profile.html.erb index 9f92fee..6e18e3a 100644 --- a/app/views/plugin/personal_diploma/_profile.html.erb +++ b/app/views/plugin/personal_diploma/_profile.html.erb @@ -7,9 +7,9 @@ <% if has_access? - @diplomas = Diploma.where(member_profile_id: @member.id).desc(:year).page(params[:page]).per(10) + @diplomas = Diploma.where(member_profile_id: @member.id).sort_date.page(params[:page]).per(10) else - @diplomas = Diploma.where(is_hidden: false, member_profile_id: @member.id).desc(:year).page(params[:page]).per(10) + @diplomas = Diploma.where(is_hidden: false, member_profile_id: @member.id).sort_date.page(params[:page]).per(10) end %>