history.rb 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. class History < ApplicationModel
  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.add(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. record = {
  31. :id => data[:id],
  32. :o_id => data[:o_id],
  33. :history_type_id => history_type.id,
  34. :history_object_id => history_object.id,
  35. :history_attribute_id => history_attribute_id,
  36. :related_history_object_id => related_history_object_id,
  37. :related_o_id => data[:related_o_id],
  38. :value_from => data[:value_from],
  39. :value_to => data[:value_to],
  40. :id_from => data[:id_from],
  41. :id_to => data[:id_to],
  42. :created_at => data[:created_at],
  43. :created_by_id => data[:created_by_id]
  44. }
  45. history_record = nil
  46. if data[:id]
  47. history_record = History.where( :id => data[:id] ).first
  48. end
  49. if history_record
  50. history_record.update_attributes(record)
  51. else
  52. record_new = History.create(record)
  53. if record[:id]
  54. record_new.id = record[:id]
  55. end
  56. record_new.save
  57. end
  58. end
  59. def self.remove( requested_object, requested_object_id )
  60. history_object = History::Object.where( :name => requested_object ).first
  61. History.where(
  62. :history_object_id => history_object.id,
  63. :o_id => requested_object_id,
  64. ).destroy_all
  65. end
  66. def self.list( requested_object, requested_object_id, related_history_object = nil )
  67. if !related_history_object
  68. history_object = self.history_object_lookup( requested_object )
  69. history = History.where( :history_object_id => history_object.id ).
  70. where( :o_id => requested_object_id ).
  71. where( :history_type_id => History::Type.where( :name => ['created', 'updated', 'notification', 'email', 'added', 'removed'] ) ).
  72. order('created_at ASC, id ASC')
  73. else
  74. history_object_requested = self.history_object_lookup( requested_object )
  75. history_object_related = self.history_object_lookup( related_history_object )
  76. history = History.where(
  77. '((history_object_id = ? AND o_id = ?) OR (history_object_id = ? AND related_o_id = ? )) AND history_type_id IN (?)',
  78. history_object_requested.id,
  79. requested_object_id,
  80. history_object_related.id,
  81. requested_object_id,
  82. History::Type.where( :name => ['created', 'updated', 'notification', 'email', 'added', 'removed'] )
  83. ).
  84. order('created_at ASC, id ASC')
  85. end
  86. return history
  87. end
  88. def self.activity_stream( user, limit = 10 )
  89. # g = Group.where( :active => true ).joins(:users).where( 'users.id' => user.id )
  90. # stream = History.select("distinct(histories.o_id), created_by_id, history_attribute_id, history_type_id, history_object_id, value_from, value_to").
  91. # where( :history_type_id => History::Type.where( :name => ['created', 'updated']) ).
  92. stream = History.select("distinct(histories.o_id), created_by_id, history_type_id, history_object_id").
  93. where( :history_object_id => History::Object.where( :name => [ 'Ticket', 'Ticket::Article' ] ) ).
  94. where( :history_type_id => History::Type.where( :name => [ 'created', 'updated' ]) ).
  95. order('created_at DESC, id DESC').
  96. limit(limit)
  97. datas = []
  98. stream.each do |item|
  99. data = item.attributes
  100. data['history_object'] = self.history_object_lookup_id( data['history_object_id'] ).name
  101. data['history_type'] = self.history_type_lookup_id( data['history_type_id'] ).name
  102. data.delete('history_object_id')
  103. data.delete('history_type_id')
  104. datas.push data
  105. # item['history_attribute'] = item.history_attribute
  106. end
  107. return datas
  108. end
  109. def self.activity_stream_fulldata( user, limit = 10 )
  110. activity_stream = History.activity_stream( user, limit )
  111. # get related users
  112. users = {}
  113. tickets = []
  114. articles = []
  115. activity_stream.each {|item|
  116. # load article ids
  117. if item['history_object'] == 'Ticket'
  118. ticket = Ticket.find( item['o_id'] ).attributes
  119. tickets.push ticket
  120. # load users
  121. if !users[ ticket['owner_id'] ]
  122. users[ ticket['owner_id'] ] = User.user_data_full( ticket['owner_id'] )
  123. end
  124. if !users[ ticket['customer_id'] ]
  125. users[ ticket['customer_id'] ] = User.user_data_full( ticket['customer_id'] )
  126. end
  127. end
  128. if item['history_object'] == 'Ticket::Article'
  129. article = Ticket::Article.find( item['o_id'] ).attributes
  130. if !article['subject'] || article['subject'] == ''
  131. article['subject'] = Ticket.find( article['ticket_id'] ).title
  132. end
  133. articles.push article
  134. # load users
  135. if !users[ article['created_by_id'] ]
  136. users[ article['created_by_id'] ] = User.user_data_full( article['created_by_id'] )
  137. end
  138. end
  139. if item['history_object'] == 'User'
  140. users[ item['o_id'] ] = User.user_data_full( item['o_id'] )
  141. end
  142. # load users
  143. if !users[ item['created_by_id'] ]
  144. users[ item['created_by_id'] ] = User.user_data_full( item['created_by_id'] )
  145. end
  146. }
  147. return {
  148. :activity_stream => activity_stream,
  149. :tickets => tickets,
  150. :articles => articles,
  151. :users => users,
  152. }
  153. end
  154. private
  155. def self.history_type_lookup_id( id )
  156. # use cache
  157. return @@cache_type[ id ] if @@cache_type[ id ]
  158. # lookup
  159. history_type = History::Type.find(id)
  160. @@cache_type[ id ] = history_type
  161. return history_type
  162. end
  163. def self.history_type_lookup( name )
  164. # use cache
  165. return @@cache_type[ name ] if @@cache_type[ name ]
  166. # lookup
  167. history_type = History::Type.where( :name => name ).first
  168. if history_type
  169. @@cache_type[ name ] = history_type
  170. return history_type
  171. end
  172. # create
  173. history_type = History::Type.create(
  174. :name => name
  175. )
  176. @@cache_type[ name ] = history_type
  177. return history_type
  178. end
  179. def self.history_object_lookup_id( id )
  180. # use cache
  181. return @@cache_object[ id ] if @@cache_object[ id ]
  182. # lookup
  183. history_object = History::Object.find(id)
  184. @@cache_object[ id ] = history_object
  185. return history_object
  186. end
  187. def self.history_object_lookup( name )
  188. # use cache
  189. return @@cache_object[ name ] if @@cache_object[ name ]
  190. # lookup
  191. history_object = History::Object.where( :name => name ).first
  192. if history_object
  193. @@cache_object[ name ] = history_object
  194. return history_object
  195. end
  196. # create
  197. history_object = History::Object.create(
  198. :name => name
  199. )
  200. @@cache_object[ name ] = history_object
  201. return history_object
  202. end
  203. def self.history_attribute_lookup( name )
  204. # use cache
  205. return @@cache_attribute[ name ] if @@cache_attribute[ name ]
  206. # lookup
  207. history_attribute = History::Attribute.where( :name => name ).first
  208. if history_attribute
  209. @@cache_attribute[ name ] = history_attribute
  210. return history_attribute
  211. end
  212. # create
  213. history_attribute = History::Attribute.create(
  214. :name => name
  215. )
  216. @@cache_attribute[ name ] = history_attribute
  217. return history_attribute
  218. end
  219. class Object < ApplicationModel
  220. end
  221. class Type < ApplicationModel
  222. end
  223. class Attribute < ApplicationModel
  224. end
  225. end