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
Add journal messages for work package duration changes
Dombi Attila9ee0dfdcf595
diff --git a/app/models/work_package/journalized.rb b/app/models/work_package/journalized.rbindex 21b82cb3152..7e1f4977b1f 100644--- a/app/models/work_package/journalized.rb+++ b/app/models/work_package/journalized.rb@@ -80,7 +80,7 @@ module WorkPackage::Journalizedregister_journal_formatter(:cost_association) do |value, journable, field|association = journable.class.reflect_on_association(field.to_sym)if association- record = association.class_name.constantize.find_by_id(value.to_i)+ record = association.class_name.constantize.find_by(id: value.to_i)record&.subjectendend@@ -103,5 +103,6 @@ module WorkPackage::Journalized:author_id, :responsible_idregister_on_journal_formatter :datetime, :start_date, :due_dateregister_on_journal_formatter :plaintext, :subject+ register_on_journal_formatter :day_count, :durationendenddiff --git a/lib_static/plugins/acts_as_journalized/lib/journal_formatter.rb b/lib_static/plugins/acts_as_journalized/lib/journal_formatter.rbindex 59c5ea9cc97..07d9310fdf2 100644--- a/lib_static/plugins/acts_as_journalized/lib/journal_formatter.rb+++ b/lib_static/plugins/acts_as_journalized/lib/journal_formatter.rb@@ -52,6 +52,7 @@require_relative './journal_formatter/base'require_relative './journal_formatter/attribute'require_relative './journal_formatter/datetime'+require_relative './journal_formatter/day_count'require_relative './journal_formatter/decimal'require_relative './journal_formatter/fraction'require_relative './journal_formatter/id'@@ -79,12 +80,13 @@ module JournalFormatterenddef self.default_formatters- { plaintext: JournalFormatter::Plaintext,- datetime: JournalFormatter::Datetime,- named_association: JournalFormatter::NamedAssociation,- fraction: JournalFormatter::Fraction,+ { datetime: JournalFormatter::Datetime,+ day_count: JournalFormatter::DayCount,decimal: JournalFormatter::Decimal,- id: JournalFormatter::Id }+ fraction: JournalFormatter::Fraction,+ id: JournalFormatter::Id,+ named_association: JournalFormatter::NamedAssociation,+ plaintext: JournalFormatter::Plaintext }endself.formatters = default_formattersdiff --git a/lib_static/plugins/acts_as_journalized/lib/journal_formatter/day_count.rb b/lib_static/plugins/acts_as_journalized/lib/journal_formatter/day_count.rbnew file mode 100644index 00000000000..892b7332966--- /dev/null+++ b/lib_static/plugins/acts_as_journalized/lib/journal_formatter/day_count.rb@@ -0,0 +1,35 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2022 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 JournalFormatter+ class DayCount < Attribute+ def format_values(values)+ values.map { |v| "#{v.to_i} #{'day'.pluralize(v.to_i)}" unless v.nil? }+ end+ end+enddiff --git a/spec/lib/journal_formatter/day_count_spec.rb b/spec/lib/journal_formatter/day_count_spec.rbnew file mode 100644index 00000000000..fe965b3ae18--- /dev/null+++ b/spec/lib/journal_formatter/day_count_spec.rb@@ -0,0 +1,73 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2022 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 File.expand_path("#{File.dirname(__FILE__)}/../../spec_helper.rb")++describe JournalFormatter::DayCount do+ let(:klass) { described_class }+ let(:id) { 1 }+ let(:journal) do+ instance_double(Journal, journable: WorkPackage.new)+ end+ let(:instance) { klass.new(journal) }+ let(:key) { :duration }++ describe '#render' do+ describe 'when setting the old value to 1 day, and the new value to 3 days' do+ let(:expected) do+ I18n.t(:text_journal_changed_html,+ label: "<strong>Duration</strong>",+ old: '<i title="1 day">1 day</i>',+ new: '<i title="3 days">3 days</i>',+ linebreak: '')+ end++ it { expect(instance.render(key, [1, 3])).to eq(expected) }+ end++ describe 'when setting the initial value to 3 days' do+ let(:expected) do+ I18n.t(:text_journal_set_to,+ label: "<strong>Duration</strong>",+ value: '<i title="3 days">3 days</i>')+ end++ it { expect(instance.render(key, [nil, 3])).to eq(expected) }+ end++ describe 'when deleting the initial value of 3 days' do+ let(:expected) do+ I18n.t(:text_journal_deleted,+ label: "<strong>Duration</strong>",+ old: '<strike><i title="3 days">3 days</i></strike>')+ end++ it { expect(instance.render(key, [3, nil])).to eq(expected) }+ end+ end+enddiff --git a/spec/models/work_package/work_package_acts_as_journalized_spec.rb b/spec/models/work_package/work_package_acts_as_journalized_spec.rbindex 98977c8a568..da3b95e6894 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@@ -43,7 +43,8 @@ describe WorkPackage, type: :model dotype:,description: 'Description',priority:,- status:)+ status:,+ duration: 1)endlet(:current_user) { create(:user) }@@ -53,7 +54,7 @@ describe WorkPackage, type: :model dowork_packageend- context 'on work package creation' do+ context 'for work package creation' doit { expect(Journal.all.count).to eq(1) }it 'has a journal entry' do@@ -89,18 +90,14 @@ describe WorkPackage, type: :model doendend- context 'nothing is changed' do- before do- work_package.save!- end-- it { expect(Journal.all.count).to eq(1) }+ context 'when nothing is changed' do+ it { expect { work_package.save! }.not_to change(Journal, :count) }end- context 'different newlines', with_settings: { journal_aggregation_time_minutes: 0 } do+ context 'for different newlines', with_settings: { journal_aggregation_time_minutes: 0 } dolet(:description) { "Description\n\nwith newlines\n\nembedded" }let(:changed_description) { description.gsub("\n", "\r\n") }- let!(:work_package_1) do+ let!(:work_package1) docreate(:work_package,project_id: project.id,type:,@@ -109,23 +106,23 @@ describe WorkPackage, type: :model doendbefore do- work_package_1.description = changed_description+ work_package1.description = changed_descriptionendcontext 'when a new journal is created tracking a simultaneously applied change' dobefore do- work_package_1.subject += 'changed'- work_package_1.save!+ work_package1.subject += 'changed'+ work_package1.save!enddescribe 'does not track the changed newline characters' do- subject { work_package_1.journals.last.data.description }+ subject { work_package1.journals.last.data.description }it { is_expected.to eq(description) }enddescribe 'tracks only the other change' do- subject { work_package_1.journals.last.details }+ subject { work_package1.journals.last.details }it { is_expected.to have_key :subject }it { is_expected.not_to have_key :description }@@ -133,48 +130,49 @@ describe WorkPackage, type: :model doendcontext 'when there is a legacy journal containing non-escaped newlines' do- let!(:work_package_journal_1) do+ let!(:work_package_journal1) docreate(:work_package_journal,- journable_id: work_package_1.id,+ journable_id: work_package1.id,version: 2,data: build(:journal_work_package_journal,description:))end- let!(:work_package_journal_2) do+ let!(:work_package_journal2) docreate(:work_package_journal,- journable_id: work_package_1.id,+ journable_id: work_package1.id,version: 3,data: build(:journal_work_package_journal,description: changed_description))end- subject { work_package_1.journals.reload.last.details }+ subject { work_package1.journals.reload.last.details }it { is_expected.not_to have_key :description }endend- context 'on work package change', with_settings: { journal_aggregation_time_minutes: 0 } do+ describe 'on work package change', with_settings: { journal_aggregation_time_minutes: 0 } dolet(:parent_work_package) docreate(:work_package,project_id: project.id,type:,priority:)end- let(:type_2) { create :type }- let(:status_2) { create :status }- let(:priority_2) { create :priority }+ let(:type2) { create :type }+ let(:status2) { create :status }+ let(:priority2) { create :priority }before do- project.types << type_2+ project.types << type2work_package.subject = 'changed'work_package.description = 'changed'- work_package.type = type_2- work_package.status = status_2- work_package.priority = priority_2+ work_package.type = type2+ work_package.status = status2+ work_package.priority = priority2work_package.start_date = Date.new(2013, 1, 24)work_package.due_date = Date.new(2013, 1, 31)+ work_package.duration = 8work_package.estimated_hours = 40.0work_package.assigned_to = User.currentwork_package.responsible = User.current@@ -184,7 +182,7 @@ describe WorkPackage, type: :model dowork_package.save!end- context 'last created journal' do+ context 'for last created journal' dosubject { work_package.journals.reload.last.details }it 'contains all changes' do@@ -209,37 +207,53 @@ describe WorkPackage, type: :model doenddescribe 'journaled value for' do- context 'description' do+ describe 'description' dolet(:property) { 'description' }- context 'old_value' do+ context 'for old value' dolet(:expected_value) { 'Description' }it_behaves_like 'old value'end- context 'new value' do+ context 'for new value' dolet(:expected_value) { 'changed' }it_behaves_like 'new value'endend- context 'schedule_manually' do+ describe 'schedule_manually' dolet(:property) { 'schedule_manually' }- context 'old_value' do+ context 'for old value' dolet(:expected_value) { false }it_behaves_like 'old value'end- context 'new value' do+ context 'for new value' dolet(:expected_value) { true }it_behaves_like 'new value'endend++ describe 'duration' do+ let(:property) { 'duration' }++ context 'for old value' do+ let(:expected_value) { 1 }++ it_behaves_like 'old value'+ end++ context 'for new value' do+ let(:expected_value) { 8 }++ it_behaves_like 'new value'+ end+ endenddescribe 'adding journal with a missing journal and an existing journal' do@@ -271,7 +285,7 @@ describe WorkPackage, type: :model doendend- context 'attachments', with_settings: { journal_aggregation_time_minutes: 0 } do+ describe 'attachments', with_settings: { journal_aggregation_time_minutes: 0 } dolet(:attachment) { build :attachment }let(:attachment_id) { "attachments_#{attachment.id}" }@@ -280,7 +294,7 @@ describe WorkPackage, type: :model dowork_package.save!end- context 'new attachment' do+ context 'for new attachment' dosubject { work_package.journals.reload.last.details }it { is_expected.to have_key attachment_id }@@ -288,20 +302,12 @@ describe WorkPackage, type: :model doit { expect(subject[attachment_id]).to eq([nil, attachment.filename]) }end- context 'attachment saved w/o change' do- before do- @original_journal_count = work_package.journals.reload.count-- attachment.save!- end-- subject { work_package.journals.reload.count }-- it { is_expected.to eq(@original_journal_count) }+ context 'when attachment saved w/o change' do+ it { expect { attachment.save! }.not_to change(Journal, :count) }endend- context 'custom values', with_settings: { journal_aggregation_time_minutes: 0 } do+ describe 'custom values', with_settings: { journal_aggregation_time_minutes: 0 } dolet(:custom_field) { create :work_package_custom_field }let(:custom_value) dobuild :custom_value,@@ -311,7 +317,7 @@ describe WorkPackage, type: :model dolet(:custom_field_id) { "custom_fields_#{custom_value.custom_field_id}" }- shared_context 'work package with custom value' do+ shared_context 'for work package with custom value' dobefore doproject.work_package_custom_fields << custom_fieldtype.custom_fields << custom_field@@ -321,8 +327,8 @@ describe WorkPackage, type: :model doendend- context 'new custom value' do- include_context 'work package with custom value'+ context 'for new custom value' do+ include_context 'for work package with custom value'subject { work_package.journals.reload.last.details }@@ -331,8 +337,8 @@ describe WorkPackage, type: :model doit { expect(subject[custom_field_id]).to eq([nil, custom_value.value]) }end- context 'custom value modified' do- include_context 'work package with custom value'+ context 'for custom value modified' do+ include_context 'for work package with custom value'let(:modified_custom_value) docreate :custom_value,@@ -352,8 +358,8 @@ describe WorkPackage, type: :model doit { expect(subject[custom_field_id]).to eq([custom_value.value.to_s, modified_custom_value.value.to_s]) }end- context 'work package saved w/o change' do- include_context 'work package with custom value'+ context 'when work package saved w/o change' do+ include_context 'for work package with custom value'let(:unmodified_custom_value) docreate :custom_value,@@ -362,20 +368,14 @@ describe WorkPackage, type: :model doendbefore do- @original_journal_count = work_package.journals.reload.count-work_package.custom_values = [unmodified_custom_value]-- work_package.save!end- subject { work_package.journals.reload.count }-- it { is_expected.to eq(@original_journal_count) }+ it { expect { work_package.save! }.not_to change(Journal, :count) }end- context 'custom value removed' do- include_context 'work package with custom value'+ context 'when custom value removed' do+ include_context 'for work package with custom value'before dowork_package.custom_values.delete(custom_value)@@ -389,7 +389,7 @@ describe WorkPackage, type: :model doit { expect(subject[custom_field_id]).to eq([custom_value.value, nil]) }end- context 'custom value did not exist before' do+ context 'when custom value did not exist before' dolet(:custom_field) docreate :work_package_custom_field,is_required: false,@@ -404,20 +404,20 @@ describe WorkPackage, type: :model doenddescribe 'empty values are recognized as unchanged' do- include_context 'work package with custom value'+ include_context 'for work package with custom value'it { expect(work_package.journals.reload.last.customizable_journals).to be_empty }enddescribe 'empty values handled as non existing' do- include_context 'work package with custom value'+ include_context 'for work package with custom value'it { expect(work_package.journals.reload.last.customizable_journals.count).to eq(0) }endendend- context 'on only journal notes adding' do+ context 'for only journal notes adding' dobefore dowork_package.add_journal(User.current, 'some notes')work_package.save@@ -432,7 +432,7 @@ describe WorkPackage, type: :model doendend- context 'on mixed journal notes and attribute adding' do+ context 'for mixed journal notes and attribute adding' dobefore dowork_package.add_journal(User.current, 'some notes')work_package.subject = 'blubs'@@ -448,7 +448,7 @@ describe WorkPackage, type: :model doendend- context 'updated within aggregation time' do+ context 'when updated within aggregation time' dosubject(:journals) { work_package.journals }let(:current_user) { user1 }@@ -471,7 +471,7 @@ describe WorkPackage, type: :model dowork_package.save!end- context 'by author of last change' do+ context 'as author of last change' dolet(:new_author) { user1 }it 'leads to a single journal' do@@ -479,7 +479,7 @@ describe WorkPackage, type: :model doendit 'is the initial journal' do- expect(subject.first.initial?).to be_truthy+ expect(subject.first).to be_initialendit 'contains the changes of both updates with the later overwriting the former' do@@ -514,8 +514,8 @@ describe WorkPackage, type: :model doendit 'has one initial journal and one non-initial journal' do- expect(subject.first.initial?).to be_truthy- expect(subject.second.initial?).to be_falsey+ expect(subject.first).to be_initial+ expect(subject.second).not_to be_initialendend@@ -541,7 +541,7 @@ describe WorkPackage, type: :model doendend- context 'by a different author' do+ context 'as a different author' dolet(:new_author) { user2 }it 'leads to two journals' do@@ -558,12 +558,12 @@ describe WorkPackage, type: :model doendend- context 'updated after aggregation timeout expired', with_settings: { journal_aggregation_time_minutes: 1 } do+ context 'when updated after aggregation timeout expired', with_settings: { journal_aggregation_time_minutes: 1 } dosubject(:journals) { work_package.journals }before do- work_package.journals.last.update_columns(created_at: Time.now - 2.minutes,- updated_at: Time.now - 2.minutes)+ work_package.journals.last.update_columns(created_at: 2.minutes.ago,+ updated_at: 2.minutes.ago)work_package.status = build(:status)work_package.save!@@ -574,10 +574,10 @@ describe WorkPackage, type: :model doendend- context 'updating with aggregation disabled', with_settings: { journal_aggregation_time_minutes: 0 } do+ context 'when updating with aggregation disabled', with_settings: { journal_aggregation_time_minutes: 0 } dosubject(:journals) { work_package.journals }- context 'WP updated within milliseconds' do+ context 'when WP updated within milliseconds' dobefore dowork_package.status = build(:status)work_package.save!@@ -590,7 +590,7 @@ describe WorkPackage, type: :model doendend- context 'on #destroy' do+ describe 'on #destroy' dolet(:project) { create(:project) }let(:type) { create(:type) }let(:custom_field) do