recent_view.rb 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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, type = nil )
  28. if !type
  29. recent_views = RecentView.where( :created_by_id => user.id ).
  30. order('created_at DESC, id DESC').
  31. limit(limit)
  32. else
  33. recent_views = RecentView.select('DISTINCT(o_id), recent_view_object_id').where( :created_by_id => user.id, :recent_view_object_id => ObjectLookup.by_name(type) ) .
  34. order('created_at DESC, id DESC').
  35. limit(limit)
  36. end
  37. list = []
  38. recent_views.each { |item|
  39. data = item.attributes
  40. data['object'] = ObjectLookup.by_id( data['recent_view_object_id'] )
  41. data.delete( 'recent_view_object_id' )
  42. list.push data
  43. }
  44. list
  45. end
  46. def self.list_full( user, limit = 10 )
  47. recent_viewed = self.list( user, limit )
  48. # get related object
  49. assets = ApplicationModel.assets_of_object_list(recent_viewed)
  50. return {
  51. :stream => recent_viewed,
  52. :assets => assets,
  53. }
  54. end
  55. def notify_clients
  56. Sessions.send_to(
  57. self.created_by_id,
  58. {
  59. :event => 'RecentView::changed',
  60. :data => {}
  61. }
  62. )
  63. end
  64. class Object < ApplicationModel
  65. end
  66. end