Provide preload method for last_notes

This commit is contained in:
Alexander Meindl 2020-05-05 15:25:23 +02:00
parent 02e45d32a1
commit 6c86a3fe7d

View File

@ -37,5 +37,30 @@ class AdditionalsJournal
true
end
# 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?
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
journals = Journal.where(id: journal_ids).to_a
entries.each do |entry|
journal = journals.detect { |j| j.journalized_id == entry.id }
entry.instance_variable_set('@last_notes', journal.try(:notes) || '')
end
end
end
end