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

Journalize project attributes

Christophe Bliard03dac03c0921
diff --git a/app/models/journal.rb b/app/models/journal.rb
index 96adca9b96d..740850c7e1d 100644
--- a/app/models/journal.rb
+++ b/app/models/journal.rb
@@ -56,6 +56,9 @@ class Journal < ApplicationRecord
# logs like the history on issue#show
scope :changing, -> { where(['version > 1']) }
+ scope :for_wiki_content, -> { where(journable_type: "WikiContent") }
+ scope :for_work_package, -> { where(journable_type: "WorkPackage") }
+
# In conjunction with the included Comparable module, allows comparison of journal records
# based on their corresponding version numbers, creation timestamps and IDs.
def <=>(other)
@@ -92,11 +95,11 @@ class Journal < ApplicationRecord
end
def new_value_for(prop)
- details[prop].last if details.keys.include? prop
+ details[prop].last if details.key? prop
end
def old_value_for(prop)
- details[prop].first if details.keys.include? prop
+ details[prop].first if details.key? prop
end
def previous
diff --git a/app/models/journal/project_journal.rb b/app/models/journal/project_journal.rb
new file mode 100644
index 00000000000..e4086e62251
--- /dev/null
+++ b/app/models/journal/project_journal.rb
@@ -0,0 +1,31 @@
+#-- 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.
+#++
+
+class Journal::ProjectJournal < Journal::BaseJournal
+ self.table_name = 'project_journals'
+end
diff --git a/app/models/project.rb b/app/models/project.rb
index b06ce748d19..b77a895e1e8 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -96,6 +96,8 @@ class Project < ApplicationRecord
project_key: 'id',
permission: nil
+ include Projects::Journalized
+
# Necessary for acts_as_searchable which depends on the event_datetime method for sorting
acts_as_event title: Proc.new { |o| "#{Project.model_name.human}: #{o.name}" },
url: Proc.new { |o| { controller: 'overviews/overviews', action: 'show', project_id: o } },
diff --git a/app/models/projects/journalized.rb b/app/models/projects/journalized.rb
new file mode 100644
index 00000000000..749a1b0b178
--- /dev/null
+++ b/app/models/projects/journalized.rb
@@ -0,0 +1,40 @@
+#-- 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 Projects::Journalized
+ extend ActiveSupport::Concern
+
+ included do
+ acts_as_journalized
+ end
+
+ # override acts_as_journalized method
+ def activity_type
+ 'project_attributes'
+ end
+end
diff --git a/db/migrate/20221130150352_create_project_journals.rb b/db/migrate/20221130150352_create_project_journals.rb
new file mode 100644
index 00000000000..216466982b8
--- /dev/null
+++ b/db/migrate/20221130150352_create_project_journals.rb
@@ -0,0 +1,15 @@
+class CreateProjectJournals < ActiveRecord::Migration[7.0]
+ # rubocop:disable Rails/CreateTableWithTimestamps(RuboCop)
+ def change
+ create_table :project_journals do |t|
+ t.string :name, null: false
+ t.text :description
+ t.boolean :public, null: false
+ t.bigint :parent_id
+ t.string :identifier
+ t.boolean :active, null: false
+ t.boolean :templated, null: false
+ end
+ end
+ # rubocop:enable Rails/CreateTableWithTimestamps(RuboCop)
+end
diff --git a/spec/controllers/work_packages/bulk_controller_spec.rb b/spec/controllers/work_packages/bulk_controller_spec.rb
index fb24a9f7963..c5054e69019 100644
--- a/spec/controllers/work_packages/bulk_controller_spec.rb
+++ b/spec/controllers/work_packages/bulk_controller_spec.rb
@@ -324,7 +324,7 @@ describe WorkPackages::BulkController, type: :controller, with_settings: { journ
it { expect(response.response_code).to eq(403) }
describe '#journal' do
- subject { Journal.count }
+ subject { Journal.for_work_package.count }
it { is_expected.to eq(work_package_ids.count) }
end
diff --git a/spec/models/projects/project_acts_as_journalized_spec.rb b/spec/models/projects/project_acts_as_journalized_spec.rb
new file mode 100644
index 00000000000..f16ff64940f
--- /dev/null
+++ b/spec/models/projects/project_acts_as_journalized_spec.rb
@@ -0,0 +1,223 @@
+#-- 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 'spec_helper'
+
+describe Project, 'acts_as_journalized' do
+ shared_let(:user) { create(:user) }
+
+ let!(:project) do
+ User.execute_as user do
+ create(:project,
+ description: 'project description')
+ end
+ end
+
+ context 'on project creation' do
+ it 'has one journal entry' do
+ expect(Journal.all.count).to eq(1)
+ expect(Journal.first.journable).to eq(project)
+ end
+
+ it 'notes the changes to name' do
+ expect(Journal.first.details[:name])
+ .to match_array [nil, project.name]
+ end
+
+ it 'notes the changes to description' do
+ expect(Journal.first.details[:description])
+ .to match_array [nil, project.description]
+ end
+
+ it 'notes the changes to public flag' do
+ expect(Journal.first.details[:public])
+ .to match_array [nil, project.public]
+ end
+
+ it 'notes the changes to identifier' do
+ expect(Journal.first.details[:identifier])
+ .to match_array [nil, project.identifier]
+ end
+
+ it 'notes the changes to active flag' do
+ expect(Journal.first.details[:active])
+ .to match_array [nil, project.active]
+ end
+
+ it 'notes the changes to template flag' do
+ expect(Journal.first.details[:templated])
+ .to match_array [nil, project.templated]
+ end
+
+ it 'has the timestamp of the project update time for created_at' do
+ expect(Journal.first.created_at)
+ .to eql(project.reload.updated_at)
+ end
+ end
+
+ context 'when nothing is changed' do
+ it { expect { project.save! }.not_to change(Journal, :count) }
+ end
+
+ describe 'on project update', with_settings: { journal_aggregation_time_minutes: 0 } do
+ shared_let(:parent_project) { create(:project) }
+
+ before do
+ project.name = 'changed project name'
+ project.description = 'changed project description'
+ project.public = !project.public
+ project.parent = parent_project
+ project.identifier = 'changed-identifier'
+ project.active = !project.active
+ project.templated = !project.templated
+
+ project.save!
+ end
+
+ context 'for last created journal' do
+ it 'has the timestamp of the project update time for created_at' do
+ expect(project.last_journal.created_at)
+ .to eql(project.reload.updated_at)
+ end
+
+ it 'contains last changes' do
+ %i[name description public parent_id identifier active templated].each do |prop|
+ expect(project.last_journal.details).to have_key(prop.to_s), "Missing change for #{prop}"
+ end
+ end
+ end
+ end
+
+ describe 'custom values', with_settings: { journal_aggregation_time_minutes: 0 } do
+ shared_let(:custom_field) { create(:string_project_custom_field) }
+ let(:custom_value) do
+ build(:custom_value,
+ value: 'some string value for project custom field',
+ custom_field:)
+ end
+ let(:custom_field_id) { "custom_fields_#{custom_value.custom_field_id}" }
+
+ shared_context 'for project with new custom value' do
+ before do
+ project.update(custom_values: [custom_value])
+ end
+ end
+
+ context 'for new custom value' do
+ include_context 'for project with new custom value'
+
+ it 'contains the new custom value change' do
+ expect(project.last_journal.details)
+ .to include(custom_field_id => [nil, custom_value.value])
+ end
+ end
+
+ context 'for updated custom value' do
+ include_context 'for project with new custom value'
+
+ let(:modified_custom_value) do
+ build(:custom_value,
+ value: 'some modified value for project custom field',
+ custom_field:)
+ end
+
+ before do
+ project.update(custom_values: [modified_custom_value])
+ end
+
+ it 'contains the change from previous value to updated value' do
+ expect(project.last_journal.details)
+ .to include(custom_field_id => [custom_value.value, modified_custom_value.value])
+ end
+ end
+
+ context 'when project saved without any changes' do
+ include_context 'for project with new custom value'
+
+ let(:unmodified_custom_value) do
+ build(:custom_value,
+ value: custom_value.value,
+ custom_field:)
+ end
+
+ before do
+ project.custom_values = [unmodified_custom_value]
+ end
+
+ it { expect { project.save! }.not_to change(Journal, :count) }
+ end
+
+ context 'when custom value removed' do
+ include_context 'for project with new custom value'
+
+ before do
+ project.update(custom_values: [])
+ end
+
+ it 'contains the change from previous value to nil' do
+ expect(project.last_journal.details)
+ .to include(custom_field_id => [custom_value.value, nil])
+ end
+ end
+ end
+
+ describe 'on project deletion' do
+ shared_let(:custom_field) { create(:string_project_custom_field) }
+ let(:custom_value) do
+ build(:custom_value,
+ value: 'some string value for project custom field',
+ custom_field:)
+ end
+ let!(:project) do
+ User.execute_as user do
+ create(:project, custom_values: [custom_value])
+ end
+ end
+ let!(:journal) { project.last_journal }
+ let!(:customizable_journals) { journal.customizable_journals }
+
+ before do
+ project.destroy
+ end
+
+ it 'removes the journal' do
+ expect(Journal.find_by(id: journal.id))
+ .to be_nil
+ end
+
+ it 'removes the journal data' do
+ expect(Journal::ProjectJournal.find_by(id: journal.data_id))
+ .to be_nil
+ end
+
+ it 'removes the customizable journals' do
+ expect(Journal::CustomizableJournal.find_by(id: customizable_journals.map(&:id)))
+ .to be_nil
+ end
+ end
+end
diff --git a/spec/models/wiki_page_spec.rb b/spec/models/wiki_page_spec.rb
index b23bd62c7ef..af58865a28e 100644
--- a/spec/models/wiki_page_spec.rb
+++ b/spec/models/wiki_page_spec.rb
@@ -133,7 +133,7 @@ describe WikiPage, type: :model do
end
it 'destroys the wiki content\'s journals as well' do
- expect { wiki_page.destroy }.to change(Journal, :count).from(1).to(0)
+ expect { wiki_page.destroy }.to change(Journal.for_wiki_content, :count).from(1).to(0)
end
end
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 aa4f361776c..71e6d886624 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
@@ -52,29 +52,29 @@ describe WorkPackage, type: :model do
current_user { create(:user) }
context 'for work package creation' do
- it { expect(Journal.all.count).to eq(1) }
+ it { expect(Journal.for_work_package.count).to eq(1) }
it 'has a journal entry' do
- expect(Journal.first.journable).to eq(work_package)
+ expect(Journal.for_work_package.first.journable).to eq(work_package)
end
it 'notes the changes to subject' do
- expect(Journal.first.details[:subject])
+ expect(work_package.last_journal.details[:subject])
.to match_array [nil, work_package.subject]
end
it 'notes the changes to project' do
- expect(Journal.first.details[:project_id])
+ expect(work_package.last_journal.details[:project_id])
.to match_array [nil, work_package.project_id]
end
it 'notes the description' do
- expect(Journal.first.details[:description])
+ expect(work_package.last_journal.details[:description])
.to match_array [nil, work_package.description]
end
it 'notes the scheduling mode' do
- expect(Journal.first.details[:schedule_manually])
+ expect(work_package.last_journal.details[:schedule_manually])
.to match_array [nil, false]
end
@@ -82,7 +82,7 @@ describe WorkPackage, type: :model do
# This seemingly unnecessary reload leads to the updated_at having the same
# precision as the created_at of the Journal. It is database dependent, so it would work without
# reload on PG 12 but does not work on PG 9.
- expect(Journal.first.created_at)
+ expect(work_package.last_journal.created_at)
.to eql(work_package.reload.updated_at)
end
end
@@ -113,13 +113,13 @@ describe WorkPackage, type: :model do
end
describe 'does not track the changed newline characters' do
- subject { work_package1.journals.last.data.description }
+ subject { work_package1.last_journal.data.description }
it { is_expected.to eq(description) }
end
describe 'tracks only the other change' do
- subject { work_package1.journals.last.details }
+ subject { work_package1.last_journal.details }
it { is_expected.to have_key :subject }
it { is_expected.not_to have_key :description }
@@ -142,7 +142,7 @@ describe WorkPackage, type: :model do
description: changed_description))
end
- subject { work_package1.journals.reload.last.details }
+ subject { work_package1.last_journal.details }
it { is_expected.not_to have_key :description }
end
@@ -180,7 +180,7 @@ describe WorkPackage, type: :model do
end
context 'for last created journal' do
- subject { work_package.journals.reload.last.details }
+ subject { work_package.last_journal.details }
it 'contains all changes' do
%i(subject description type_id status_id priority_id
@@ -267,7 +267,7 @@ describe WorkPackage, type: :model do
end
it 'creates a journal for the last change' do
- last_journal = work_package.journals.order(:id).last
+ last_journal = work_package.last_journal
expect(last_journal.data.description).to eql('description v4')
end
@@ -277,7 +277,7 @@ describe WorkPackage, type: :model do
# This seemingly unnecessary reload leads to the updated_at having the same
# precision as the created_at of the Journal. It is database dependent, so it would work without
# reload on PG 12 but does not work on PG 9.
- expect(work_package.journals.order(:id).last.created_at)
+ expect(work_package.last_journal.created_at)
.to eql(work_package.reload.updated_at)
end
end
@@ -292,7 +292,7 @@ describe WorkPackage, type: :model do
end
context 'for new attachment' do
- subject { work_package.journals.reload.last.details }
+ subject { work_package.last_journal.details }
it { is_expected.to have_key attachment_id }
@@ -327,7 +327,7 @@ describe WorkPackage, type: :model do
context 'for new custom value' do
include_context 'for work package with custom value'
- subject { work_package.journals.reload.last.details }
+ subject { work_package.last_journal.details }
it { is_expected.to have_key custom_field_id }
@@ -348,7 +348,7 @@ describe WorkPackage, type: :model do
work_package.save!
end
- subject { work_package.journals.reload.last.details }
+ subject { work_package.last_journal.details }
it { is_expected.to have_key custom_field_id }
@@ -379,7 +379,7 @@ describe WorkPackage, type: :model do
work_package.save!
end
- subject { work_package.journals.last.details }
+ subject { work_package.last_journal.details }
it { is_expected.to have_key custom_field_id }
@@ -403,13 +403,13 @@ describe WorkPackage, type: :model do
describe 'empty values are recognized as unchanged' do
include_context 'for work package with custom value'
- it { expect(work_package.journals.reload.last.customizable_journals).to be_empty }
+ it { expect(work_package.last_journal.customizable_journals).to be_empty }
end
describe 'empty values handled as non existing' do
include_context 'for work package with custom value'
- it { expect(work_package.journals.reload.last.customizable_journals.count).to eq(0) }
+ it { expect(work_package.last_journal.customizable_journals.count).to eq(0) }
end
end
end
@@ -421,7 +421,7 @@ describe WorkPackage, type: :model do
end
it 'has the timestamp of the work package update time for created_at' do
- expect(work_package.journals.last.updated_at)
+ expect(work_package.last_journal.updated_at)
.to eql(work_package.updated_at)
end
end
@@ -434,7 +434,7 @@ describe WorkPackage, type: :model do
end
it 'has the timestamp of the work package update time for created_at' do
- expect(work_package.journals.last.updated_at)
+ expect(work_package.last_journal.updated_at)
.to eql(work_package.updated_at)
end
end
@@ -592,8 +592,8 @@ describe WorkPackage, type: :model do
subject(:journals) { work_package.journals }
before do
- work_package.journals.last.update_columns(created_at: 2.minutes.ago,
- updated_at: 2.minutes.ago)
+ work_package.last_journal.update_columns(created_at: 2.minutes.ago,
+ updated_at: 2.minutes.ago)
work_package.status = build(:status)
work_package.save!
diff --git a/spec/requests/api/v3/work_packages/by_project_index_resource_spec.rb b/spec/requests/api/v3/work_packages/by_project_index_resource_spec.rb
index 02f4fef57dc..2771690a1ef 100644
--- a/spec/requests/api/v3/work_packages/by_project_index_resource_spec.rb
+++ b/spec/requests/api/v3/work_packages/by_project_index_resource_spec.rb
@@ -56,7 +56,7 @@ describe API::V3::WorkPackages::WorkPackagesByProjectAPI, type: :request, conten
end
context 'when not allowed to see the project' do
- let(:current_user) { build(:user) }
+ let(:current_user) { create(:user) }
it 'fails with HTTP Not Found' do
expect(subject.status).to eq 404
diff --git a/spec/requests/api/v3/work_packages/create_project_form_resource_spec.rb b/spec/requests/api/v3/work_packages/create_project_form_resource_spec.rb
index 3c9025f6d8c..69176150d9a 100644
--- a/spec/requests/api/v3/work_packages/create_project_form_resource_spec.rb
+++ b/spec/requests/api/v3/work_packages/create_project_form_resource_spec.rb
@@ -34,7 +34,7 @@ describe ::API::V3::WorkPackages::CreateProjectFormAPI, content_type: :json do
let(:project) { create(:project, id: 5) }
let(:post_path) { api_v3_paths.create_project_work_package_form(project.id) }
- let(:user) { build(:admin) }
+ let(:user) { create(:admin) }
before do
login_as(user)