history.rb 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. class History < ActiveRecord::Base
  2. self.table_name = 'histories'
  3. belongs_to :history_type, :class_name => 'History::Type'
  4. belongs_to :history_object, :class_name => 'History::Object'
  5. belongs_to :history_attribute, :class_name => 'History::Attribute'
  6. # before_validation :check_type, :check_object
  7. # attr_writer :history_type, :history_object
  8. @@cache_type = {}
  9. @@cache_object = {}
  10. @@cache_attribute = {}
  11. def self.history_create(data)
  12. # lookups
  13. if data[:history_type]
  14. history_type = self.history_type_lookup( data[:history_type] )
  15. end
  16. if data[:history_object]
  17. history_object = self.history_object_lookup( data[:history_object] )
  18. end
  19. related_history_object_id = nil
  20. if data[:related_history_object]
  21. related_history_object = self.history_object_lookup( data[:related_history_object] )
  22. related_history_object_id = related_history_object.id
  23. end
  24. history_attribute_id = nil
  25. if data[:history_attribute]
  26. history_attribute = self.history_attribute_lookup( data[:history_attribute] )
  27. history_attribute_id = history_attribute.id
  28. end
  29. # create history
  30. History.create(
  31. :o_id => data[:o_id],
  32. :history_type_id => history_type.id,
  33. :history_object_id => history_object.id,
  34. :history_attribute_id => history_attribute_id,
  35. :related_history_object_id => related_history_object_id,
  36. :related_o_id => data[:related_o_id],
  37. :value_from => data[:value_from],
  38. :value_to => data[:value_to],
  39. :id_from => data[:id_from],
  40. :id_to => data[:id_to],
  41. :created_by_id => data[:created_by_id]
  42. )
  43. end
  44. def self.history_destroy( requested_object, requested_object_id )
  45. History.where( :history_object_id => History::Object.where( :name => requested_object ) ).
  46. where( :o_id => requested_object_id ).
  47. destroy_all
  48. end
  49. def self.history_list( requested_object, requested_object_id, related_history_object = nil )
  50. if !related_history_object
  51. history_object = self.history_object_lookup( requested_object )
  52. history = History.where( :history_object_id => history_object.id ).
  53. where( :o_id => requested_object_id ).
  54. where( :history_type_id => History::Type.where( :name => ['created', 'updated', 'notification', 'email', 'added', 'removed'] ) ).
  55. order('created_at ASC, id ASC')
  56. else
  57. history_object_requested = self.history_object_lookup( requested_object )
  58. history_object_related = self.history_object_lookup( related_history_object )
  59. history = History.where(
  60. '((history_object_id = ? AND o_id = ?) OR (history_object_id = ? AND related_o_id = ? )) AND history_type_id IN (?)',
  61. history_object_requested.id,
  62. requested_object_id,
  63. history_object_related.id,
  64. requested_object_id,
  65. History::Type.where( :name => ['created', 'updated', 'notification', 'email', 'added', 'removed'] )
  66. ).
  67. order('created_at ASC, id ASC')
  68. end
  69. list = []
  70. history.each { |item|
  71. item_tmp = item.attributes
  72. item_tmp['history_type'] = item.history_type.name
  73. item_tmp['history_object'] = item.history_object.name
  74. if item.history_attribute
  75. item_tmp['history_attribute'] = item.history_attribute.name
  76. end
  77. item_tmp.delete( 'history_attribute_id' )
  78. item_tmp.delete( 'history_object_id' )
  79. item_tmp.delete( 'history_type_id' )
  80. item_tmp.delete( 'o_id' )
  81. item_tmp.delete( 'updated_at' )
  82. if item_tmp['id_to'] == nil && item_tmp['id_from'] == nil
  83. item_tmp.delete( 'id_to' )
  84. item_tmp.delete( 'id_from' )
  85. end
  86. if item_tmp['value_to'] == nil && item_tmp['value_from'] == nil
  87. item_tmp.delete( 'value_to' )
  88. item_tmp.delete( 'value_from' )
  89. end
  90. if item_tmp['related_history_object_id'] == nil
  91. item_tmp.delete( 'related_history_object_id' )
  92. end
  93. if item_tmp['related_o_id'] == nil
  94. item_tmp.delete( 'related_o_id' )
  95. end
  96. list.push item_tmp
  97. }
  98. return list
  99. end
  100. def self.activity_stream( user, limit = 10 )
  101. # g = Group.where( :active => true ).joins(:users).where( 'users.id' => user.id )
  102. # stream = History.select("distinct(histories.o_id), created_by_id, history_attribute_id, history_type_id, history_object_id, value_from, value_to").
  103. # where( :history_type_id => History::Type.where( :name => ['created', 'updated']) ).
  104. stream = History.select("distinct(histories.o_id), created_by_id, history_type_id, history_object_id").
  105. where( :history_object_id => History::Object.where( :name => [ 'Ticket', 'Ticket::Article' ] ) ).
  106. where( :history_type_id => History::Type.where( :name => [ 'created', 'updated' ]) ).
  107. order('created_at DESC, id DESC').
  108. limit(limit)
  109. datas = []
  110. stream.each do |item|
  111. data = item.attributes
  112. data['history_object'] = self.history_object_lookup_id( data['history_object_id'] ).name
  113. data['history_type'] = self.history_type_lookup_id( data['history_type_id'] ).name
  114. data.delete('history_object_id')
  115. data.delete('history_type_id')
  116. datas.push data
  117. # item['history_attribute'] = item.history_attribute
  118. end
  119. return datas
  120. end
  121. def self.activity_stream_fulldata( user, limit = 10 )
  122. activity_stream = History.activity_stream( user, limit )
  123. # get related users
  124. users = {}
  125. tickets = []
  126. articles = []
  127. activity_stream.each {|item|
  128. # load article ids
  129. if item['history_object'] == 'Ticket'
  130. ticket = Ticket.find( item['o_id'] ).attributes
  131. tickets.push ticket
  132. # load users
  133. if !users[ ticket['owner_id'] ]
  134. users[ ticket['owner_id'] ] = User.user_data_full( ticket['owner_id'] )
  135. end
  136. if !users[ ticket['customer_id'] ]
  137. users[ ticket['customer_id'] ] = User.user_data_full( ticket['customer_id'] )
  138. end
  139. end
  140. if item['history_object'] == 'Ticket::Article'
  141. article = Ticket::Article.find( item['o_id'] ).attributes
  142. if !article['subject'] || article['subject'] == ''
  143. article['subject'] = Ticket.find( article['ticket_id'] ).title
  144. end
  145. articles.push article
  146. # load users
  147. if !users[ article['created_by_id'] ]
  148. users[ article['created_by_id'] ] = User.user_data_full( article['created_by_id'] )
  149. end
  150. end
  151. if item['history_object'] == 'User'
  152. users[ item['o_id'] ] = User.user_data_full( item['o_id'] )
  153. end
  154. # load users
  155. if !users[ item['created_by_id'] ]
  156. users[ item['created_by_id'] ] = User.user_data_full( item['created_by_id'] )
  157. end
  158. }
  159. return {
  160. :activity_stream => activity_stream,
  161. :tickets => tickets,
  162. :articles => articles,
  163. :users => users,
  164. }
  165. end
  166. def self.log_view ( object, current_user )
  167. history_type = self.history_type_lookup( 'viewed' )
  168. history_object = self.history_object_lookup( object.class.name )
  169. History.create(
  170. :o_id => object.id,
  171. :history_type_id => history_type.id,
  172. :history_object_id => history_object.id,
  173. :created_by_id => current_user.id
  174. )
  175. end
  176. def self.recent_viewed( user, limit = 10 )
  177. # g = Group.where( :active => true ).joins(:users).where( 'users.id' => user.id )
  178. history_type = self.history_type_lookup( 'viewed' )
  179. history_object = self.history_object_lookup( 'Ticket' )
  180. stream = History.select("distinct(o_id), created_by_id, history_type_id, history_object_id, created_at").
  181. where( :history_object_id => history_object.id ).
  182. where( :history_type_id => history_type.id ).
  183. where( :created_by_id => user.id ).
  184. order('created_at DESC, id ASC').
  185. limit(limit)
  186. datas = []
  187. stream.each do |item|
  188. data = item.attributes
  189. data['history_object'] = self.history_object_lookup_id( data['history_object_id'] ).name
  190. data['history_type'] = self.history_type_lookup_id( data['history_type_id'] ).name
  191. datas.push data
  192. # item['history_attribute'] = item.history_attribute
  193. end
  194. # puts 'pppppppppp'
  195. # puts datas.inspect
  196. return datas
  197. end
  198. def self.recent_viewed_fulldata( user, limit = 10 )
  199. recent_viewed = History.recent_viewed( user, limit )
  200. # get related users
  201. users = {}
  202. tickets = []
  203. recent_viewed.each {|item|
  204. # load article ids
  205. # if item.history_object == 'Ticket'
  206. ticket = Ticket.find( item['o_id'] ).attributes
  207. tickets.push ticket
  208. # end
  209. # if item.history_object 'Ticket::Article'
  210. # tickets.push Ticket::Article.find(item.o_id)
  211. # end
  212. # if item.history_object 'User'
  213. # tickets.push User.find(item.o_id)
  214. # end
  215. # load users
  216. if !users[ ticket['owner_id'] ]
  217. users[ ticket['owner_id'] ] = User.user_data_full( ticket['owner_id'] )
  218. end
  219. if !users[ ticket['created_by_id'] ]
  220. users[ ticket['created_by_id'] ] = User.user_data_full( ticket['created_by_id'] )
  221. end
  222. if !users[ item['created_by_id'] ]
  223. users[ item['created_by_id'] ] = User.user_data_full( item['created_by_id'] )
  224. end
  225. }
  226. return {
  227. :recent_viewed => recent_viewed,
  228. :tickets => tickets,
  229. :users => users,
  230. }
  231. end
  232. private
  233. def self.history_type_lookup_id( id )
  234. # use cache
  235. return @@cache_type[ id ] if @@cache_type[ id ]
  236. # lookup
  237. history_type = History::Type.find(id)
  238. @@cache_type[ id ] = history_type
  239. return history_type
  240. end
  241. def self.history_type_lookup( name )
  242. # use cache
  243. return @@cache_type[ name ] if @@cache_type[ name ]
  244. # lookup
  245. history_type = History::Type.where( :name => name ).first
  246. if history_type
  247. @@cache_type[ name ] = history_type
  248. return history_type
  249. end
  250. # create
  251. history_type = History::Type.create(
  252. :name => name
  253. )
  254. @@cache_type[ name ] = history_type
  255. return history_type
  256. end
  257. def self.history_object_lookup_id( id )
  258. # use cache
  259. return @@cache_object[ id ] if @@cache_object[ id ]
  260. # lookup
  261. history_object = History::Object.find(id)
  262. @@cache_object[ id ] = history_object
  263. return history_object
  264. end
  265. def self.history_object_lookup( name )
  266. # use cache
  267. return @@cache_object[ name ] if @@cache_object[ name ]
  268. # lookup
  269. history_object = History::Object.where( :name => name ).first
  270. if history_object
  271. @@cache_object[ name ] = history_object
  272. return history_object
  273. end
  274. # create
  275. history_object = History::Object.create(
  276. :name => name
  277. )
  278. @@cache_object[ name ] = history_object
  279. return history_object
  280. end
  281. def self.history_attribute_lookup( name )
  282. # use cache
  283. return @@cache_attribute[ name ] if @@cache_attribute[ name ]
  284. # lookup
  285. history_attribute = History::Attribute.where( :name => name ).first
  286. if history_attribute
  287. @@cache_attribute[ name ] = history_attribute
  288. return history_attribute
  289. end
  290. # create
  291. history_attribute = History::Attribute.create(
  292. :name => name
  293. )
  294. @@cache_attribute[ name ] = history_attribute
  295. return history_attribute
  296. end
  297. class Object < ActiveRecord::Base
  298. end
  299. class Type < ActiveRecord::Base
  300. end
  301. class Attribute < ActiveRecord::Base
  302. end
  303. end