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
[#50496] Keep MeetingAgenda items when deleting related Work Package
Dombi Attila2080e2e1db31
diff --git a/app/models/work_package.rb b/app/models/work_package.rbindex b418e8854c2..e806e3395ac 100644--- a/app/models/work_package.rb+++ b/app/models/work_package.rb@@ -70,7 +70,8 @@ class WorkPackage < ApplicationRecordhas_many :members, as: :entity, dependent: :destroyhas_many :member_principals, through: :members, class_name: 'Principal', source: :principal- has_many :meeting_agenda_items, dependent: :destroy # Question: What about finalized minutes within an agenda item?+ has_many :meeting_agenda_items, dependent: nil # Cleanup handled in dissociate_agenda_items method+ has_many :meetings, through: :meeting_agenda_items, source: :meetingscope :recently_updated, -> {order(updated_at: :desc)@@ -141,6 +142,7 @@ class WorkPackage < ApplicationRecordif: lambda { |work_package| work_package.errors.messages.has_key? :attachments }before_save :close_duplicates, :update_done_ratio_from_statusbefore_create :default_assign+ before_destroy :dissociate_agenda_items, if: -> { meeting_agenda_items.any? }acts_as_customizable@@ -636,4 +638,12 @@ class WorkPackage < ApplicationRecorderrors.messages[:attachments].first << " - #{invalid_attachment.errors.full_messages.first}"endend++ def dissociate_agenda_items+ # Unscoping the order is required, because the MeetingAgendaItems have a default order+ # and it throws an error, because the ordered field is not part of the select.+ uniq_meetings = meetings.unscope(:order).uniq+ meeting_agenda_items.update_all(work_package_id: nil)+ uniq_meetings.each(&:save_journals)+ endenddiff --git a/modules/meeting/app/models/journal/meeting_agenda_item_journal.rb b/modules/meeting/app/models/journal/meeting_agenda_item_journal.rbindex 4a3d543e588..637af1b5380 100644--- a/modules/meeting/app/models/journal/meeting_agenda_item_journal.rb+++ b/modules/meeting/app/models/journal/meeting_agenda_item_journal.rb@@ -31,6 +31,7 @@ class Journal::MeetingAgendaItemJournal < Journal::BaseJournalbelongs_to :meetingbelongs_to :author, class_name: 'User'+ belongs_to :agenda_item, class_name: 'MeetingAgendaItem'def editable?falsediff --git a/spec/models/work_package_spec.rb b/spec/models/work_package_spec.rbindex 9db9a9ebc18..423e65bd184 100644--- a/spec/models/work_package_spec.rb+++ b/spec/models/work_package_spec.rb@@ -70,6 +70,8 @@ RSpec.describe WorkPackage doit { is_expected.to have_and_belong_to_many(:github_pull_requests) }it { is_expected.to have_many(:members).dependent(:destroy) }it { is_expected.to have_many(:member_principals).through(:members).class_name('Principal').source(:principal) }+ it { is_expected.to have_many(:meeting_agenda_items) }+ it { is_expected.to have_many(:meetings).through(:meeting_agenda_items).source(:meeting) }enddescribe '.new' do@@ -724,4 +726,56 @@ RSpec.describe WorkPackage doendendend++ context 'when destroying with agenda items' do+ let(:meeting_agenda_items) { create_list(:meeting_agenda_item, 3, work_package:) }+ let(:other_agenda_item) { create(:meeting_agenda_item, work_package_id: create(:work_package).id) }+ let(:other_meeting) { other_agenda_item.meeting }+ let(:latest_journals) do+ Journal+ .select('DISTINCT ON (journable_id) *')+ .where(journable_type: 'Meeting', journable_id: meeting_agenda_items.pluck(:meeting_id))+ .order('journable_id, updated_at DESC')+ end++ subject { work_package.destroy }++ before do+ work_package.save+ meeting_agenda_items+ other_agenda_item+ Meeting.find_each(&:save_journals)+ end++ it 'dissociates the agenda items' do+ expect { subject }+ .to change { MeetingAgendaItem.find(meeting_agenda_items).pluck(:work_package_id) }+ .from(Array.new(3, work_package.id))+ .to(Array.new(3, nil))+ end++ it 'does not affect other agenda items' do+ expect { subject }.not_to change(other_agenda_item, :reload)+ end++ it 'updates the agenda item journal' do+ expect { subject }+ .to change {+ Journal::MeetingAgendaItemJournal+ .where(agenda_item: meeting_agenda_items)+ .pluck(:work_package_id)+ }+ .from(Array.new(3, work_package.id))+ .to(Array.new(3, nil))+ end++ it 'does not affect the agenda item journal' do+ expect { subject }+ .not_to change {+ Journal::MeetingAgendaItemJournal+ .find_by(agenda_item: other_agenda_item)+ .work_package_id+ }+ end+ endend