can_latest_change.rb 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. module ApplicationModel::CanLatestChange
  3. extend ActiveSupport::Concern
  4. # methods defined here are going to extend the class, not the instance of it
  5. class_methods do
  6. =begin
  7. get latest updated_at object timestamp
  8. latest_change = Ticket.latest_change
  9. returns
  10. result = timestamp
  11. =end
  12. def latest_change
  13. key = "#{new.class.name}_latest_change"
  14. updated_at = Cache.get(key)
  15. # if we do not have it cached, do lookup
  16. if !updated_at
  17. o = select(:updated_at).order(updated_at: :desc, id: :desc).limit(1).first
  18. if o
  19. updated_at = o.updated_at
  20. latest_change_set(updated_at)
  21. end
  22. end
  23. updated_at
  24. end
  25. def latest_change_set(updated_at)
  26. key = "#{new.class.name}_latest_change"
  27. expires_in = 86_400 # 1 day
  28. if updated_at.nil?
  29. Cache.delete(key)
  30. else
  31. Cache.write(key, updated_at, { expires_in: expires_in })
  32. end
  33. end
  34. end
  35. end