application_model.rb 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. # Copyright (C) 2013-2013 Zammad Foundation, http://zammad-foundation.org/
  2. class ApplicationModel < ActiveRecord::Base
  3. include ApplicationModel::HistoryLogBase
  4. include ApplicationModel::ActivityStreamBase
  5. self.abstract_class = true
  6. before_create :check_attributes_protected, :cache_delete, :fill_up_user_create
  7. before_create :cache_delete, :fill_up_user_create
  8. before_update :cache_delete_before, :fill_up_user_update
  9. before_destroy :cache_delete_before, :destroy_dependencies
  10. after_create :cache_delete
  11. after_update :cache_delete
  12. after_destroy :cache_delete
  13. after_create :activity_stream_create
  14. after_update :activity_stream_update
  15. after_destroy :activity_stream_destroy
  16. after_create :history_create
  17. after_update :history_update
  18. after_destroy :history_destroy
  19. # create instance accessor
  20. class << self
  21. attr_accessor :activity_stream_support_config, :history_support_config
  22. end
  23. @@import_class_list = ['Ticket', 'Ticket::Article', 'History', 'Ticket::State', 'Ticket::Priority', 'Group', 'User' ]
  24. def check_attributes_protected
  25. if Setting.get('import_mode') && @@import_class_list.include?( self.name.to_s )
  26. # do noting, use id as it is
  27. else
  28. self[:id] = nil
  29. end
  30. end
  31. =begin
  32. remove all not used model attributes of params
  33. result = Model.param_cleanup(params)
  34. returns
  35. result = params # params with valid attributes of model
  36. =end
  37. def self.param_cleanup(params)
  38. if params == nil
  39. raise "No params for #{self.to_s}!"
  40. end
  41. # only use object attributes
  42. data = {}
  43. self.new.attributes.each {|item|
  44. if params.has_key?(item[0])
  45. # puts 'use ' + item[0].to_s + '-' + params[item[0]].to_s
  46. data[item[0].to_sym] = params[item[0]]
  47. end
  48. }
  49. # we do want to set this via database
  50. self.param_validation(data)
  51. end
  52. =begin
  53. remove all not used params of object (per default :updated_at, :created_at, :updated_by_id and :created_by_id)
  54. result = Model.param_validation(params)
  55. returns
  56. result = params # params without listed attributes
  57. =end
  58. def self.param_validation(data)
  59. # we do want to set this via database
  60. data.delete( :updated_at )
  61. data.delete( :created_at )
  62. data.delete( :updated_by_id )
  63. data.delete( :created_by_id )
  64. if data.respond_to?('permit!')
  65. data.permit!
  66. end
  67. data
  68. end
  69. =begin
  70. set created_by_id & updated_by_id if not given based on UserInfo (current session)
  71. Used as before_create callback, no own use needed
  72. result = Model.fill_up_user_create(params)
  73. returns
  74. result = params # params with updated_by_id & created_by_id if not given based on UserInfo (current session)
  75. =end
  76. def fill_up_user_create
  77. if self.class.column_names.include? 'updated_by_id'
  78. if UserInfo.current_user_id
  79. if self.updated_by_id && self.updated_by_id != UserInfo.current_user_id
  80. puts "NOTICE create - self.updated_by_id is different: #{self.updated_by_id.to_s}/#{UserInfo.current_user_id.to_s}"
  81. end
  82. self.updated_by_id = UserInfo.current_user_id
  83. end
  84. end
  85. if self.class.column_names.include? 'created_by_id'
  86. if UserInfo.current_user_id
  87. if self.created_by_id && self.created_by_id != UserInfo.current_user_id
  88. puts "NOTICE create - self.created_by_id is different: #{self.created_by_id.to_s}/#{UserInfo.current_user_id.to_s}"
  89. end
  90. self.created_by_id = UserInfo.current_user_id
  91. end
  92. end
  93. end
  94. =begin
  95. set updated_by_id if not given based on UserInfo (current session)
  96. Used as before_update callback, no own use needed
  97. result = Model.fill_up_user_update(params)
  98. returns
  99. result = params # params with updated_by_id & created_by_id if not given based on UserInfo (current session)
  100. =end
  101. def fill_up_user_update
  102. return if !self.class.column_names.include? 'updated_by_id'
  103. if UserInfo.current_user_id
  104. self.updated_by_id = UserInfo.current_user_id
  105. end
  106. end
  107. def cache_update(o)
  108. # puts 'u ' + self.class.to_s
  109. if self.respond_to?('cache_delete') then self.cache_delete end
  110. # puts 'g ' + group.class.to_s
  111. if o.respond_to?('cache_delete') then o.cache_delete end
  112. end
  113. def cache_delete_before
  114. old_object = self.class.where( :id => self.id ).first
  115. if old_object
  116. old_object.cache_delete
  117. end
  118. self.cache_delete
  119. end
  120. def cache_delete
  121. key = self.class.to_s + '::' + self.id.to_s
  122. Cache.delete( key.to_s )
  123. key = self.class.to_s + ':f:' + self.id.to_s
  124. Cache.delete( key.to_s )
  125. if self[:name]
  126. key = self.class.to_s + '::' + self.name.to_s
  127. Cache.delete( key.to_s )
  128. key = self.class.to_s + ':f:' + self.name.to_s
  129. Cache.delete( key.to_s )
  130. end
  131. if self[:login]
  132. key = self.class.to_s + '::' + self.login.to_s
  133. Cache.delete( key.to_s )
  134. key = self.class.to_s + ':f:' + self.login.to_s
  135. Cache.delete( key.to_s )
  136. end
  137. end
  138. def self.cache_set(data_id, data, full = false)
  139. if !full
  140. key = self.to_s + '::' + data_id.to_s
  141. else
  142. key = self.to_s + ':f:' + data_id.to_s
  143. end
  144. Cache.write( key.to_s, data )
  145. end
  146. def self.cache_get(data_id, full = false)
  147. if !full
  148. key = self.to_s + '::' + data_id.to_s
  149. else
  150. key = self.to_s + ':f:' + data_id.to_s
  151. end
  152. Cache.get( key.to_s )
  153. end
  154. =begin
  155. lookup model from cache (if exists) or retrieve it from db, id, name or login possible
  156. result = Model.lookup( :id => 123 )
  157. result = Model.lookup( :name => 'some name' )
  158. result = Model.lookup( :login => 'some login' )
  159. returns
  160. result = model # with all attributes
  161. =end
  162. def self.lookup(data)
  163. if data[:id]
  164. # puts "GET- + #{self.to_s}.#{data[:id].to_s}"
  165. cache = self.cache_get( data[:id] )
  166. return cache if cache
  167. # puts "Fillup- + #{self.to_s}.#{data[:id].to_s}"
  168. record = self.where( :id => data[:id] ).first
  169. self.cache_set( data[:id], record )
  170. return record
  171. elsif data[:name]
  172. cache = self.cache_get( data[:name] )
  173. return cache if cache
  174. records = self.where( :name => data[:name] )
  175. records.each {|record|
  176. if record.name == data[:name]
  177. self.cache_set( data[:name], record )
  178. return record
  179. end
  180. }
  181. return
  182. elsif data[:login]
  183. cache = self.cache_get( data[:login] )
  184. return cache if cache
  185. records = self.where( :login => data[:login] )
  186. records.each {|record|
  187. if record.login == data[:login]
  188. self.cache_set( data[:login], record )
  189. return record
  190. end
  191. }
  192. return
  193. else
  194. raise "Need name, id or login for lookup()"
  195. end
  196. end
  197. =begin
  198. create model if not exists (check exists based on id, name, login or locale)
  199. result = Model.create_if_not_exists( attributes )
  200. returns
  201. result = model # with all attributes
  202. =end
  203. def self.create_if_not_exists(data)
  204. if data[:id]
  205. record = self.where( :id => data[:id] ).first
  206. return record if record
  207. elsif data[:name]
  208. records = self.where( :name => data[:name] )
  209. records.each {|record|
  210. return record if record.name == data[:name]
  211. }
  212. elsif data[:login]
  213. records = self.where( :login => data[:login] )
  214. records.each {|record|
  215. return record if record.login == data[:login]
  216. }
  217. elsif data[:locale] && data[:source]
  218. records = self.where( :locale => data[:locale], :source => data[:source] )
  219. records.each {|record|
  220. return record if record.source == data[:source]
  221. }
  222. end
  223. self.create(data)
  224. end
  225. =begin
  226. create or update model (check exists based on name, login or locale)
  227. result = Model.create_or_update( attributes )
  228. returns
  229. result = model # with all attributes
  230. =end
  231. def self.create_or_update(data)
  232. if data[:name]
  233. records = self.where( :name => data[:name] )
  234. records.each {|record|
  235. if record.name == data[:name]
  236. record.update_attributes( data )
  237. return record
  238. end
  239. }
  240. record = self.new( data )
  241. record.save
  242. return record
  243. elsif data[:login]
  244. records = self.where( :login => data[:login] )
  245. records.each {|record|
  246. if record.login.downcase == data[:login].downcase
  247. record.update_attributes( data )
  248. return record
  249. end
  250. }
  251. record = self.new( data )
  252. record.save
  253. return record
  254. elsif data[:locale]
  255. records = self.where( :locale => data[:locale] )
  256. records.each {|record|
  257. if record.locale.downcase == data[:locale].downcase
  258. record.update_attributes( data )
  259. return record
  260. end
  261. }
  262. record = self.new( data )
  263. record.save
  264. return record
  265. else
  266. raise "Need name, login or locale for create_or_update()"
  267. end
  268. end
  269. =begin
  270. notify_clients_after_create after model got created
  271. used as callback in model file
  272. class OwnModel < ApplicationModel
  273. after_create :notify_clients_after_create
  274. after_update :notify_clients_after_update
  275. after_destroy :notify_clients_after_destroy
  276. [...]
  277. =end
  278. def notify_clients_after_create
  279. # return if we run import mode
  280. return if Setting.get('import_mode')
  281. class_name = self.class.name
  282. class_name.gsub!(/::/, '')
  283. Sessions.broadcast(
  284. :event => class_name + ':created',
  285. :data => { :id => self.id, :updated_at => self.updated_at }
  286. )
  287. end
  288. =begin
  289. notify_clients_after_update after model got updated
  290. used as callback in model file
  291. class OwnModel < ApplicationModel
  292. after_create :notify_clients_after_create
  293. after_update :notify_clients_after_update
  294. after_destroy :notify_clients_after_destroy
  295. [...]
  296. =end
  297. def notify_clients_after_update
  298. # return if we run import mode
  299. return if Setting.get('import_mode')
  300. puts "#{self.class.name.downcase} UPDATED " + self.updated_at.to_s
  301. class_name = self.class.name
  302. class_name.gsub!(/::/, '')
  303. Sessions.broadcast(
  304. :event => class_name + ':updated',
  305. :data => { :id => self.id, :updated_at => self.updated_at }
  306. )
  307. end
  308. =begin
  309. notify_clients_after_destroy after model got destroyed
  310. used as callback in model file
  311. class OwnModel < ApplicationModel
  312. after_create :notify_clients_after_create
  313. after_update :notify_clients_after_update
  314. after_destroy :notify_clients_after_destroy
  315. [...]
  316. =end
  317. def notify_clients_after_destroy
  318. # return if we run import mode
  319. return if Setting.get('import_mode')
  320. puts "#{self.class.name.downcase} DESTOY " + self.updated_at.to_s
  321. class_name = self.class.name
  322. class_name.gsub!(/::/, '')
  323. Sessions.broadcast(
  324. :event => class_name + ':destroy',
  325. :data => { :id => self.id, :updated_at => self.updated_at }
  326. )
  327. end
  328. private
  329. =begin
  330. serve methode to configure and enable activity stream support for this model
  331. class Model < ApplicationModel
  332. activity_stream_support :role => 'Admin'
  333. end
  334. =end
  335. def self.activity_stream_support(data = {})
  336. @activity_stream_support_config = data
  337. end
  338. =begin
  339. log object create activity stream, if configured - will be executed automatically
  340. model = Model.find(123)
  341. model.activity_stream_create
  342. =end
  343. def activity_stream_create
  344. activity_stream_log( 'created', self['created_by_id'] )
  345. end
  346. =begin
  347. log object update activity stream, if configured - will be executed automatically
  348. model = Model.find(123)
  349. model.activity_stream_update
  350. =end
  351. def activity_stream_update
  352. return if !self.changed?
  353. activity_stream_log( 'updated', self['updated_by_id'] )
  354. end
  355. =begin
  356. delete object activity stream, will be executed automatically
  357. model = Model.find(123)
  358. model.activity_stream_destroy
  359. =end
  360. def activity_stream_destroy
  361. ActivityStream.remove( self.class.to_s, self.id )
  362. end
  363. =begin
  364. serve methode to configure and enable history support for this model
  365. class Model < ApplicationModel
  366. history_support
  367. end
  368. class Model < ApplicationModel
  369. history_support :ignore_attributes => { :article_count => true }
  370. end
  371. =end
  372. def self.history_support(data = {})
  373. @history_support_config = data
  374. end
  375. =begin
  376. log object create history, if configured - will be executed automatically
  377. model = Model.find(123)
  378. model.history_create
  379. =end
  380. def history_create
  381. return if !self.class.history_support_config
  382. # puts self.changes.inspect
  383. self.history_log( 'created', self.created_by_id )
  384. end
  385. =begin
  386. log object update history with all updated attributes, if configured - will be executed automatically
  387. model = Model.find(123)
  388. model.history_update
  389. =end
  390. def history_update
  391. return if !self.class.history_support_config
  392. return if !self.changed?
  393. # return if it's no update
  394. return if self.new_record?
  395. # new record also triggers update, so ignore new records
  396. changes = self.changes
  397. return if changes['id'] && !changes['id'][0]
  398. #puts 'updated' + self.changes.inspect
  399. # TODO: Swop it to config file later
  400. ignore_attributes = {
  401. :created_at => true,
  402. :updated_at => true,
  403. :created_by_id => true,
  404. :updated_by_id => true,
  405. :article_count => true,
  406. :create_article_type_id => true,
  407. :create_article_sender_id => true,
  408. }
  409. changes.each {|key, value|
  410. # do not log created_at and updated_at attributes
  411. next if ignore_attributes[key.to_sym] == true
  412. # get attribute name
  413. attribute_name = key.to_s
  414. if attribute_name[-3,3] == '_id'
  415. attribute_name = attribute_name[ 0, attribute_name.length-3 ]
  416. end
  417. value_id = []
  418. if key.to_s[-3,3] == '_id'
  419. value_id[0] = value[0]
  420. value_id[1] = value[1]
  421. if self.respond_to?( attribute_name )
  422. relation_class = self.send(attribute_name).class
  423. if relation_class
  424. relation_model = relation_class.lookup( :id => value_id[0] )
  425. if relation_model
  426. if relation_model['name']
  427. value[0] = relation_model['name']
  428. elsif relation_model.respond_to?('fullname')
  429. value[0] = relation_model.send('fullname')
  430. end
  431. end
  432. relation_model = relation_class.lookup( :id => value_id[1] )
  433. if relation_model
  434. if relation_model['name']
  435. value[1] = relation_model['name']
  436. elsif relation_model.respond_to?('fullname')
  437. value[1] = relation_model.send('fullname')
  438. end
  439. end
  440. end
  441. end
  442. end
  443. data = {
  444. :history_attribute => attribute_name,
  445. :value_from => value[0],
  446. :value_to => value[1],
  447. :id_from => value_id[0],
  448. :id_to => value_id[1],
  449. }
  450. #puts "HIST NEW " + data.inspect
  451. self.history_log( 'updated', self.updated_by_id, data )
  452. }
  453. end
  454. =begin
  455. delete object history, will be executed automatically
  456. model = Model.find(123)
  457. model.history_destroy
  458. =end
  459. def history_destroy
  460. History.remove( self.class.to_s, self.id )
  461. end
  462. =begin
  463. destory object dependencies, will be executed automatically
  464. =end
  465. def destroy_dependencies
  466. end
  467. end