provide notes filter

This commit is contained in:
Alexander Meindl 2022-06-13 07:42:26 +02:00
parent b1218577f3
commit aeba93eb14
2 changed files with 23 additions and 9 deletions

View File

@ -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

View File

@ -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