OpenProject is the leading open source project management software.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
openproject/app/models/application_record.rb

30 lines
859 B

class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
##
# Get the newest recently changed resource for the given record classes
#
# e.g., +most_recently_changed(WorkPackage, Type, Status)+
#
# Returns the timestamp of the most recently updated value
def self.most_recently_changed(*record_classes)
queries = record_classes.map do |clz|
column_name = clz.send(:timestamp_attributes_for_update_in_model)&.first || 'updated_at'
"(SELECT MAX(#{column_name}) AS max_updated_at FROM #{clz.table_name})"
end
.join(" UNION ")
union_query = <<~SQL
SELECT MAX(union_query.max_updated_at)
FROM (#{queries})
AS union_query
SQL
ActiveRecord::Base
.connection
.select_all(union_query)
.rows
&.first # first result row
&.first # max column
end
end