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

at_timestamp: handle case where "work_packages.id IN sub query"

Sebastian Fiedlschuster47c45b6a9f8a
diff --git a/app/models/journable/historic_active_record_relation.rb b/app/models/journable/historic_active_record_relation.rb
index 64b6a891bb8..6752d19865a 100644
--- a/app/models/journable/historic_active_record_relation.rb
+++ b/app/models/journable/historic_active_record_relation.rb
@@ -140,22 +140,16 @@ class Journable::HistoricActiveRecordRelation < ActiveRecord::Relation
Arel::Nodes::LessThanOrEqual,
Arel::Nodes::GreaterThan,
Arel::Nodes::GreaterThanOrEqual
- if predicate.left.relation == arel_table
- if predicate.right.respond_to? :name
- case predicate.right.name
- when "id"
- predicate.right.instance_variable_set(:@name, "journable_id")
- predicate.left.name = "journable_id"
- predicate.left.relation = Journal.arel_table
- when "updated_at"
- predicate.right.instance_variable_set(:@name, "created_at")
- predicate.left.name = "created_at"
- predicate.left.relation = Journal.arel_table
- when "created_at"
- predicate.left = Arel::Nodes::SqlLiteral.new("\"journables\".\"created_at\"")
- else
- predicate.left.relation = journal_class.arel_table
- end
+ if predicate.left.relation == arel_table or predicate.left.relation == journal_class.arel_table
+ case predicate.left.name
+ when "id"
+ predicate.left.name = "journable_id"
+ predicate.left.relation = Journal.arel_table
+ when "updated_at"
+ predicate.left.name = "created_at"
+ predicate.left.relation = Journal.arel_table
+ when "created_at"
+ predicate.left = Arel::Nodes::SqlLiteral.new("\"journables\".\"created_at\"")
else
predicate.left.relation = journal_class.arel_table
end
diff --git a/spec/models/journable/historic_active_record_relation_spec.rb b/spec/models/journable/historic_active_record_relation_spec.rb
index 5c735065733..44f0696c30c 100644
--- a/spec/models/journable/historic_active_record_relation_spec.rb
+++ b/spec/models/journable/historic_active_record_relation_spec.rb
@@ -115,6 +115,38 @@ describe Journable::HistoricActiveRecordRelation do
end
end
+ describe "id in array (Arel::Nodes::HomogeneousIn)" do
+ let(:relation) { WorkPackage.where(id: [work_package.id, 999, 9999]) }
+
+ describe "#to_sql" do
+ it "transforms the expression to query the correct table" do
+ expect(subject.to_sql).to include "\"journals\".\"journable_id\" IN (#{work_package.id}, 999, 9999)"
+ end
+ end
+
+ describe "#to_a" do
+ it "returns the requested work package" do
+ expect(subject.to_a).to include work_package
+ end
+ end
+ end
+
+ describe "id in subquery (Arel::Nodes::In)" do
+ let(:relation) { WorkPackage.where(id: WorkPackage.where(id: [work_package.id, 999, 9999])) }
+
+ describe "#to_sql" do
+ it "transforms the expression to query the correct table" do
+ expect(subject.to_sql).to include "\"journals\".\"journable_id\" IN (SELECT \"work_packages\".\"id\""
+ end
+ end
+
+ describe "#to_a" do
+ it "returns the requested work package" do
+ expect(subject.to_a).to include work_package
+ end
+ end
+ end
+
describe "sql string (as used by Query#statement)" do
let(:relation) { WorkPackage.where("(work_packages.description ILIKE '%been on Wednesday%')") }