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
[#50278] Journalize the MeetingAgendaItem class
Dombi Attila75e420b9d2fe
diff --git a/app/models/journal.rb b/app/models/journal.rbindex 349474bc9ef..ae346900880 100644--- a/app/models/journal.rb+++ b/app/models/journal.rb@@ -77,6 +77,7 @@ class Journal < ApplicationRecordhas_many :attachable_journals, class_name: 'Journal::AttachableJournal', dependent: :delete_allhas_many :customizable_journals, class_name: 'Journal::CustomizableJournal', dependent: :delete_allhas_many :storable_journals, class_name: 'Journal::StorableJournal', dependent: :delete_all+ has_many :agenda_item_journals, class_name: 'Journal::MeetingAgendaItemJournal', dependent: :delete_allhas_many :notifications, dependent: :destroy@@ -86,6 +87,7 @@ class Journal < ApplicationRecordscope :for_wiki_page, -> { where(journable_type: "WikiPage") }scope :for_work_package, -> { where(journable_type: "WorkPackage") }+ scope :for_meeting, -> { where(journable_type: "Meeting") }# In conjunction with the included Comparable module, allows comparison of journal records# based on their corresponding version numbers, creation timestamps and IDs.diff --git a/app/services/journals/create_service.rb b/app/services/journals/create_service.rbindex d32a96f3dbf..2a4420a0fdc 100644--- a/app/services/journals/create_service.rb+++ b/app/services/journals/create_service.rb@@ -108,6 +108,7 @@ module Journals# * New entries in the attachable_journals table, one for every attachment the journable has at the time.# * New entries in the customizable_journals table, one for every custom value the journable has at the time.# * New entries in the storages_file_links_journals table, one for file link value the journable has at the time.+ # * New entries in the meeting_agenda_item_journals table, one for agenda_item the journable has at the time.## It consists of a couple of parts that are kept as individual queries (as CTEs) but# are all executed within a single database call.@@ -135,6 +136,8 @@ module Journals# recent journal.# * the journable's file_links are compared to the storages_file_links_journals entries being associated with the most# recent journal.+ # * the journable's meeting_agenda_items are compared to the meeting_agenda_item_journals entries being associated with the+ # most recent journal.# When comparing text based values, newlines are normalized as otherwise users having a different OS might change a text value# without intending to.#@@ -202,6 +205,9 @@ module Journalscleanup_predecessor_storable AS (#{cleanup_predecessor_storable(predecessor)}),+ cleanup_predecessor_agenda_itemable AS (+ #{cleanup_predecessor_agenda_itemable(predecessor)}+ ),max_journals AS (#{select_max_journal_sql(predecessor)}), changes AS (@@ -222,6 +228,8 @@ module Journals#{insert_customizable_sql}), insert_storable AS (#{insert_storable_sql}+ ), insert_agenda_itemable AS (+ #{insert_agenda_itemable_sql})SELECT * from inserted_journal@@ -256,6 +264,13 @@ module Journals:id)end+ def cleanup_predecessor_agenda_itemable(predecessor)+ cleanup_predecessor(predecessor,+ 'meeting_agenda_item_journals',+ :journal_id,+ :id)+ end+def cleanup_predecessor(predecessor, table_name, column, referenced_id)return "SELECT 1" unless predecessor@@ -448,6 +463,42 @@ module Journalsjournable_class_name:)end+ def insert_agenda_itemable_sql+ agenda_itemable_sql = <<~SQL+ INSERT INTO+ meeting_agenda_item_journals (+ journal_id,+ agenda_item_id,+ author_id,+ title,+ notes,+ position,+ duration_in_minutes,+ start_time,+ end_time,+ work_package_id+ )+ SELECT+ #{id_from_inserted_journal_sql},+ agenda_items.id,+ agenda_items.author_id,+ agenda_items.title,+ agenda_items.notes,+ agenda_items.position,+ agenda_items.duration_in_minutes,+ agenda_items.start_time,+ agenda_items.end_time,+ agenda_items.work_package_id+ FROM meeting_agenda_items agenda_items+ WHERE+ #{only_if_created_sql}+ AND agenda_items.meeting_id = :journable_id+ SQL++ sanitize(agenda_itemable_sql,+ journable_id: journable.id)+ end+# Updates the updated_at timestamp of the journable.# That is only carried out if the journable doesn't already have a newer timestamp than the most recent journal or# hasn't been updated by the `#save` call after which this service runs.@@ -557,6 +608,10 @@ module Journals(#{storable_changes_sql}) storable_changesONstorable_changes.journable_id = data_changes.journable_id+ FULL JOIN+ (#{agenda_itemable_changes_sql}) agenda_itemable_changes+ ON+ agenda_itemable_changes.journable_id = data_changes.journable_idSQLend@@ -640,6 +695,38 @@ module Journalscontainer_type: journable_class_name)end+ def agenda_itemable_changes_sql+ agenda_itemable_changes_sql = <<~SQL+ SELECT+ max_journals.journable_id+ FROM+ max_journals+ LEFT OUTER JOIN+ meeting_agenda_item_journals+ ON+ meeting_agenda_item_journals.journal_id = max_journals.id+ FULL JOIN+ (SELECT *+ FROM meeting_agenda_items+ WHERE meeting_agenda_items.meeting_id = :journable_id) agenda_items+ ON+ agenda_items.id = meeting_agenda_item_journals.agenda_item_id+ WHERE+ (agenda_items.id IS DISTINCT FROM meeting_agenda_item_journals.agenda_item_id)+ OR (agenda_items.title IS DISTINCT FROM meeting_agenda_item_journals.title)+ OR (#{normalize_newlines_sql('agenda_items.notes')} IS DISTINCT FROM+ #{normalize_newlines_sql('meeting_agenda_item_journals.notes')})+ OR (agenda_items.position IS DISTINCT FROM meeting_agenda_item_journals.position)+ OR (agenda_items.duration_in_minutes IS DISTINCT FROM meeting_agenda_item_journals.duration_in_minutes)+ OR (agenda_items.start_time IS DISTINCT FROM meeting_agenda_item_journals.start_time)+ OR (agenda_items.end_time IS DISTINCT FROM meeting_agenda_item_journals.end_time)+ OR (agenda_items.work_package_id IS DISTINCT FROM meeting_agenda_item_journals.work_package_id)+ SQL++ sanitize(agenda_itemable_changes_sql,+ journable_id: journable.id)+ end+def data_changes_sqldata_changes_sql = <<~SQLSELECTdiff --git a/db/migrate/20231003151656_create_meeting_agenda_item_journals.rb b/db/migrate/20231003151656_create_meeting_agenda_item_journals.rbnew file mode 100644index 00000000000..55e6c0f07e1--- /dev/null+++ b/db/migrate/20231003151656_create_meeting_agenda_item_journals.rb@@ -0,0 +1,18 @@+class CreateMeetingAgendaItemJournals < ActiveRecord::Migration[7.0]+ # rubocop:disable Rails/CreateTableWithTimestamps(RuboCop)+ def change+ create_table :meeting_agenda_item_journals do |t|+ t.integer :journal_id+ t.integer :agenda_item_id+ t.integer :author_id+ t.string :title+ t.text :notes+ t.integer :position+ t.integer :duration_in_minutes+ t.datetime :start_time+ t.datetime :end_time+ t.integer :work_package_id+ end+ end+ # rubocop:enable Rails/CreateTableWithTimestamps(RuboCop)+enddiff --git a/modules/meeting/app/controllers/meeting_agenda_items_controller.rb b/modules/meeting/app/controllers/meeting_agenda_items_controller.rbindex cb3357844f7..1dd463d09ad 100644--- a/modules/meeting/app/controllers/meeting_agenda_items_controller.rb+++ b/modules/meeting/app/controllers/meeting_agenda_items_controller.rb@@ -65,7 +65,7 @@ class MeetingAgendaItemsController < ApplicationController@meeting_agenda_item = call.resultif call.success?- # enabel continue editing+ # enable continue editingupdate_list_via_turbo_stream(form_hidden: false, form_type: @agenda_item_type)update_header_component_via_turbo_streamupdate_sidebar_details_component_via_turbo_streamdiff --git a/modules/meeting/app/models/journal/meeting_agenda_item_journal.rb b/modules/meeting/app/models/journal/meeting_agenda_item_journal.rbnew file mode 100644index 00000000000..4a3d543e588--- /dev/null+++ b/modules/meeting/app/models/journal/meeting_agenda_item_journal.rb@@ -0,0 +1,38 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2023 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++class Journal::MeetingAgendaItemJournal < Journal::BaseJournal+ self.table_name = 'meeting_agenda_item_journals'++ belongs_to :meeting+ belongs_to :author, class_name: 'User'++ def editable?+ false+ end+enddiff --git a/modules/meeting/app/services/meeting_agenda_items/create_service.rb b/modules/meeting/app/services/meeting_agenda_items/create_service.rbindex 93940cc3fb4..39583242c55 100644--- a/modules/meeting/app/services/meeting_agenda_items/create_service.rb+++ b/modules/meeting/app/services/meeting_agenda_items/create_service.rb@@ -28,5 +28,15 @@module MeetingAgendaItemsclass CreateService < ::BaseServices::Create+ include TouchMeeting++ def after_perform(call)+ meeting_agenda_item = call.result+ meeting = meeting_agenda_item.meeting++ touch(meeting) unless meeting.nil?++ call+ endendenddiff --git a/modules/meeting/app/services/meeting_agenda_items/delete_service.rb b/modules/meeting/app/services/meeting_agenda_items/delete_service.rbindex 079198106fd..bde330df939 100644--- a/modules/meeting/app/services/meeting_agenda_items/delete_service.rb+++ b/modules/meeting/app/services/meeting_agenda_items/delete_service.rb@@ -28,5 +28,15 @@module MeetingAgendaItemsclass DeleteService < ::BaseServices::Delete+ include TouchMeeting++ def after_perform(call)+ meeting_agenda_item = call.result+ meeting = meeting_agenda_item.meeting++ touch(meeting) unless meeting.nil?++ call+ endendenddiff --git a/modules/meeting/app/services/meeting_agenda_items/touch_meeting.rb b/modules/meeting/app/services/meeting_agenda_items/touch_meeting.rbnew file mode 100644index 00000000000..e8a3da9ac60--- /dev/null+++ b/modules/meeting/app/services/meeting_agenda_items/touch_meeting.rb@@ -0,0 +1,49 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2023 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++module MeetingAgendaItems+ module TouchMeeting+ extend ActiveSupport::Concern++ included do+ private++ def touch(meeting)+ # We allow invalid meetings to be saved as+ # adding the attachments does not change the validity of the meeting+ # but without that leeway, the user needs to fix the meeting before+ # the agenda_item can be added.+ # However we want the meeting to be updated when updating an agenda_item. This is important,+ # e.g. for invalidating caches and also for journalizing++ meeting.update_column(:updated_at, Time.current)+ meeting.save_journals if meeting.respond_to?(:save_journals)+ end+ end+ end+enddiff --git a/modules/meeting/app/services/meeting_agenda_items/update_service.rb b/modules/meeting/app/services/meeting_agenda_items/update_service.rbindex 07c10a49171..50786494ab3 100644--- a/modules/meeting/app/services/meeting_agenda_items/update_service.rb+++ b/modules/meeting/app/services/meeting_agenda_items/update_service.rb@@ -28,5 +28,15 @@module MeetingAgendaItemsclass UpdateService < ::BaseServices::Update+ include TouchMeeting++ def after_perform(call)+ meeting_agenda_item = call.result+ meeting = meeting_agenda_item.meeting++ touch(meeting) unless meeting.nil?++ call+ endendenddiff --git a/modules/meeting/spec/factories/meeting_agenda_item_factory.rb b/modules/meeting/spec/factories/meeting_agenda_item_factory.rbindex 34686b173ad..6e30049ab64 100644--- a/modules/meeting/spec/factories/meeting_agenda_item_factory.rb+++ b/modules/meeting/spec/factories/meeting_agenda_item_factory.rb@@ -28,7 +28,7 @@FactoryBot.define dofactory :meeting_agenda_item do |m|- meeting+ meeting factory: :structured_meetingwork_package { nil }author factory: :userduration_in_minutes { 10 }diff --git a/modules/meeting/spec/models/meeting_acts_as_journalized_spec.rb b/modules/meeting/spec/models/meeting_acts_as_journalized_spec.rbnew file mode 100644index 00000000000..72232173994--- /dev/null+++ b/modules/meeting/spec/models/meeting_acts_as_journalized_spec.rb@@ -0,0 +1,307 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2023 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++require 'spec_helper'++RSpec.describe Meeting do+ shared_let (:user) { create(:user) }+ current_user { user }++ let!(:meeting) do+ User.execute_as current_user do+ create(:structured_meeting, author: user)+ end+ end++ describe '#journal' do+ context 'for meeting creation' do+ it { expect(Journal.for_meeting.count).to eq(1) }++ it 'has a journal entry' do+ expect(Journal.for_meeting.first.journable).to eq(meeting)+ end++ it 'notes the changes to title' do+ expect(meeting.last_journal.details[:title])+ .to contain_exactly(nil, meeting.title)+ end++ it 'notes the changes to project' do+ expect(meeting.last_journal.details[:project_id])+ .to contain_exactly(nil, meeting.project_id)+ end++ it 'notes the location' do+ expect(meeting.last_journal.details[:location])+ .to contain_exactly(nil, meeting.location)+ end++ it 'notes the start time' do+ expect(meeting.last_journal.details[:start_time])+ .to contain_exactly(nil, meeting.start_time)+ end++ it 'notes the duration' do+ expect(meeting.last_journal.details[:duration])+ .to contain_exactly(nil, meeting.duration)+ end++ it 'has the timestamp of the meeting update time for created_at' do+ expect(meeting.last_journal.created_at)+ .to eql(meeting.reload.updated_at)+ end++ it 'has the updated_at of the meeting as the lower bound for validity_period and no upper bound' do+ expect(meeting.last_journal.validity_period)+ .to eql(meeting.reload.updated_at...Float::INFINITY)+ end+ end++ context 'when nothing is changed' do+ it { expect { meeting.save! }.not_to change(Journal, :count) }++ it 'does not update the updated_at time of the work package' do+ expect { meeting.save! }.not_to change(meeting, :updated_at)+ end+ end++ describe 'agenda_items' do+ before do+ meeting.agenda_items << create(:meeting_agenda_item, meeting:, **agenda_item_attributes)+ meeting.save+ end++ let(:agenda_item_attributes) { {} }+ let(:agenda_item) { meeting.agenda_items.first }+ let(:agenda_item_journals) { meeting.journals.last.agenda_item_journals }++ context 'for a new agenda item within aggregation time' do+ it { expect(meeting.journals.count).to eq(1) }+ it { expect(agenda_item_journals.count).to eq(1) }++ it {+ expect(agenda_item_journals.last).to have_attributes agenda_item.attributes.slice(+ "author_id", "title", "notes", "position", "duration_in_minutes",+ "start_time", "end_time", "work_package_id"+ )+ }+ end++ context 'for a new agenda item outside aggregation time', with_settings: { journal_aggregation_time_minutes: 0 } do+ it { expect(meeting.journals.count).to eq(2) }+ it { expect(meeting.journals.first.agenda_item_journals).to be_empty }+ it { expect(agenda_item_journals.count).to eq(1) }++ it {+ expect(agenda_item_journals.last).to have_attributes agenda_item.attributes.slice(+ "author_id", "title", "notes", "position", "duration_in_minutes",+ "start_time", "end_time", "work_package_id"+ )+ }+ end++ context 'when agenda item saved w/o change' do+ it {+ expect do+ agenda_item.save+ meeting.save_journals+ end.not_to change(Journal, :count)+ }+ end++ Journal::MeetingAgendaItemJournal.columns_hash.slice(+ "notes", "position", "duration_in_minutes", "work_package_id"+ ).each do |column_name, column_info|+ column_value =+ case column_info.type+ when :integer then 11+ else 'A string'+ end++ context 'when updating an agenda_item within the aggregation time' do+ shared_examples 'it updates the existing journals' do+ subject(:update_agenda_item) do+ agenda_item.update(column_name => updated_value)+ meeting.save_journals+ end++ it { expect { update_agenda_item }.not_to change(Journal, :count) }++ it 'updates the agenda item journal' do+ expect { update_agenda_item }+ .to change { agenda_item_journals.last.send(column_name) }+ .to(updated_value)+ end+ end++ describe "setting a value for the #{column_name} column" do+ it_behaves_like 'it updates the existing journals' do+ let(:updated_value) { column_value }+ end+ end++ describe "unsetting a value for the #{column_name} column" do+ let(:agenda_item_attributes) { { column_name => column_value } }++ it_behaves_like 'it updates the existing journals' do+ let(:updated_value) { nil }+ end+ end+ end++ context 'when updating an agenda_item outside the aggregation time',+ with_settings: { journal_aggregation_time_minutes: 0 } do+ shared_examples 'creates a new journal and keeps old journal intact' do+ subject(:update_agenda_item) do+ agenda_item.update(column_name => updated_value)+ meeting.save_journals+ end++ it { expect { update_agenda_item }.to change(Journal, :count).by(1) }+ it { expect { update_agenda_item }.to change(Journal::MeetingJournal, :count).by(1) }+ it { expect { update_agenda_item }.to change(Journal::MeetingAgendaItemJournal, :count).by(1) }++ it 'does not update the previous agenda item journal' do+ previous_journal_agenda_items = agenda_item_journals+ expect { update_agenda_item }+ .not_to change { previous_journal_agenda_items.reload }+ end++ it 'updates the new agenda item journal' do+ expect { update_agenda_item }+ .to change { Journal::MeetingAgendaItemJournal.last.send(column_name) }+ .to(updated_value)+ end+ end++ describe "setting a value for the #{column_name} column" do+ it_behaves_like 'creates a new journal and keeps old journal intact' do+ let(:updated_value) { column_value }+ end+ end++ describe "unsetting a value for the #{column_name} column" do+ let(:agenda_item_attributes) { { column_name => column_value } }++ it_behaves_like 'creates a new journal and keeps old journal intact' do+ let(:updated_value) { nil }+ end+ end+ end+ end++ context 'for a removed agenda_item within aggregation time' do+ subject(:remove_agenda_item) do+ agenda_item.destroy+ meeting.save_journals+ end++ it { expect { remove_agenda_item }.not_to change(Journal, :count) }++ it {+ expect { remove_agenda_item }+ .to change { agenda_item_journals.reload.count }+ .from(1)+ .to(0)+ }+ end++ context 'for a removed agenda_item outside aggregation time', with_settings: { journal_aggregation_time_minutes: 0 } do+ let(:agenda_item_journals) { meeting.journals.second.agenda_item_journals }++ subject(:remove_agenda_item) do+ agenda_item.destroy+ meeting.save_journals+ end++ it {+ expect { remove_agenda_item }+ .to change { meeting.journals.count }+ .from(2)+ .to(3)+ }++ it {+ expect { remove_agenda_item }+ .not_to change { meeting.journals.first.agenda_item_journals.reload.size }+ }++ it {+ expect { remove_agenda_item }+ .not_to change { agenda_item_journals.reload.count }+ }++ it {+ remove_agenda_item+ expect(agenda_item_journals.last).to have_attributes agenda_item.attributes.slice(+ "author_id", "title", "notes", "position", "duration_in_minutes",+ "start_time", "end_time", "work_package_id"+ )+ }++ it 'removes the agenda_item_journals from the new journal' do+ remove_agenda_item+ expect(meeting.journals.last.agenda_item_journals).to be_empty+ end+ end+ end+ end++ describe '#destroy' do+ before do+ meeting.agenda_items << create(:meeting_agenda_item)+ meeting.save+ end++ let(:journal) { meeting.journals.first }+ let(:agenda_item_journals) { journal.agenda_item_journals }++ subject { meeting.destroy }++ it 'removes the journal' do+ expect { subject }+ .to change { Journal.exists?(journal.id) }+ .from(true)+ .to(false)+ end++ it 'removes the journal data' do+ expect { subject }+ .to change { Journal::MeetingJournal.exists?(id: journal.data_id) }+ .from(true)+ .to(false)+ end++ it 'removes the meeting agenda items journals' do+ expect { subject }+ .to change {+ Journal::MeetingAgendaItemJournal.where(id: agenda_item_journals.map(&:id)).count+ }.by(-1)+ end+ end+end