trabajo-afectivo/app/models/application_model/can_latest_change.rb
Thorsten Eckel ca56de3648 Maintenance: Updated to Rails 6.0.4 and the new Zeitwerk autoloader.
This changes the minimum supported version of PostgreSQL to 9.3.
2021-06-23 11:35:27 +00:00

47 lines
997 B
Ruby

# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
module ApplicationModel::CanLatestChange
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 = object.latest_change
returns
result = timestamp
=end
def latest_change
key = "#{name}_latest_change"
updated_at = Cache.read(key)
return updated_at if updated_at
# if we do not have it cached, do lookup
updated_at = order(updated_at: :desc).limit(1).pick(:updated_at)
return if !updated_at
latest_change_set(updated_at)
updated_at
end
def latest_change_set(updated_at)
key = "#{name}_latest_change"
expires_in = 86_400 # 1 day
if updated_at.nil?
Cache.delete(key)
else
Cache.write(key, updated_at, { expires_in: expires_in })
end
end
end
end