recent_view.rb 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. RecentView.where( :recent_view_object_id => ObjectLookup.by_name( requested_object ) ).
  20. where( :o_id => requested_object_id ).
  21. destroy_all
  22. end
  23. def self.user_log_destroy( user )
  24. RecentView.where( :created_by_id => user.id ).destroy_all
  25. end
  26. def self.list( user, limit = 10 )
  27. recent_views = RecentView.where( :created_by_id => user.id ).
  28. order('created_at DESC, id DESC').
  29. limit(limit)
  30. list = []
  31. recent_views.each { |item|
  32. data = item.attributes
  33. data['object'] = ObjectLookup.by_id( data['recent_view_object_id'] )
  34. data.delete( 'recent_view_object_id' )
  35. list.push data
  36. }
  37. list
  38. end
  39. def self.list_fulldata( user, limit = 10 )
  40. recent_viewed = self.list( user, limit )
  41. # get related object
  42. assets = ApplicationModel.assets_of_object_list(recent_viewed)
  43. return {
  44. :recent_viewed => recent_viewed,
  45. :assets => assets,
  46. }
  47. end
  48. def notify_clients
  49. data = RecentView.list_fulldata( User.find(self.created_by_id), 10 )
  50. Sessions.send_to(
  51. self.created_by_id,
  52. {
  53. :event => 'update_recent_viewed',
  54. :data => data,
  55. }
  56. )
  57. end
  58. class Object < ApplicationModel
  59. end
  60. end