application_model.rb 23 KB

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