Skip to content

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

Implement aggregation for journals with the same cause

Klaus Zanders855ce2eddf81
diff --git a/app/services/journals/create_service.rb b/app/services/journals/create_service.rb
index bbc5a2bfb8e..8ce4d56e61a 100644
--- a/app/services/journals/create_service.rb
+++ b/app/services/journals/create_service.rb
@@ -48,7 +48,7 @@ module Journals
self.journable = journable
end
- def call(notes: '', cause: nil)
+ def call(notes: '', cause: {})
Rails.logger.info("Journalin with cause #{cause}")
Journal.transaction do
@@ -69,16 +69,16 @@ module Journals
# and only the predecessor or the journal to be created has notes, the changes are aggregated.
# Instead of removing the predecessor, return it here so that it can be stripped in the journal creating
# SQL to than be refilled. That way, references to the journal, including ones users have, are kept intact.
- def aggregatable_predecessor(notes)
+ def aggregatable_predecessor(notes, cause)
predecessor = journable.last_journal
- if aggregatable?(predecessor, notes)
+ if aggregatable?(predecessor, notes, cause)
predecessor
end
end
def create_journal(notes, cause)
- predecessor = aggregatable_predecessor(notes)
+ predecessor = aggregatable_predecessor(notes, cause)
log_journal_creation(predecessor)
@@ -163,7 +163,7 @@ module Journals
), changes AS (
#{select_changed_sql}
), insert_data AS (
- #{insert_data_sql(notes, predecessor)}
+ #{insert_data_sql(predecessor, notes, cause)}
), inserted_journal AS (
#{update_or_insert_journal_sql(predecessor, notes, cause)}
), insert_attachable AS (
@@ -289,8 +289,8 @@ module Journals
data_type: journable.class.journal_class.name)
end
- def insert_data_sql(notes, predecessor)
- condition = if notes.blank? && predecessor.nil?
+ def insert_data_sql(predecessor, notes, cause)
+ condition = if notes.blank? && cause.blank? && predecessor.nil?
"AND EXISTS (SELECT * FROM changes)"
else
""
@@ -600,7 +600,7 @@ module Journals
end
def touch_journable(journal)
- return if journal.notes.blank?
+ return if journal.notes.blank? && journal.cause.blank?
# Not using touch here on purpose,
# as to avoid changing lock versions on the journables for this change
@@ -610,11 +610,12 @@ module Journals
journable.update_columns(timestamps) if timestamps.any?
end
- def aggregatable?(predecessor, notes)
+ def aggregatable?(predecessor, notes, cause)
predecessor.present? &&
aggregation_active? &&
within_aggregation_time?(predecessor) &&
same_user?(predecessor) &&
+ same_cause?(predecessor, cause) &&
only_one_note(predecessor, notes)
end
@@ -634,6 +635,10 @@ module Journals
predecessor.user_id == user.id
end
+ def same_cause?(predecessor, cause)
+ predecessor.cause == cause
+ end
+
def log_journal_creation(predecessor)
if predecessor
Rails.logger.debug { "Aggregating journal #{predecessor.id} for #{journable_type} ##{journable.id}" }
diff --git a/lib_static/plugins/acts_as_journalized/lib/acts/journalized/save_hooks.rb b/lib_static/plugins/acts_as_journalized/lib/acts/journalized/save_hooks.rb
index e374743609f..8530d7af6ba 100644
--- a/lib_static/plugins/acts_as_journalized/lib/acts/journalized/save_hooks.rb
+++ b/lib_static/plugins/acts_as_journalized/lib/acts/journalized/save_hooks.rb
@@ -73,7 +73,7 @@ module Acts::Journalized
end
end
- def add_journal(user: User.current, notes: '', cause: nil)
+ def add_journal(user: User.current, notes: '', cause: {})
self.journal_user ||= user
self.journal_notes ||= notes
self.journal_cause ||= cause
@@ -84,6 +84,7 @@ module Acts::Journalized
def with_ensured_journal_attributes
self.journal_user ||= User.current
self.journal_notes ||= ''
+ self.journal_cause ||= {}
yield
ensure
diff --git a/spec/models/work_package/work_package_acts_as_journalized_spec.rb b/spec/models/work_package/work_package_acts_as_journalized_spec.rb
index 6669e332f5e..85c59e1b30b 100644
--- a/spec/models/work_package/work_package_acts_as_journalized_spec.rb
+++ b/spec/models/work_package/work_package_acts_as_journalized_spec.rb
@@ -415,27 +415,100 @@ RSpec.describe WorkPackage do
end
context 'for only journal notes adding' do
- before do
- work_package.add_journal(user: User.current, notes: 'some notes')
+ let(:note_text) { 'some notes' }
+
+ subject do
+ work_package.add_journal(user: User.current, notes: note_text)
work_package.save
end
- it 'has the timestamp of the work package update time for created_at' do
- expect(work_package.last_journal.updated_at)
- .to eql(work_package.updated_at)
+ it 'does not create a new journal entry' do
+ expect { subject }.not_to change(work_package, :last_journal)
+ end
+
+ it 'has the timestamp of the work package update time for updated_at' do
+ subject
+ expect(work_package.last_journal.updated_at).to eql(work_package.updated_at)
+ end
+
+ it 'stores the note with the existing journal entry' do
+ expect { subject }.to change { work_package.last_journal.notes }.from('').to(note_text)
end
end
context 'for mixed journal notes and attribute adding' do
- before do
- work_package.add_journal(user: User.current, notes: 'some notes')
+ let(:note_text) { 'some notes' }
+
+ subject do
+ work_package.add_journal(user: User.current, notes: note_text)
+ work_package.subject = 'blubs'
+ work_package.save
+ end
+
+ it 'does not create a new journal entry' do
+ expect { subject }.not_to change(work_package, :last_journal)
+ end
+
+ it 'has the timestamp of the work package update time for updated_at' do
+ subject
+ expect(work_package.last_journal.updated_at).to eql(work_package.updated_at)
+ end
+
+ it 'stores the note with the existing journal entry' do
+ expect { subject }.to change { work_package.last_journal.notes }.from('').to(note_text)
+ end
+ end
+
+ context 'for only journal cause adding' do
+ subject do
+ work_package.add_journal(
+ user: User.current,
+ cause: {
+ type: 'work_package_predecessor_changed_times',
+ work_package_id: 42
+ }
+ )
+ work_package.save
+ end
+
+ it 'has the timestamp of the work package update time for created_at' do
+ subject
+ expect(work_package.last_journal.updated_at).to eql(work_package.updated_at)
+ end
+
+ it 'does create a new journal entry' do
+ expect { subject }.to change(work_package, :last_journal)
+ end
+ end
+
+ context 'for mixed journal cause, notes and attribute adding' do
+ subject do
+ work_package.add_journal(
+ user: User.current,
+ notes: 'some notes',
+ cause: {
+ type: 'work_package_predecessor_changed_times',
+ work_package_id: 42
+ }
+ )
work_package.subject = 'blubs'
work_package.save
end
it 'has the timestamp of the work package update time for created_at' do
- expect(work_package.last_journal.updated_at)
- .to eql(work_package.updated_at)
+ expect(work_package.last_journal.updated_at).to eql(work_package.updated_at)
+ end
+
+ it 'does create a new journal entry' do
+ expect { subject }.to change(work_package, :last_journal)
+ end
+
+ it 'stores the cause and note with the existing journal entry' do
+ subject
+
+ expect(work_package.last_journal.notes).to eq('some notes')
+ expect(work_package.last_journal.cause_type).to eq('work_package_predecessor_changed_times')
+ expect(work_package.last_journal.cause_work_package_id).to eq(42)
end
end