recent_view.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/
  2. class RecentView < ApplicationModel
  3. belongs_to :recent_view_object, :class_name => 'RecentView::Object'
  4. @@cache_object = {}
  5. def self.log( object, user )
  6. # lookups
  7. recent_view_object = self.recent_view_object_lookup( object.class.to_s )
  8. # create entry
  9. record = {
  10. :o_id => object.id,
  11. :recent_view_object_id => recent_view_object.id,
  12. :created_by_id => user.id,
  13. }
  14. RecentView.create(record)
  15. end
  16. def self.log_destroy( requested_object, requested_object_id )
  17. RecentView.where( :recent_view_object_id => RecentView::Object.where( :name => requested_object ) ).
  18. where( :o_id => requested_object_id ).
  19. destroy_all
  20. end
  21. def self.list( user, limit = 10 )
  22. recent_views = RecentView.where( :created_by_id => user.id ).
  23. order('created_at DESC, id DESC').
  24. limit(limit)
  25. list = []
  26. recent_views.each { |item|
  27. data = item.attributes
  28. data['recent_view_object'] = self.recent_view_object_lookup_id( data['recent_view_object_id'] ).name
  29. data.delete( 'history_object_id' )
  30. list.push data
  31. }
  32. return list
  33. end
  34. def self.list_fulldata( user, limit = 10 )
  35. recent_viewed = self.list( user, limit )
  36. # get related users
  37. assets = {}
  38. ticket_ids = []
  39. recent_viewed.each {|item|
  40. # load article ids
  41. # if item.recent_view_object == 'Ticket'
  42. ticket = Ticket.find( item['o_id'] )
  43. ticket_ids.push ticket.id
  44. # end
  45. # if item.recent_view_object 'Ticket::Article'
  46. # tickets.push Ticket::Article.find(item.o_id)
  47. # end
  48. # if item.recent_view_object 'User'
  49. # tickets.push User.find(item.o_id)
  50. # end
  51. assets = ticket.assets(assets)
  52. }
  53. return {
  54. :recent_viewed => recent_viewed,
  55. :ticket_ids => ticket_ids,
  56. :assets => assets,
  57. }
  58. end
  59. private
  60. def self.recent_view_object_lookup_id( id )
  61. # use cache
  62. return @@cache_object[ id ] if @@cache_object[ id ]
  63. # lookup
  64. history_object = RecentView::Object.find(id)
  65. @@cache_object[ id ] = history_object
  66. return history_object
  67. end
  68. def self.recent_view_object_lookup( name )
  69. # use cache
  70. return @@cache_object[ name ] if @@cache_object[ name ]
  71. # lookup
  72. recent_view_object = RecentView::Object.where( :name => name ).first
  73. if recent_view_object
  74. @@cache_object[ name ] = recent_view_object
  75. return recent_view_object
  76. end
  77. # create
  78. recent_view_object = RecentView::Object.create(
  79. :name => name
  80. )
  81. @@cache_object[ name ] = recent_view_object
  82. return recent_view_object
  83. end
  84. class Object < ApplicationModel
  85. end
  86. end