trabajo-afectivo/app/models/recent_view.rb

82 lines
2 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
2014-08-23 22:29:04 +00:00
belongs_to :object_lookup, :class_name => 'ObjectLookup'
after_create :notify_clients
after_update :notify_clients
after_destroy :notify_clients
def self.log( object, o_id, user )
# lookups
object_lookup_id = ObjectLookup.by_name( object )
# create entry
record = {
2014-08-24 00:16:50 +00:00
:o_id => o_id,
:recent_view_object_id => object_lookup_id.to_i,
:created_by_id => user.id,
}
RecentView.create(record)
end
def self.log_destroy( requested_object, requested_object_id )
2014-08-24 00:16:50 +00:00
RecentView.where( :recent_view_object_id => ObjectLookup.by_name( requested_object ) ).
where( :o_id => requested_object_id ).
destroy_all
end
2014-08-23 22:29:04 +00:00
def self.user_log_destroy( user )
RecentView.where( :created_by_id => user.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
2014-08-24 00:16:50 +00:00
data['object'] = ObjectLookup.by_id( data['recent_view_object_id'] )
data.delete( 'recent_view_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|
2014-08-23 22:29:04 +00:00
# get related objects
require item['object'].to_filename
record = Kernel.const_get( item['object'] ).find( item['o_id'] )
assets = record.assets(assets)
}
return {
:recent_viewed => recent_viewed,
:assets => assets,
}
end
def notify_clients
data = RecentView.list_fulldata( User.find(self.created_by_id), 10 )
Sessions.send_to(
self.created_by_id,
{
:event => 'update_recent_viewed',
:data => data,
}
)
end
class Object < ApplicationModel
end
2014-08-23 22:29:04 +00:00
end