From 556858858f9b06341a62d3b63cd24f0b0b23a2b1 Mon Sep 17 00:00:00 2001 From: bohung Date: Tue, 26 Jul 2022 19:14:47 +0800 Subject: [PATCH] Update sort. --- app/controllers/admin/writings_controller.rb | 4 ++-- app/controllers/personal_writings_controller.rb | 2 +- app/models/writing.rb | 5 +++-- app/views/plugin/personal_writing/_profile.html.erb | 4 ++-- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/app/controllers/admin/writings_controller.rb b/app/controllers/admin/writings_controller.rb index 361a528..72ce911 100644 --- a/app/controllers/admin/writings_controller.rb +++ b/app/controllers/admin/writings_controller.rb @@ -24,9 +24,9 @@ class Admin::WritingsController < OrbitMemberController end if params[:clear] != 'true' if(params[:new_filter] != nil && params[:new_filter][:type] == 'category') - @writings = Writing.where(:category_id.in=> @filter['category']).order_by(:year=>'desc',:month=>'desc',:id=>'desc').page(params[:page]).per(10) + @writings = Writing.where(:category_id.in=> @filter['category']).sort_year_month.page(params[:page]).per(10) else - @writings = Writing.order_by(:year=>'desc',:month=>'desc',:id=>'desc').page(params[:page]).per(10) + @writings = Writing.sort_year_month.page(params[:page]).per(10) end else redirect_to admin_writings_url diff --git a/app/controllers/personal_writings_controller.rb b/app/controllers/personal_writings_controller.rb index c6aeeb1..bb76b8d 100644 --- a/app/controllers/personal_writings_controller.rb +++ b/app/controllers/personal_writings_controller.rb @@ -1,6 +1,6 @@ class PersonalWritingsController < ApplicationController def index - writings = Writing.where(:is_hidden=>false).order_by(:year=>'desc').page(OrbitHelper.params[:page_no]).per(OrbitHelper.page_data_count) + writings = Writing.sort_for_frontend.page(OrbitHelper.params[:page_no]).per(OrbitHelper.page_data_count) writing_list = writings.collect do |writing| { "year" => writing.year, diff --git a/app/models/writing.rb b/app/models/writing.rb index efba74b..5124f40 100644 --- a/app/models/writing.rb +++ b/app/models/writing.rb @@ -20,8 +20,9 @@ class Writing accepts_nested_attributes_for :writing_files, :allow_destroy => true before_validation :add_http - - scope :sort_for_frontend, ->{ where(:is_hidden=>false).order_by(:year => "desc") } + index({year: -1, month: -1, _id: -1}, { unique: false, background: false }) + scope :sort_year_month, ->{ order_by(:year=>'desc',:month=>'desc',:id=>'desc') } + scope :sort_for_frontend, ->{ where(:is_hidden=>false).order_by(:year=>"desc",:month=>'desc',:id=>"desc") } def get_plugin_data(fields_to_show) plugin_datas = [] diff --git a/app/views/plugin/personal_writing/_profile.html.erb b/app/views/plugin/personal_writing/_profile.html.erb index 1cf229a..121d97d 100644 --- a/app/views/plugin/personal_writing/_profile.html.erb +++ b/app/views/plugin/personal_writing/_profile.html.erb @@ -7,9 +7,9 @@ <% if has_access? - @writings = Writing.where(member_profile_id: @member.id).desc(:year).page(params[:page]).per(10) + @writings = Writing.where(member_profile_id: @member.id).sort_year_month.page(params[:page]).per(10) else - @writings = Writing.where(is_hidden: false, member_profile_id: @member.id).desc(:year).page(params[:page]).per(10) + @writings = Writing.where(is_hidden: false, member_profile_id: @member.id).sort_year_month.page(params[:page]).per(10) end %>