ref_object_touch.rb 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class Observer::Organization::RefObjectTouch < ActiveRecord::Observer
  3. observe 'organization'
  4. def after_create(record)
  5. ref_object_touch(record)
  6. end
  7. def after_update(record)
  8. ref_object_touch(record)
  9. end
  10. def after_destroy(record)
  11. ref_object_touch(record)
  12. end
  13. def ref_object_touch(record)
  14. # return if we run import mode
  15. return true if Setting.get('import_mode')
  16. # featrue used for different propose, do not touch references
  17. return true if User.where(organization_id: record.id).count > 100
  18. # touch organizations tickets
  19. Ticket.select('id').where(organization_id: record.id).pluck(:id).each do |ticket_id|
  20. ticket = Ticket.find(ticket_id)
  21. ticket.with_lock do
  22. ticket.touch # rubocop:disable Rails/SkipsModelValidations
  23. end
  24. end
  25. # touch current members
  26. User.select('id').where(organization_id: record.id).pluck(:id).each do |user_id|
  27. user = User.find(user_id)
  28. user.with_lock do
  29. user.touch # rubocop:disable Rails/SkipsModelValidations
  30. end
  31. end
  32. true
  33. end
  34. end