touches_organization.rb 1020 B

123456789101112131415161718192021222324252627282930313233343536
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. # Update assigned organization change_time information on user changes.
  3. module User::TouchesOrganization
  4. extend ActiveSupport::Concern
  5. included do
  6. after_create :touch_user_organization
  7. after_update :touch_user_organization
  8. after_destroy :touch_user_organization
  9. end
  10. private
  11. def touch_user_organization
  12. # return if we run import mode
  13. return true if Setting.get('import_mode')
  14. organization_id_changed = saved_changes['organization_id']
  15. return true if !organization_id_changed
  16. return true if organization_id_changed[0] == organization_id_changed[1]
  17. # touch old organization
  18. if organization_id_changed[0]
  19. old_organization = Organization.find(organization_id_changed[0])
  20. old_organization&.touch # rubocop:disable Rails/SkipsModelValidations
  21. end
  22. # touch new/current organization
  23. organization&.touch # rubocop:disable Rails/SkipsModelValidations
  24. true
  25. end
  26. end