Order results of RecentView.list (fixes #2194)

This commit is contained in:
Ryan Lue 2018-09-11 12:51:40 +08:00 committed by Ryan Lue
parent 6bc72e6b46
commit 768788da7e
2 changed files with 5 additions and 2 deletions

View file

@ -42,15 +42,17 @@ class RecentView < ApplicationModel
def self.list(user, limit = 10, object_name = nil)
recent_views = if !object_name
RecentView.select('o_id, recent_view_object_id, MAX(created_at) as created_at, MAX(id) as id, created_by_id')
RecentView.select('o_id, recent_view_object_id, created_by_id, MAX(created_at) as created_at, MAX(id) as id')
.group(:o_id, :recent_view_object_id, :created_by_id)
.where(created_by_id: user.id)
.order('MAX(created_at) DESC, MAX(id) DESC')
.limit(limit)
elsif object_name == 'Ticket'
state_ids = Ticket::State.by_category(:viewable_agent_new).pluck(:id)
local_recent_views = RecentView.select('o_id, recent_view_object_id, MAX(created_at) as created_at, MAX(id) as id, created_by_id')
.group(:o_id, :recent_view_object_id, :created_by_id)
.where(created_by_id: user.id, recent_view_object_id: ObjectLookup.by_name(object_name))
.order('MAX(created_at) DESC, MAX(id) DESC')
.limit(limit + 10)
clear_list = []
local_recent_views.each do |item|
@ -65,6 +67,7 @@ class RecentView < ApplicationModel
RecentView.select('o_id, recent_view_object_id, MAX(created_at) as created_at, MAX(id) as id, created_by_id')
.group(:o_id, :recent_view_object_id, :created_by_id)
.where(created_by_id: user.id, recent_view_object_id: ObjectLookup.by_name(object_name))
.order('MAX(created_at) DESC, MAX(id) DESC')
.limit(limit)
end

View file

@ -9,7 +9,7 @@ RSpec.describe RecentView, type: :model do
let(:owner) { admin }
describe '::list' do
xit 'returns a sample of recently viewed objects (e.g., tickets/users/organizations)' do
it 'returns a sample of recently viewed objects (e.g., tickets/users/organizations)' do
tickets.each { |t| described_class.log('Ticket', t.id, admin) }
expect(described_class.list(admin).map(&:o_id)).to include(*tickets.last(10).map(&:id))