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
|
|
|
|
|
|
|
|
latest_change = Ticket.latest_change
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = timestamp
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def latest_change
|
|
|
|
key = "#{new.class.name}_latest_change"
|
|
|
|
updated_at = Cache.get(key)
|
|
|
|
|
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
|
2018-06-16 10:14:49 +00:00
|
|
|
updated_at = order(updated_at: :desc, id: :desc).limit(1).pluck(:updated_at).first
|
|
|
|
|
|
|
|
return if !updated_at
|
|
|
|
latest_change_set(updated_at)
|
2017-01-31 17:13:45 +00:00
|
|
|
updated_at
|
|
|
|
end
|
|
|
|
|
|
|
|
def latest_change_set(updated_at)
|
|
|
|
key = "#{new.class.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
|