Refactor RecentView
This commit is contained in:
parent
768788da7e
commit
d51d22213f
1 changed files with 36 additions and 63 deletions
|
@ -13,24 +13,16 @@ class RecentView < ApplicationModel
|
||||||
after_destroy :notify_clients
|
after_destroy :notify_clients
|
||||||
|
|
||||||
def self.log(object, o_id, user)
|
def self.log(object, o_id, user)
|
||||||
|
|
||||||
# access check
|
|
||||||
return if !access(object, o_id, user)
|
return if !access(object, o_id, user)
|
||||||
|
|
||||||
# lookups
|
RecentView.create(o_id: o_id,
|
||||||
object_lookup_id = ObjectLookup.by_name(object)
|
recent_view_object_id: ObjectLookup.by_name(object),
|
||||||
|
created_by_id: user.id)
|
||||||
# create entry
|
|
||||||
record = {
|
|
||||||
o_id: o_id,
|
|
||||||
recent_view_object_id: object_lookup_id.to_i,
|
|
||||||
created_by_id: user.id,
|
|
||||||
}
|
|
||||||
RecentView.create(record)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.log_destroy(requested_object, requested_object_id)
|
def self.log_destroy(requested_object, requested_object_id)
|
||||||
return if requested_object == 'RecentView'
|
return if requested_object == 'RecentView'
|
||||||
|
|
||||||
RecentView.where(recent_view_object_id: ObjectLookup.by_name(requested_object))
|
RecentView.where(recent_view_object_id: ObjectLookup.by_name(requested_object))
|
||||||
.where(o_id: requested_object_id)
|
.where(o_id: requested_object_id)
|
||||||
.destroy_all
|
.destroy_all
|
||||||
|
@ -41,46 +33,34 @@ class RecentView < ApplicationModel
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.list(user, limit = 10, object_name = nil)
|
def self.list(user, limit = 10, object_name = nil)
|
||||||
recent_views = if !object_name
|
recent_views = RecentView.select('o_id, ' \
|
||||||
RecentView.select('o_id, recent_view_object_id, created_by_id, MAX(created_at) as created_at, MAX(id) as id')
|
'recent_view_object_id, ' \
|
||||||
.group(:o_id, :recent_view_object_id, :created_by_id)
|
'created_by_id, ' \
|
||||||
.where(created_by_id: user.id)
|
'MAX(created_at) as created_at, ' \
|
||||||
.order('MAX(created_at) DESC, MAX(id) DESC')
|
'MAX(id) as id')
|
||||||
.limit(limit)
|
.group(:o_id, :recent_view_object_id, :created_by_id)
|
||||||
elsif object_name == 'Ticket'
|
.where(created_by_id: user.id)
|
||||||
state_ids = Ticket::State.by_category(:viewable_agent_new).pluck(:id)
|
.order('MAX(created_at) DESC, MAX(id) DESC')
|
||||||
local_recent_views = RecentView.select('o_id, recent_view_object_id, MAX(created_at) as created_at, MAX(id) as id, created_by_id')
|
.limit(limit)
|
||||||
.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|
|
|
||||||
ticket = Ticket.find_by(id: item.o_id)
|
|
||||||
next if !ticket
|
|
||||||
next if !state_ids.include?(ticket.state_id)
|
|
||||||
clear_list.push item
|
|
||||||
break if clear_list.count == limit
|
|
||||||
end
|
|
||||||
clear_list
|
|
||||||
else
|
|
||||||
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
|
|
||||||
|
|
||||||
list = []
|
if object_name.present?
|
||||||
recent_views.each do |item|
|
recent_views = recent_views.where(recent_view_object_id: ObjectLookup.by_name(object_name))
|
||||||
|
|
||||||
# access check
|
|
||||||
next if !access(ObjectLookup.by_id(item['recent_view_object_id']), item['o_id'], user)
|
|
||||||
|
|
||||||
# add to result list
|
|
||||||
list.push item
|
|
||||||
end
|
end
|
||||||
list
|
|
||||||
|
# hide merged / removed tickets in Ticket Merge dialog
|
||||||
|
if object_name == 'Ticket'
|
||||||
|
recent_views = recent_views.limit(limit * 2)
|
||||||
|
|
||||||
|
viewable_ticket_ids = Ticket.where('id IN (?) AND state_id in (?)',
|
||||||
|
recent_views.map(&:o_id),
|
||||||
|
Ticket::State.by_category(:viewable_agent_new).pluck(:id))
|
||||||
|
.pluck(:id)
|
||||||
|
|
||||||
|
recent_views = recent_views.select { |rv| viewable_ticket_ids.include?(rv.o_id) }
|
||||||
|
.first(limit)
|
||||||
|
end
|
||||||
|
|
||||||
|
recent_views.select { |rv| access(ObjectLookup.by_id(rv.recent_view_object_id), rv.o_id, user) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def notify_clients
|
def notify_clients
|
||||||
|
@ -94,19 +74,12 @@ class RecentView < ApplicationModel
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.access(object, o_id, user)
|
def self.access(object, o_id, user)
|
||||||
|
object.to_s
|
||||||
# check if object exists
|
.constantize
|
||||||
begin
|
.try(:lookup, { id: o_id })
|
||||||
return if !Kernel.const_get(object)
|
.try(:access?, user, 'read')
|
||||||
record = Kernel.const_get(object).lookup(id: o_id)
|
rescue NameError
|
||||||
return if !record
|
false
|
||||||
rescue
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
# check permission
|
|
||||||
return if !record.respond_to?(:access?)
|
|
||||||
record.access?(user, 'read')
|
|
||||||
end
|
end
|
||||||
|
|
||||||
=begin
|
=begin
|
||||||
|
|
Loading…
Reference in a new issue