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 sql where clause (as used by `Query#statement`)

Sebastian Fiedlschuster6f3bd4706fb0
diff --git a/app/models/journable/historic_active_record_relation.rb b/app/models/journable/historic_active_record_relation.rb
index 85415ec7af2..c26afcb1563 100644
--- a/app/models/journable/historic_active_record_relation.rb
+++ b/app/models/journable/historic_active_record_relation.rb
@@ -75,8 +75,10 @@ class Journable::HistoricActiveRecordRelation < ActiveRecord::Relation
# Modify there where clauses such that the work-packages table is substituted
# with the work-package-journals table.
relation.where_clause.instance_variable_get(:@predicates).each do |predicate|
- if predicate.left.relation == self.arel_table
- predicate.left.relation = self.journal_class.arel_table
+ if predicate.kind_of? String
+ predicate.gsub! "#{model.table_name}.", "#{model.journal_class.table_name}."
+ elsif predicate.left.relation == self.arel_table
+ predicate.left.relation = self.journal_class.arel_table
end
end
diff --git a/spec/models/journable/timestamps_spec.rb b/spec/models/journable/timestamps_spec.rb
index 18b732396dd..9c68c63984a 100644
--- a/spec/models/journable/timestamps_spec.rb
+++ b/spec/models/journable/timestamps_spec.rb
@@ -406,6 +406,18 @@ describe Journable::Timestamps, type: :model do
end
+ describe "when chaining a sql where clause (as used by Query#statement)" do
+ subject { WorkPackage.where("(work_packages.description ILIKE '%been on Wednesday%')").at_timestamp(@wednesday) }
+
+ it "transforms the table name" do
+ expect(subject.to_sql).to include "work_package_journals.description ILIKE"
+ end
+
+ it "returns the requested work package" do
+ expect(subject).to include work_package
+ end
+ end
+
describe "when chaining an order clause" do
subject { WorkPackage.at_timestamp(@wednesday).order(description: :desc) }