trabajo-afectivo/app/models/recent_view.rb

109 lines
2.7 KiB
Ruby
Raw Normal View History

2014-02-03 19:23:00 +00:00
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
class RecentView < ApplicationModel
belongs_to :recent_view_object, :class_name => 'RecentView::Object'
@@cache_object = {}
def self.log( object, user )
# lookups
2013-09-23 14:44:43 +00:00
recent_view_object = self.object_lookup( object.class.to_s )
# create entry
record = {
:o_id => object.id,
:recent_view_object_id => recent_view_object.id,
:created_by_id => user.id,
}
RecentView.create(record)
end
def self.log_destroy( requested_object, requested_object_id )
RecentView.where( :recent_view_object_id => RecentView::Object.where( :name => requested_object ) ).
where( :o_id => requested_object_id ).
destroy_all
end
def self.list( user, limit = 10 )
recent_views = RecentView.where( :created_by_id => user.id ).
order('created_at DESC, id DESC').
limit(limit)
list = []
recent_views.each { |item|
data = item.attributes
2013-09-23 14:44:43 +00:00
data['recent_view_object'] = self.object_lookup_id( data['recent_view_object_id'] ).name
data.delete( 'history_object_id' )
list.push data
}
2014-07-13 18:52:32 +00:00
list
end
def self.list_fulldata( user, limit = 10 )
recent_viewed = self.list( user, limit )
# get related users
assets = {}
ticket_ids = []
recent_viewed.each {|item|
# load article ids
# if item.recent_view_object == 'Ticket'
ticket = Ticket.find( item['o_id'] )
ticket_ids.push ticket.id
# end
# if item.recent_view_object 'Ticket::Article'
# tickets.push Ticket::Article.find(item.o_id)
# end
# if item.recent_view_object 'User'
# tickets.push User.find(item.o_id)
# end
assets = ticket.assets(assets)
}
return {
:recent_viewed => recent_viewed,
:ticket_ids => ticket_ids,
:assets => assets,
}
end
private
2013-09-23 14:44:43 +00:00
def self.object_lookup_id( id )
# use cache
return @@cache_object[ id ] if @@cache_object[ id ]
# lookup
2013-09-29 21:00:40 +00:00
history_object = RecentView::Object.lookup( :id => id )
@@cache_object[ id ] = history_object
2013-09-29 21:00:40 +00:00
history_object
end
2013-09-23 14:44:43 +00:00
def self.object_lookup( name )
# use cache
return @@cache_object[ name ] if @@cache_object[ name ]
# lookup
2013-09-29 21:00:40 +00:00
recent_view_object = RecentView::Object.lookup( :name => name )
if recent_view_object
@@cache_object[ name ] = recent_view_object
return recent_view_object
end
# create
recent_view_object = RecentView::Object.create(
:name => name
)
@@cache_object[ name ] = recent_view_object
2013-09-29 21:00:40 +00:00
recent_view_object
end
class Object < ApplicationModel
end
end