Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

to_timestamp: handle sub queries

Sebastian Fiedlschuster74c6d6639b52
diff --git a/app/models/journable/historic_active_record_relation.rb b/app/models/journable/historic_active_record_relation.rb
index 1bcec40baa7..e47d58ad7a0 100644
--- a/app/models/journable/historic_active_record_relation.rb
+++ b/app/models/journable/historic_active_record_relation.rb
@@ -102,6 +102,13 @@ class Journable::HistoricActiveRecordRelation < ActiveRecord::Relation
# will be a WorkPackage, we need a work-package id, which we take from the journals table.
if relation.select_values.count == 0
relation = relation.select("'#{timestamp}' as timestamp, #{model.journal_class.table_name}.*, journals.journable_id as id, journables.created_at as created_at, journals.created_at as updated_at")
+ elsif relation.select_values.count == 1
+ # For sub queries, we need to use the journals.journable_id as well.
+ # See https://github.com/fiedl/openproject/issues/3.
+ if relation.select_values.first.respond_to? :relation and relation.select_values.first.relation.name == model.journal_class.table_name and relation.select_values.first.name == "id"
+ relation.instance_variable_get(:@values)[:select] = []
+ relation = relation.select("journals.journable_id as id")
+ end
end
# Based on the previous modifications, build the algebra object.
@@ -131,7 +138,7 @@ class Journable::HistoricActiveRecordRelation < ActiveRecord::Relation
core.instance_variable_get(:@source).right.each do |node|
if node.kind_of?(Arel::Nodes::Join) and node.right.kind_of?(Arel::Nodes::On)
[node.right.expr.left, node.right.expr.right].each do |attribute|
- if (attribute.relation == journal_class.arel_table) and (attribute.name == "id")
+ if attribute.respond_to? :relation and (attribute.relation == journal_class.arel_table) and (attribute.name == "id")
attribute.relation = Journal.arel_table
attribute.name = "journable_id"
end
diff --git a/spec/models/journable/timestamps_spec.rb b/spec/models/journable/timestamps_spec.rb
index 5af8db315fd..b6790ffda8c 100644
--- a/spec/models/journable/timestamps_spec.rb
+++ b/spec/models/journable/timestamps_spec.rb
@@ -391,7 +391,7 @@ describe Journable::Timestamps, type: :model do
sub_query = WorkPackage.at_timestamp(@monday).where(description: "The work package as it has been on Monday")
WorkPackage.where(id: sub_query)
end
- pending "returns the current records that have matched the filter on monday" do
+ it "returns the current records that have matched the filter on monday" do
expect(subject.pluck(:description)).to eq ["The work package as it is since Friday"]
end
describe "when plucking the id as sub query (workaround)" do