recent_view.rb 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
  2. class RecentView < ApplicationModel
  3. belongs_to :object_lookup, :class_name => 'ObjectLookup'
  4. after_create :notify_clients
  5. after_update :notify_clients
  6. after_destroy :notify_clients
  7. def self.log( object, o_id, user )
  8. # lookups
  9. object_lookup_id = ObjectLookup.by_name( object )
  10. # create entry
  11. record = {
  12. :o_id => o_id,
  13. :recent_view_object_id => object_lookup_id.to_i,
  14. :created_by_id => user.id,
  15. }
  16. RecentView.create(record)
  17. end
  18. def self.log_destroy( requested_object, requested_object_id )
  19. return if requested_object == 'RecentView'
  20. RecentView.where( :recent_view_object_id => ObjectLookup.by_name( requested_object ) ).
  21. where( :o_id => requested_object_id ).
  22. destroy_all
  23. end
  24. def self.user_log_destroy( user )
  25. RecentView.where( :created_by_id => user.id ).destroy_all
  26. end
  27. def self.list( user, limit = 10 )
  28. recent_views = RecentView.where( :created_by_id => user.id ).
  29. order('created_at DESC, id DESC').
  30. limit(limit)
  31. list = []
  32. recent_views.each { |item|
  33. data = item.attributes
  34. data['object'] = ObjectLookup.by_id( data['recent_view_object_id'] )
  35. data.delete( 'recent_view_object_id' )
  36. list.push data
  37. }
  38. list
  39. end
  40. def self.list_fulldata( user, limit = 10 )
  41. recent_viewed = self.list( user, limit )
  42. # get related users
  43. assets = {}
  44. ticket_ids = []
  45. recent_viewed.each {|item|
  46. # get related objects
  47. require item['object'].to_filename
  48. record = Kernel.const_get( item['object'] ).find( item['o_id'] )
  49. assets = record.assets(assets)
  50. }
  51. return {
  52. :recent_viewed => recent_viewed,
  53. :assets => assets,
  54. }
  55. end
  56. def notify_clients
  57. data = RecentView.list_fulldata( User.find(self.created_by_id), 10 )
  58. Sessions.send_to(
  59. self.created_by_id,
  60. {
  61. :event => 'update_recent_viewed',
  62. :data => data,
  63. }
  64. )
  65. end
  66. class Object < ApplicationModel
  67. end
  68. end