application_model.rb 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  1. # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
  2. class ApplicationModel < ActiveRecord::Base
  3. include ApplicationModel::Assets
  4. include ApplicationModel::HistoryLogBase
  5. include ApplicationModel::ActivityStreamBase
  6. include ApplicationModel::SearchIndexBase
  7. self.abstract_class = true
  8. before_create :check_attributes_protected, :check_limits, :cache_delete, :fill_up_user_create
  9. before_update :check_limits, :fill_up_user_update
  10. before_destroy :destroy_dependencies
  11. after_create :cache_delete
  12. after_update :cache_delete
  13. after_touch :cache_delete
  14. after_destroy :cache_delete
  15. after_create :attachments_buffer_check
  16. after_update :attachments_buffer_check
  17. after_create :activity_stream_create
  18. after_update :activity_stream_update
  19. before_destroy :activity_stream_destroy
  20. after_create :history_create
  21. after_update :history_update
  22. after_destroy :history_destroy
  23. after_create :search_index_update
  24. after_update :search_index_update
  25. after_destroy :search_index_destroy
  26. before_destroy :recent_view_destroy
  27. # create instance accessor
  28. class << self
  29. attr_accessor :activity_stream_support_config, :history_support_config, :search_index_support_config
  30. end
  31. attr_accessor :history_changes_last_done
  32. @@import_class_list = ['Ticket', 'Ticket::Article', 'History', 'Ticket::State', 'Ticket::StateType', 'Ticket::Priority', 'Group', 'User', 'Role' ]
  33. def check_attributes_protected
  34. if !Setting.get('system_init_done') || ( Setting.get('import_mode') && @@import_class_list.include?( self.class.to_s ) )
  35. # do noting, use id as it is
  36. else
  37. self[:id] = nil
  38. end
  39. end
  40. =begin
  41. remove all not used model attributes of params
  42. result = Model.param_cleanup(params)
  43. for object creation, ignore id's
  44. result = Model.param_cleanup(params, true)
  45. returns
  46. result = params # params with valid attributes of model
  47. =end
  48. def self.param_cleanup(params, newObject = false)
  49. if params == nil
  50. raise "No params for #{self.to_s}!"
  51. end
  52. # ignore id for new objects
  53. if newObject && params[:id]
  54. params[:id] = nil
  55. end
  56. # only use object attributes
  57. data = {}
  58. self.new.attributes.each {|item|
  59. if params.has_key?(item[0])
  60. # puts 'use ' + item[0].to_s + '-' + params[item[0]].to_s
  61. data[item[0].to_sym] = params[item[0]]
  62. end
  63. }
  64. # we do want to set this via database
  65. self.param_validation(data)
  66. end
  67. =begin
  68. set rellations of model based on params
  69. model = Model.find(1)
  70. result = model.param_set_associations(params)
  71. returns
  72. result = true|false
  73. =end
  74. def param_set_associations(params)
  75. # set relations
  76. self.class.reflect_on_all_associations.map { |assoc|
  77. real_key = assoc.name.to_s[0, assoc.name.to_s.length - 1] + '_ids'
  78. if params.has_key?( real_key.to_sym )
  79. list_of_items = params[ real_key.to_sym ]
  80. if params[ real_key.to_sym ].class != Array
  81. list_of_items = [ params[ real_key.to_sym ] ]
  82. end
  83. list = []
  84. list_of_items.each {|item|
  85. list.push( assoc.klass.find(item) )
  86. }
  87. self.send( assoc.name.to_s + '=', list )
  88. end
  89. }
  90. end
  91. =begin
  92. get rellations of model based on params
  93. model = Model.find(1)
  94. attributes = model.attributes_with_associations
  95. returns
  96. hash with attributes and association ids
  97. =end
  98. def attributes_with_associations
  99. # set relations
  100. attributes = self.attributes
  101. self.class.reflect_on_all_associations.map { |assoc|
  102. real_key = assoc.name.to_s[0, assoc.name.to_s.length - 1] + '_ids'
  103. if self.respond_to?( real_key )
  104. attributes[ real_key ] = self.send( real_key )
  105. end
  106. }
  107. attributes
  108. end
  109. =begin
  110. remove all not used params of object (per default :updated_at, :created_at, :updated_by_id and :created_by_id)
  111. result = Model.param_validation(params)
  112. returns
  113. result = params # params without listed attributes
  114. =end
  115. def self.param_validation(data)
  116. # we do want to set this via database
  117. data.delete( :updated_at )
  118. data.delete( :created_at )
  119. data.delete( :updated_by_id )
  120. data.delete( :created_by_id )
  121. if data.respond_to?('permit!')
  122. data.permit!
  123. end
  124. data
  125. end
  126. =begin
  127. set created_by_id & updated_by_id if not given based on UserInfo (current session)
  128. Used as before_create callback, no own use needed
  129. result = Model.fill_up_user_create(params)
  130. returns
  131. result = params # params with updated_by_id & created_by_id if not given based on UserInfo (current session)
  132. =end
  133. def fill_up_user_create
  134. if self.class.column_names.include? 'updated_by_id'
  135. if UserInfo.current_user_id
  136. if self.updated_by_id && self.updated_by_id != UserInfo.current_user_id
  137. logger.info "NOTICE create - self.updated_by_id is different: #{self.updated_by_id.to_s}/#{UserInfo.current_user_id.to_s}"
  138. end
  139. self.updated_by_id = UserInfo.current_user_id
  140. end
  141. end
  142. if self.class.column_names.include? 'created_by_id'
  143. if UserInfo.current_user_id
  144. if self.created_by_id && self.created_by_id != UserInfo.current_user_id
  145. logger.info "NOTICE create - self.created_by_id is different: #{self.created_by_id.to_s}/#{UserInfo.current_user_id.to_s}"
  146. end
  147. self.created_by_id = UserInfo.current_user_id
  148. end
  149. end
  150. end
  151. =begin
  152. set updated_by_id if not given based on UserInfo (current session)
  153. Used as before_update callback, no own use needed
  154. result = Model.fill_up_user_update(params)
  155. returns
  156. result = params # params with updated_by_id & created_by_id if not given based on UserInfo (current session)
  157. =end
  158. def fill_up_user_update
  159. return if !self.class.column_names.include? 'updated_by_id'
  160. if UserInfo.current_user_id
  161. self.updated_by_id = UserInfo.current_user_id
  162. end
  163. end
  164. def cache_update(o)
  165. # puts 'u ' + self.class.to_s
  166. if self.respond_to?('cache_delete') then self.cache_delete end
  167. # puts 'g ' + group.class.to_s
  168. if o.respond_to?('cache_delete') then o.cache_delete end
  169. end
  170. def cache_delete
  171. # delete id caches
  172. key = self.class.to_s + '::' + self.id.to_s
  173. Cache.delete( key.to_s )
  174. key = self.class.to_s + ':f:' + self.id.to_s
  175. Cache.delete( key.to_s )
  176. # delete old name / login caches
  177. if self.changed?
  178. if self.changes.has_key?('name')
  179. name = self.changes['name'][0].to_s
  180. key = self.class.to_s + '::' + name
  181. Cache.delete( key.to_s )
  182. key = self.class.to_s + ':f:' + name
  183. Cache.delete( key.to_s )
  184. end
  185. if self.changes.has_key?('login')
  186. name = self.changes['login'][0].to_s
  187. key = self.class.to_s + '::' + name
  188. Cache.delete( key.to_s )
  189. key = self.class.to_s + ':f:' + name
  190. Cache.delete( key.to_s )
  191. end
  192. end
  193. # delete name / login caches
  194. if self[:name]
  195. key = self.class.to_s + '::' + self.name.to_s
  196. Cache.delete( key.to_s )
  197. key = self.class.to_s + ':f:' + self.name.to_s
  198. Cache.delete( key.to_s )
  199. end
  200. if self[:login]
  201. key = self.class.to_s + '::' + self.login.to_s
  202. Cache.delete( key.to_s )
  203. key = self.class.to_s + ':f:' + self.login.to_s
  204. Cache.delete( key.to_s )
  205. end
  206. end
  207. def self.cache_set(data_id, data, full = false)
  208. if !full
  209. key = self.to_s + '::' + data_id.to_s
  210. else
  211. key = self.to_s + ':f:' + data_id.to_s
  212. end
  213. Cache.write( key.to_s, data )
  214. end
  215. def self.cache_get(data_id, full = false)
  216. if !full
  217. key = self.to_s + '::' + data_id.to_s
  218. else
  219. key = self.to_s + ':f:' + data_id.to_s
  220. end
  221. Cache.get( key.to_s )
  222. end
  223. =begin
  224. lookup model from cache (if exists) or retrieve it from db, id, name or login possible
  225. result = Model.lookup( :id => 123 )
  226. result = Model.lookup( :name => 'some name' )
  227. result = Model.lookup( :login => 'some login' )
  228. returns
  229. result = model # with all attributes
  230. =end
  231. def self.lookup(data)
  232. if data[:id]
  233. # puts "GET- + #{self.to_s}.#{data[:id].to_s}"
  234. cache = self.cache_get( data[:id] )
  235. return cache if cache
  236. # puts "Fillup- + #{self.to_s}.#{data[:id].to_s}"
  237. record = self.where( id: data[:id] ).first
  238. self.cache_set( data[:id], record )
  239. return record
  240. elsif data[:name]
  241. cache = self.cache_get( data[:name] )
  242. return cache if cache
  243. records = self.where( name: data[:name] )
  244. records.each {|record|
  245. if record.name == data[:name]
  246. self.cache_set( data[:name], record )
  247. return record
  248. end
  249. }
  250. return
  251. elsif data[:login]
  252. cache = self.cache_get( data[:login] )
  253. return cache if cache
  254. records = self.where( login: data[:login] )
  255. records.each {|record|
  256. if record.login == data[:login]
  257. self.cache_set( data[:login], record )
  258. return record
  259. end
  260. }
  261. return
  262. else
  263. raise 'Need name, id or login for lookup()'
  264. end
  265. end
  266. =begin
  267. create model if not exists (check exists based on id, name, login or locale)
  268. result = Model.create_if_not_exists( attributes )
  269. returns
  270. result = model # with all attributes
  271. =end
  272. def self.create_if_not_exists(data)
  273. if data[:id]
  274. record = self.where( id: data[:id] ).first
  275. return record if record
  276. elsif data[:name]
  277. records = self.where( name: data[:name] )
  278. records.each {|record|
  279. return record if record.name == data[:name]
  280. }
  281. elsif data[:login]
  282. records = self.where( login: data[:login] )
  283. records.each {|record|
  284. return record if record.login == data[:login]
  285. }
  286. elsif data[:locale] && data[:source]
  287. records = self.where( locale: data[:locale], source: data[:source] )
  288. records.each {|record|
  289. return record if record.source == data[:source]
  290. }
  291. end
  292. self.create(data)
  293. end
  294. =begin
  295. create or update model (check exists based on id, name, login or locale)
  296. result = Model.create_or_update( attributes )
  297. returns
  298. result = model # with all attributes
  299. =end
  300. def self.create_or_update(data)
  301. if data[:id]
  302. records = self.where( id: data[:id] )
  303. records.each {|record|
  304. record.update_attributes( data )
  305. return record
  306. }
  307. record = self.new( data )
  308. record.save
  309. return record
  310. elsif data[:name]
  311. records = self.where( name: data[:name] )
  312. records.each {|record|
  313. if record.name == data[:name]
  314. record.update_attributes( data )
  315. return record
  316. end
  317. }
  318. record = self.new( data )
  319. record.save
  320. return record
  321. elsif data[:login]
  322. records = self.where( login: data[:login] )
  323. records.each {|record|
  324. if record.login.downcase == data[:login].downcase
  325. record.update_attributes( data )
  326. return record
  327. end
  328. }
  329. record = self.new( data )
  330. record.save
  331. return record
  332. elsif data[:locale]
  333. records = self.where( locale: data[:locale] )
  334. records.each {|record|
  335. if record.locale.downcase == data[:locale].downcase
  336. record.update_attributes( data )
  337. return record
  338. end
  339. }
  340. record = self.new( data )
  341. record.save
  342. return record
  343. else
  344. raise 'Need name, login or locale for create_or_update()'
  345. end
  346. end
  347. =begin
  348. activate latest change on create, update, touch and destroy
  349. class Model < ApplicationModel
  350. latest_change_support
  351. end
  352. =end
  353. def self.latest_change_support
  354. after_create :latest_change_set_from_observer
  355. after_update :latest_change_set_from_observer
  356. after_touch :latest_change_set_from_observer
  357. after_destroy :latest_change_set_from_observer_destroy
  358. end
  359. def latest_change_set_from_observer
  360. self.class.latest_change_set(self.updated_at)
  361. end
  362. def latest_change_set_from_observer_destroy
  363. self.class.latest_change_set(nil)
  364. end
  365. def self.latest_change_set(updated_at)
  366. key = "#{self.new.class.name}_latest_change"
  367. expires_in = 31_536_000 # 1 year
  368. if updated_at == nil
  369. Cache.delete( key )
  370. else
  371. Cache.write( key, updated_at, { expires_in: expires_in } )
  372. end
  373. end
  374. =begin
  375. get latest updated_at object timestamp
  376. latest_change = Ticket.latest_change
  377. returns
  378. result = timestamp
  379. =end
  380. def self.latest_change
  381. key = "#{self.new.class.name}_latest_change"
  382. updated_at = Cache.get( key )
  383. # if we do not have it cached, do lookup
  384. if !updated_at
  385. o = self.select(:updated_at).order(updated_at: :desc).limit(1).first
  386. if o
  387. updated_at = o.updated_at
  388. self.latest_change_set(updated_at)
  389. end
  390. end
  391. updated_at
  392. end
  393. =begin
  394. activate client notify support on create, update, touch and destroy
  395. class Model < ApplicationModel
  396. notify_clients_support
  397. end
  398. =end
  399. def self.notify_clients_support
  400. after_create :notify_clients_after_create
  401. after_update :notify_clients_after_update
  402. # after_touch :notify_clients_after_touch
  403. after_touch :notify_clients_after_update
  404. after_destroy :notify_clients_after_destroy
  405. end
  406. =begin
  407. notify_clients_after_create after model got created
  408. used as callback in model file
  409. class OwnModel < ApplicationModel
  410. after_create :notify_clients_after_create
  411. after_update :notify_clients_after_update
  412. after_touch :notify_clients_after_touch
  413. after_destroy :notify_clients_after_destroy
  414. [...]
  415. =end
  416. def notify_clients_after_create
  417. # return if we run import mode
  418. return if Setting.get('import_mode')
  419. logger.debug "#{ self.class.name }.find(#{ self.id }) notify created " + self.created_at.to_s
  420. class_name = self.class.name
  421. class_name.gsub!(/::/, '')
  422. Sessions.broadcast(
  423. event: class_name + ':create',
  424. data: { id: self.id, updated_at: self.updated_at }
  425. )
  426. end
  427. =begin
  428. notify_clients_after_update after model got updated
  429. used as callback in model file
  430. class OwnModel < ApplicationModel
  431. after_create :notify_clients_after_create
  432. after_update :notify_clients_after_update
  433. after_touch :notify_clients_after_touch
  434. after_destroy :notify_clients_after_destroy
  435. [...]
  436. =end
  437. def notify_clients_after_update
  438. # return if we run import mode
  439. return if Setting.get('import_mode')
  440. logger.debug "#{ self.class.name }.find(#{ self.id }) notify UPDATED " + self.updated_at.to_s
  441. class_name = self.class.name
  442. class_name.gsub!(/::/, '')
  443. Sessions.broadcast(
  444. event: class_name + ':update',
  445. data: { id: self.id, updated_at: self.updated_at }
  446. )
  447. end
  448. =begin
  449. notify_clients_after_touch after model got touched
  450. used as callback in model file
  451. class OwnModel < ApplicationModel
  452. after_create :notify_clients_after_create
  453. after_update :notify_clients_after_update
  454. after_touch :notify_clients_after_touch
  455. after_destroy :notify_clients_after_destroy
  456. [...]
  457. =end
  458. def notify_clients_after_touch
  459. # return if we run import mode
  460. return if Setting.get('import_mode')
  461. logger.debug "#{ self.class.name }.find(#{ self.id }) notify TOUCH " + self.updated_at.to_s
  462. class_name = self.class.name
  463. class_name.gsub!(/::/, '')
  464. Sessions.broadcast(
  465. :event => class_name + ':touch',
  466. :data => { :id => self.id, :updated_at => self.updated_at }
  467. )
  468. end
  469. =begin
  470. notify_clients_after_destroy after model got destroyed
  471. used as callback in model file
  472. class OwnModel < ApplicationModel
  473. after_create :notify_clients_after_create
  474. after_update :notify_clients_after_update
  475. after_touch :notify_clients_after_touch
  476. after_destroy :notify_clients_after_destroy
  477. [...]
  478. =end
  479. def notify_clients_after_destroy
  480. # return if we run import mode
  481. return if Setting.get('import_mode')
  482. logger.debug "#{ self.class.name }.find(#{ self.id }) notify DESTOY " + self.updated_at.to_s
  483. class_name = self.class.name
  484. class_name.gsub!(/::/, '')
  485. Sessions.broadcast(
  486. event: class_name + ':destroy',
  487. data: { id: self.id, updated_at: self.updated_at }
  488. )
  489. end
  490. =begin
  491. serve methode to configure and enable search index support for this model
  492. class Model < ApplicationModel
  493. search_index_support :ignore_attributes => {
  494. :create_article_type_id => true,
  495. :create_article_sender_id => true,
  496. :article_count => true,
  497. }
  498. end
  499. =end
  500. def self.search_index_support(data = {})
  501. @search_index_support_config = data
  502. end
  503. =begin
  504. update search index, if configured - will be executed automatically
  505. model = Model.find(123)
  506. model.search_index_update
  507. =end
  508. def search_index_update
  509. return if !self.class.search_index_support_config
  510. # start background job to transfer data to search index
  511. return if !SearchIndexBackend.enabled?
  512. Delayed::Job.enqueue( ApplicationModel::BackgroundJobSearchIndex.new( self.class.to_s, self.id ) )
  513. end
  514. =begin
  515. delete search index object, will be executed automatically
  516. model = Model.find(123)
  517. model.search_index_destroy
  518. =end
  519. def search_index_destroy
  520. return if !self.class.search_index_support_config
  521. SearchIndexBackend.remove( self.class.to_s, self.id )
  522. end
  523. =begin
  524. reload search index with full data
  525. Model.search_index_reload
  526. =end
  527. def self.search_index_reload
  528. return if !@search_index_support_config
  529. all_ids = self.select('id').all.order('created_at DESC')
  530. all_ids.each { |item_with_id|
  531. item = self.find( item_with_id.id )
  532. item.search_index_update_backend
  533. }
  534. end
  535. =begin
  536. serve methode to configure and enable activity stream support for this model
  537. class Model < ApplicationModel
  538. activity_stream_support :role => 'Admin'
  539. end
  540. =end
  541. def self.activity_stream_support(data = {})
  542. @activity_stream_support_config = data
  543. end
  544. =begin
  545. log object create activity stream, if configured - will be executed automatically
  546. model = Model.find(123)
  547. model.activity_stream_create
  548. =end
  549. def activity_stream_create
  550. return if !self.class.activity_stream_support_config
  551. activity_stream_log( 'created', self['created_by_id'] )
  552. end
  553. =begin
  554. log object update activity stream, if configured - will be executed automatically
  555. model = Model.find(123)
  556. model.activity_stream_update
  557. =end
  558. def activity_stream_update
  559. return if !self.class.activity_stream_support_config
  560. return if !self.changed?
  561. # default ignored attributes
  562. ignore_attributes = {
  563. created_at: true,
  564. updated_at: true,
  565. created_by_id: true,
  566. updated_by_id: true,
  567. }
  568. if self.class.activity_stream_support_config[:ignore_attributes]
  569. self.class.activity_stream_support_config[:ignore_attributes].each {|key, value|
  570. ignore_attributes[key] = value
  571. }
  572. end
  573. log = false
  574. self.changes.each {|key, value|
  575. # do not log created_at and updated_at attributes
  576. next if ignore_attributes[key.to_sym] == true
  577. log = true
  578. }
  579. return if !log
  580. activity_stream_log( 'updated', self['updated_by_id'] )
  581. end
  582. =begin
  583. delete object activity stream, will be executed automatically
  584. model = Model.find(123)
  585. model.activity_stream_destroy
  586. =end
  587. def activity_stream_destroy
  588. return if !self.class.activity_stream_support_config
  589. ActivityStream.remove( self.class.to_s, self.id )
  590. end
  591. =begin
  592. serve methode to configure and enable history support for this model
  593. class Model < ApplicationModel
  594. history_support
  595. end
  596. class Model < ApplicationModel
  597. history_support :ignore_attributes => { :article_count => true }
  598. end
  599. =end
  600. def self.history_support(data = {})
  601. @history_support_config = data
  602. end
  603. =begin
  604. log object create history, if configured - will be executed automatically
  605. model = Model.find(123)
  606. model.history_create
  607. =end
  608. def history_create
  609. return if !self.class.history_support_config
  610. #puts 'create ' + self.changes.inspect
  611. self.history_log( 'created', self.created_by_id )
  612. end
  613. =begin
  614. log object update history with all updated attributes, if configured - will be executed automatically
  615. model = Model.find(123)
  616. model.history_update
  617. =end
  618. def history_update
  619. return if !self.class.history_support_config
  620. return if !self.changed?
  621. # return if it's no update
  622. return if self.new_record?
  623. # new record also triggers update, so ignore new records
  624. changes = self.changes
  625. if self.history_changes_last_done
  626. self.history_changes_last_done.each {|key, value|
  627. if changes.has_key?(key) && changes[key] == value
  628. changes.delete(key)
  629. end
  630. }
  631. end
  632. self.history_changes_last_done = changes
  633. #puts 'updated ' + self.changes.inspect
  634. return if changes['id'] && !changes['id'][0]
  635. # default ignored attributes
  636. ignore_attributes = {
  637. created_at: true,
  638. updated_at: true,
  639. created_by_id: true,
  640. updated_by_id: true,
  641. }
  642. if self.class.history_support_config[:ignore_attributes]
  643. self.class.history_support_config[:ignore_attributes].each {|key, value|
  644. ignore_attributes[key] = value
  645. }
  646. end
  647. changes.each {|key, value|
  648. # do not log created_at and updated_at attributes
  649. next if ignore_attributes[key.to_sym] == true
  650. # get attribute name
  651. attribute_name = key.to_s
  652. if attribute_name[-3, 3] == '_id'
  653. attribute_name = attribute_name[ 0, attribute_name.length - 3 ]
  654. end
  655. value_id = []
  656. value_str = [ value[0], value[1] ]
  657. if key.to_s[-3, 3] == '_id'
  658. value_id[0] = value[0]
  659. value_id[1] = value[1]
  660. if self.respond_to?( attribute_name ) && self.send(attribute_name)
  661. relation_class = self.send(attribute_name).class
  662. if relation_class && value_id[0]
  663. relation_model = relation_class.lookup( id: value_id[0] )
  664. if relation_model
  665. if relation_model['name']
  666. value_str[0] = relation_model['name']
  667. elsif relation_model.respond_to?('fullname')
  668. value_str[0] = relation_model.send('fullname')
  669. end
  670. end
  671. end
  672. if relation_class && value_id[1]
  673. relation_model = relation_class.lookup( id: value_id[1] )
  674. if relation_model
  675. if relation_model['name']
  676. value_str[1] = relation_model['name']
  677. elsif relation_model.respond_to?('fullname')
  678. value_str[1] = relation_model.send('fullname')
  679. end
  680. end
  681. end
  682. end
  683. end
  684. data = {
  685. history_attribute: attribute_name,
  686. value_from: value_str[0].to_s,
  687. value_to: value_str[1].to_s,
  688. id_from: value_id[0],
  689. id_to: value_id[1],
  690. }
  691. #puts "HIST NEW #{self.class.to_s}.find(#{self.id}) #{data.inspect}"
  692. self.history_log( 'updated', self.updated_by_id, data )
  693. }
  694. end
  695. =begin
  696. delete object history, will be executed automatically
  697. model = Model.find(123)
  698. model.history_destroy
  699. =end
  700. def history_destroy
  701. return if !self.class.history_support_config
  702. History.remove( self.class.to_s, self.id )
  703. end
  704. =begin
  705. get list of attachments of this object
  706. item = Model.find(123)
  707. list = item.attachments
  708. returns
  709. # array with Store model objects
  710. =end
  711. def attachments
  712. Store.list( object: self.class.to_s, o_id: self.id )
  713. end
  714. =begin
  715. store attachments for this object
  716. item = Model.find(123)
  717. item.attachments = [ Store-Object1, Store-Object2 ]
  718. =end
  719. def attachments=(attachments)
  720. self.attachments_buffer = attachments
  721. # update if object already exists
  722. if self.id && self.id != 0
  723. attachments_buffer_check
  724. end
  725. end
  726. =begin
  727. return object and assets
  728. data = Model.full(123)
  729. data = {
  730. :id => 123,
  731. :assets => assets,
  732. }
  733. =end
  734. def self.full(id)
  735. object = self.find(id)
  736. assets = object.assets({})
  737. {
  738. id: id,
  739. assets: assets,
  740. }
  741. end
  742. =begin
  743. get assets of object list
  744. list = [
  745. {
  746. object => 'Ticket',
  747. o_id => 1,
  748. },
  749. {
  750. object => 'User',
  751. o_id => 121,
  752. },
  753. ]
  754. assets = Model.assets_of_object_list(list, assets)
  755. =end
  756. def self.assets_of_object_list(list, assets = {})
  757. list.each {|item|
  758. require item['object'].to_filename
  759. record = Kernel.const_get( item['object'] ).find( item['o_id'] )
  760. assets = record.assets(assets)
  761. if item['created_by_id']
  762. user = User.find( item['created_by_id'] )
  763. assets = user.assets(assets)
  764. end
  765. if item['updated_by_id']
  766. user = User.find( item['updated_by_id'] )
  767. assets = user.assets(assets)
  768. end
  769. }
  770. assets
  771. end
  772. private
  773. def attachments_buffer
  774. @attachments_buffer_data
  775. end
  776. def attachments_buffer=(attachments)
  777. @attachments_buffer_data = attachments
  778. end
  779. def attachments_buffer_check
  780. # do nothing if no attachment exists
  781. return 1 if attachments_buffer == nil
  782. # store attachments
  783. article_store = []
  784. attachments_buffer.each do |attachment|
  785. article_store.push Store.add(
  786. object: self.class.to_s,
  787. o_id: self.id,
  788. data: attachment.content,
  789. filename: attachment.filename,
  790. preferences: attachment.preferences,
  791. created_by_id: self.created_by_id,
  792. )
  793. end
  794. attachments_buffer = nil
  795. end
  796. =begin
  797. delete object recent viewed list, will be executed automatically
  798. model = Model.find(123)
  799. model.recent_view_destroy
  800. =end
  801. def recent_view_destroy
  802. RecentView.log_destroy( self.class.to_s, self.id )
  803. end
  804. =begin
  805. check string/varchar size and cut them if needed
  806. =end
  807. def check_limits
  808. self.attributes.each {|attribute|
  809. next if !self[ attribute[0] ]
  810. next if self[ attribute[0] ].class != String
  811. next if self[ attribute[0] ].empty?
  812. column = self.class.columns_hash[ attribute[0] ]
  813. limit = column.limit
  814. if column && limit
  815. current_length = attribute[1].to_s.length
  816. if limit < current_length
  817. logger.info "WARNING: cut string because of database length #{self.class.to_s}.#{attribute[0]}(#{limit} but is #{current_length}:#{attribute[1].to_s})"
  818. self[ attribute[0] ] = attribute[1][ 0, limit ]
  819. end
  820. end
  821. # strip 4 bytes utf8 chars if needed
  822. if column && self[ attribute[0] ]
  823. self[attribute[0]] = self[ attribute[0] ].utf8_to_3bytesutf8
  824. end
  825. }
  826. end
  827. =begin
  828. destory object dependencies, will be executed automatically
  829. =end
  830. def destroy_dependencies
  831. end
  832. end