Skip to content

Commit

Permalink
Undo unneeded fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
klaustopher committed Jun 17, 2024
1 parent 46c7479 commit 13ad94a
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions app/models/projects/activity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ def self.included(base)

module ActivityScopes
def latest_project_activity
@latest_project_activity ||= OpenProject::ProjectLatestActivity.registered.map do |params|
build_latest_project_activity_for(on: params[:on].constantize,
chain: Array(params[:chain]).map(&:constantize),
attribute: params[:attribute],
project_id_attribute: params[:project_id_attribute])
end
@latest_project_activity ||=
OpenProject::ProjectLatestActivity.registered.map do |params|
build_latest_project_activity_for(on: params[:on].constantize,
chain: Array(params[:chain]).map(&:constantize),
attribute: params[:attribute],
project_id_attribute: params[:project_id_attribute])
end
end

def with_latest_activity
Expand All @@ -49,7 +50,7 @@ def with_latest_activity
end

def latest_activity_sql
<<-SQL.squish
<<-SQL
SELECT project_id, MAX(updated_at) latest_activity_at
FROM (#{all_activity_provider_union_sql}) activity
GROUP BY project_id
Expand All @@ -66,7 +67,7 @@ def build_latest_project_activity_for(on:, chain:, attribute:, project_id_attrib

joins = build_joins_from_chain(join_chain)

<<-SQL.squish
<<-SQL
SELECT #{project_id_attribute} project_id, MAX(#{on.table_name}.#{attribute}) updated_at
FROM #{from.table_name}
#{joins.join(' ')}
Expand All @@ -76,17 +77,21 @@ def build_latest_project_activity_for(on:, chain:, attribute:, project_id_attrib
end

def build_joins_from_chain(join_chain)
(0..join_chain.length - 2).map do |i|
build_join(join_chain[i + 1],
join_chain[i])
joins = []

(0..join_chain.length - 2).each do |i|
joins << build_join(join_chain[i + 1],
join_chain[i])
end

joins
end

def build_join(right, left)
associations = right.reflect_on_all_associations
association = associations.detect { |a| a.class_name == left.to_s }

<<-SQL.squish
<<-SQL
LEFT OUTER JOIN #{right.table_name}
ON #{left.table_name}.id =
#{right.table_name}.#{association.foreign_key}
Expand Down

0 comments on commit 13ad94a

Please sign in to comment.