history.rb 10 KB

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