2021-04-18 13:34:55 +02:00
# frozen_string_literal: true
2019-10-11 15:41:46 +02:00
class AdditionalsJournal
2020-01-23 15:15:12 +01:00
class << self
def save_journal_history ( journal , prop_key , ids_old , ids )
ids_all = ( ids_old + ids ) . uniq
ids_all . each do | id |
next if ids_old . include? ( id ) && ids . include? ( id )
2021-04-18 13:34:55 +02:00
if ids . include? id
2020-01-23 15:15:12 +01:00
value = id
old_value = nil
else
old_value = id
value = nil
end
journal . details << JournalDetail . new ( property : 'attr' ,
prop_key : prop_key ,
old_value : old_value ,
value : value )
journal . save
2019-10-11 15:41:46 +02:00
end
2020-01-23 15:15:12 +01:00
true
2019-10-11 15:41:46 +02:00
end
2020-01-23 15:15:12 +01:00
def validate_relation ( entries , entry_id )
old_entries = entries . select { | entry | entry . id . present? }
new_entries = entries . select { | entry | entry . id . blank? }
return true if new_entries . blank?
2021-04-18 13:34:55 +02:00
new_entries . map! { | entry | entry . send entry_id }
2020-01-23 15:15:12 +01:00
return false if new_entries . count != new_entries . uniq . count
2021-04-18 13:34:55 +02:00
old_entries . map! { | entry | entry . send entry_id }
2020-01-23 15:15:12 +01:00
return false unless ( old_entries & new_entries ) . count . zero?
true
end
2020-05-05 15:25:23 +02:00
# 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 }
2021-12-26 05:57:23 +01:00
entry . instance_variable_set ( :@last_notes , journal . try ( :notes ) || '' )
2020-05-05 15:25:23 +02:00
end
end
2020-06-22 10:29:39 +02:00
def set_relation_detail ( entity , detail , value_key )
value = detail . send value_key
detail [ value_key ] = ( entity . find_by ( id : value ) || value ) if value . present?
end
2019-10-11 15:41:46 +02:00
end
end