2017-01-31 17:13:45 +00:00
|
|
|
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
|
2017-05-02 15:21:13 +00:00
|
|
|
module ApplicationModel::CanLatestChange
|
2017-01-31 17:13:45 +00:00
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
# methods defined here are going to extend the class, not the instance of it
|
|
|
|
class_methods do
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
get latest updated_at object timestamp
|
|
|
|
|
2019-06-27 10:01:40 +00:00
|
|
|
latest_change = object.latest_change
|
2017-01-31 17:13:45 +00:00
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = timestamp
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def latest_change
|
2019-06-27 10:01:40 +00:00
|
|
|
key = "#{name}_latest_change"
|
2021-05-31 13:05:54 +00:00
|
|
|
updated_at = Cache.read(key)
|
2017-01-31 17:13:45 +00:00
|
|
|
|
2018-06-16 10:14:49 +00:00
|
|
|
return updated_at if updated_at
|
|
|
|
|
2017-01-31 17:13:45 +00:00
|
|
|
# if we do not have it cached, do lookup
|
2019-06-27 10:01:40 +00:00
|
|
|
updated_at = order(updated_at: :desc).limit(1).pluck(:updated_at).first
|
2018-06-16 10:14:49 +00:00
|
|
|
|
|
|
|
return if !updated_at
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2018-06-16 10:14:49 +00:00
|
|
|
latest_change_set(updated_at)
|
2017-01-31 17:13:45 +00:00
|
|
|
updated_at
|
|
|
|
end
|
|
|
|
|
|
|
|
def latest_change_set(updated_at)
|
2019-06-27 10:01:40 +00:00
|
|
|
key = "#{name}_latest_change"
|
2017-06-16 22:53:20 +00:00
|
|
|
expires_in = 86_400 # 1 day
|
2017-01-31 17:13:45 +00:00
|
|
|
|
|
|
|
if updated_at.nil?
|
|
|
|
Cache.delete(key)
|
|
|
|
else
|
|
|
|
Cache.write(key, updated_at, { expires_in: expires_in })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|