From aeba93eb14df3b59d5c21179896ea6de5d020132 Mon Sep 17 00:00:00 2001 From: Alexander Meindl Date: Mon, 13 Jun 2022 07:42:26 +0200 Subject: [PATCH] provide notes filter --- app/models/additionals_journal.rb | 18 +++++++++--------- app/models/additionals_query.rb | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/app/models/additionals_journal.rb b/app/models/additionals_journal.rb index 51218a98..394edb12 100644 --- a/app/models/additionals_journal.rb +++ b/app/models/additionals_journal.rb @@ -43,19 +43,19 @@ class AdditionalsJournal # Preloads visible last notes for a collection of entity # this is a copy of Issue.load_visible_last_notes, but usable for all entities # @see https://www.redmine.org/projects/redmine/repository/entry/trunk/app/models/issue.rb#L1214 - def load_visible_last_notes(entries, entity, user = User.current) - return unless entries.any? + def load_visible_last_notes(entries, entity, user = User.current, scope = nil) + return if entries.none? ids = entries.map(&:id) journal_class = (entity == Issue ? Journal : "#{entity}Journal").constantize - journal_ids = journal_class.joins(entity.name.underscore.to_sym => :project) - .where(journalized_type: entity.to_s, journalized_id: ids) - .where(journal_class.visible_notes_condition(user, skip_pre_condition: true)) - .where.not(notes: '') - .group(:journalized_id) - .maximum(:id) - .values + scope ||= journal_class.joins entity.name.underscore.to_sym => :project + journal_ids = scope.where(journalized_type: entity.to_s, journalized_id: ids) + .where(journal_class.visible_notes_condition(user, skip_pre_condition: true)) + .where.not(notes: '') + .group(:journalized_id) + .maximum(:id) + .values journals = Journal.where(id: journal_ids).to_a diff --git a/app/models/additionals_query.rb b/app/models/additionals_query.rb index af6fb80f..ccf30808 100644 --- a/app/models/additionals_query.rb +++ b/app/models/additionals_query.rb @@ -163,6 +163,20 @@ module AdditionalsQuery values end + def initialize_notes_filter(position: nil) + add_available_filter 'notes', type: :text, order: position + end + + def sql_for_notes_field(field, operator, value) + subquery = "SELECT 1 FROM #{Journal.table_name}" \ + " WHERE #{Journal.table_name}.journalized_type='#{queried_class}'" \ + " AND #{Journal.table_name}.journalized_id=#{queried_table_name}.id" \ + " AND (#{sql_for_field field, operator.sub(/^!/, ''), value, Journal.table_name, 'notes'})" \ + " AND (#{Journal.visible_notes_condition User.current, skip_pre_condition: true})" + + "#{/^!/.match?(operator) ? 'NOT EXISTS' : 'EXISTS'} (#{subquery})" + end + def sql_for_watcher_id_field(field, operator, value) watchable_type = queried_class == User ? 'Principal' : queried_class.to_s