From 8f6a823516fd523b13e44aa5e46e1510780d6170 Mon Sep 17 00:00:00 2001 From: bohung Date: Tue, 26 Jul 2022 19:13:36 +0800 Subject: [PATCH] Update sort. --- app/controllers/admin/books_controller.rb | 4 ++-- app/controllers/personal_books_controller.rb | 2 +- app/models/book.rb | 4 +++- app/views/plugin/personal_book/_profile.html.erb | 4 ++-- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/app/controllers/admin/books_controller.rb b/app/controllers/admin/books_controller.rb index 63b1a7f..46ec24d 100644 --- a/app/controllers/admin/books_controller.rb +++ b/app/controllers/admin/books_controller.rb @@ -25,7 +25,7 @@ class Admin::BooksController < OrbitMemberController redirect_to :action=> 'index' end def merge - @books=Book.order_by(:year=>'desc').map{|value| value}.group_by{|v| [v[:book_title],v[:isbn]]} + @books=Book.sort_year_date.map{|value| value}.group_by{|v| [v[:book_title],v[:isbn]]} @books.each do |key,value| if value.length<=1 @books.delete key @@ -48,7 +48,7 @@ class Admin::BooksController < OrbitMemberController end def index - @writing_books = Book.order_by(:year=>'desc').page(params[:page]).per(10) + @writing_books = Book.sort_year_date.page(params[:page]).per(10) respond_to do |format| format.html # index.html.erb format.js { } diff --git a/app/controllers/personal_books_controller.rb b/app/controllers/personal_books_controller.rb index b2b2105..2a06965 100644 --- a/app/controllers/personal_books_controller.rb +++ b/app/controllers/personal_books_controller.rb @@ -3,7 +3,7 @@ class PersonalBooksController < ApplicationController def index params = OrbitHelper.params page_data_count = OrbitHelper.page_data_count - books = Book.where(is_hidden: false).sort_for_frontend.page(OrbitHelper.params[:page_no]).per(page_data_count) + books = Book.sort_for_frontend.page(OrbitHelper.params[:page_no]).per(page_data_count) page = Page.where(page_id: params[:page_id]).first rescue nil if page.custom_string_field == 'table' diff --git a/app/models/book.rb b/app/models/book.rb index 77175f9..319a619 100644 --- a/app/models/book.rb +++ b/app/models/book.rb @@ -39,7 +39,9 @@ class Book before_validation :add_http - scope :sort_for_frontend, ->{ where(:is_hidden=>false).order_by(:year => "desc", :publish_date => "desc", :publication_date => "desc") } + index({year: -1, :publish_date => -1, :publication_date => -1, _id: -1}, { unique: false, background: false }) + scope :sort_year_date, ->{ order_by(:year => "desc", :publish_date => "desc", :publication_date => "desc", :id => "desc")) } + scope :sort_for_frontend, ->{ where(:is_hidden=>false).order_by(:year => "desc", :publish_date => "desc", :publication_date => "desc", :id => "desc") } def book_paper_type self.book_type.title rescue "" end diff --git a/app/views/plugin/personal_book/_profile.html.erb b/app/views/plugin/personal_book/_profile.html.erb index 2ac8b3a..1ce7b34 100644 --- a/app/views/plugin/personal_book/_profile.html.erb +++ b/app/views/plugin/personal_book/_profile.html.erb @@ -7,9 +7,9 @@ <% if has_access? - @books = Book.where(member_profile_id: @member.id).desc(:year).page(params[:page]).per(10) + @books = Book.where(member_profile_id: @member.id).sort_year_date.page(params[:page]).per(10) else - @books = Book.where(is_hidden: false, member_profile_id: @member.id).desc(:year).page(params[:page]).per(10) + @books = Book.where(is_hidden: false, member_profile_id: @member.id).sort_year_date.page(params[:page]).per(10) end %>