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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
at_timestamp: handle `Arel::Nodes::Grouping` (or clauses)
Sebastian Fiedlschuster636e1956853c
diff --git a/app/models/journable/historic_active_record_relation.rb b/app/models/journable/historic_active_record_relation.rbindex e16b01430ed..e78dc3b7705 100644--- a/app/models/journable/historic_active_record_relation.rb+++ b/app/models/journable/historic_active_record_relation.rb@@ -122,9 +122,20 @@ class Journable::HistoricActiveRecordRelation < ActiveRecord::Relation#def substitute_database_table_in_where_clause(relation)relation.where_clause.instance_variable_get(:@predicates).each do |predicate|- if predicate.kind_of? String- gsub_table_names_in_sql_string!(predicate)- elsif predicate.left.relation == arel_table+ substitute_database_table_in_predicate(predicate)+ end+ relation+ end++ def substitute_database_table_in_predicate(predicate)+ case predicate+ when String+ gsub_table_names_in_sql_string!(predicate)+ when Arel::Nodes::In,+ Arel::Nodes::NotIn,+ Arel::Nodes::Equality,+ Arel::Nodes::NotEqual+ if predicate.left.relation == arel_tableif predicate.right.respond_to? :name and predicate.right.name == "id"predicate.right.instance_variable_set(:@name, "journable_id")predicate.left.relation = Journal.arel_table@@ -132,8 +143,12 @@ class Journable::HistoricActiveRecordRelation < ActiveRecord::Relationpredicate.left.relation = journal_class.arel_tableendend+ when Arel::Nodes::Grouping+ substitute_database_table_in_predicate(predicate.expr.left)+ substitute_database_table_in_predicate(predicate.expr.right)+ else+ raise NotImplementedError, "FIXME A predicate of type #{predicate.class.name} is not handled, yet."end- relationend# Add a timestamp condition: Select the work package journals that are the@@ -251,6 +266,8 @@ class Journable::HistoricActiveRecordRelation < ActiveRecord::Relationsql_string.gsub! /(?<!_)#{model.table_name}\./, "#{model.journal_class.table_name}."sql_string.gsub! "\"#{model.table_name}\".", "\"#{model.journal_class.table_name}\"."end++ class NotImplementedError < StandardError; endend# rubocop:enable Style/ClassCheckdiff --git a/spec/models/journable/timestamps_spec.rb b/spec/models/journable/timestamps_spec.rbindex 67c3332a957..14c6addfbde 100644--- a/spec/models/journable/timestamps_spec.rb+++ b/spec/models/journable/timestamps_spec.rb@@ -550,6 +550,27 @@ describe Journable::Timestamps doendend+ describe "when chaining where clauses with OR (Arel::Nodes::Grouping)" do+ # https://github.com/opf/openproject/pull/11678#issuecomment-1328011996++ subject do+ relation = WorkPackage.where(subject: "Foo").or(+ WorkPackage.where(description: "The work package as it has been on Wednesday")+ )+ relation.at_timestamp(wednesday)+ end++ it "transforms the expression to query the correct table" do+ expect(subject.to_sql).to include \+ "\"work_package_journals\".\"subject\" = 'Foo' OR " \+ "\"work_package_journals\".\"description\" = 'The work package as it has been on Wednesday'"+ end++ it "returns the requested work package" do+ expect(subject).to include work_package+ end+ end+describe "when chaining an order clause" dosubject { WorkPackage.at_timestamp(wednesday).order(description: :desc) }