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

Makes WorkPackage aware of file links

Marcello Rocha29975875e3a5
diff --git a/app/models/journal.rb b/app/models/journal.rb
index cc12932cced..a242b38f787 100644
--- a/app/models/journal.rb
+++ b/app/models/journal.rb
@@ -51,6 +51,7 @@ class Journal < ApplicationRecord
register_journal_formatter :wiki_diff, OpenProject::JournalFormatter::WikiDiff
register_journal_formatter :time_entry_named_association, OpenProject::JournalFormatter::TimeEntryNamedAssociation
register_journal_formatter :cause, OpenProject::JournalFormatter::Cause
+ register_journal_formatter :file_link, OpenProject::JournalFormatter::FileLink
# Attributes related to the cause are stored in a JSONB column so we can easily add new relations and related
# attributes without a heavy database migration. Fields will be prefixed with `cause_` but are stored in the JSONB
@@ -74,6 +75,7 @@ class Journal < ApplicationRecord
has_many :attachable_journals, class_name: 'Journal::AttachableJournal', dependent: :delete_all
has_many :customizable_journals, class_name: 'Journal::CustomizableJournal', dependent: :delete_all
+ has_many :storable_journals, class_name: 'Journal::StorableJournal', dependent: :delete_all
has_many :notifications, dependent: :destroy
diff --git a/app/models/work_package/journalized.rb b/app/models/work_package/journalized.rb
index 6e1f4c624c9..fda25523507 100644
--- a/app/models/work_package/journalized.rb
+++ b/app/models/work_package/journalized.rb
@@ -86,6 +86,7 @@ module WorkPackage::Journalized
register_journal_formatted_fields(:custom_field, /custom_fields_\d+/)
register_journal_formatted_fields(:ignore_non_working_days, 'ignore_non_working_days')
register_journal_formatted_fields(:cause, 'cause')
+ register_journal_formatted_fields(:file_link, /file_links_?\d+/)
# Joined
register_journal_formatted_fields :named_association, :parent_id, :project_id,
diff --git a/lib/open_project/journal_formatter/file_link.rb b/lib/open_project/journal_formatter/file_link.rb
new file mode 100644
index 00000000000..7d546a36457
--- /dev/null
+++ b/lib/open_project/journal_formatter/file_link.rb
@@ -0,0 +1,58 @@
+# frozen_string_literal: true
+
+#-- 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 OpenProject::JournalFormatter::FileLink < JournalFormatter::Base
+ include OpenProject::ObjectLinking
+
+ def render(key, values, options = { html: true })
+ id = key.to_s.sub('file_links_', '')
+ label, old_value, value = format_details(id, values)
+
+ if options[:html]
+ label, old_value, value = *format_html_details(label, old_value, value)
+ value = format_html_file_link_detail(id, value)
+ end
+
+ render_binary_detail_text(label, value, old_value)
+ end
+
+ private
+
+ # Based this off the Attachment formatter. Not sure if it is the best approach
+ def label(_key) = Storages::FileLink.model_name.human
+
+ def format_html_file_link_detail(key, value)
+ if value.present? && a = ::Storages::FileLink.find_by(id: key.to_i)
+ link_to_file_link(a, only_path: false)
+ elsif value.present?
+ value
+ end
+ end
+end
diff --git a/lib/open_project/object_linking.rb b/lib/open_project/object_linking.rb
index c8fc0fc3072..4cc04444ee6 100644
--- a/lib/open_project/object_linking.rb
+++ b/lib/open_project/object_linking.rb
@@ -79,6 +79,14 @@ module OpenProject
options
end
+ def link_to_file_link(file_link, options = {})
+ text = options.delete(:text) || file_link.origin_name
+
+ link_to text,
+ url_to_file_link(file_link, only_path: options.delete(:only_path) { true }),
+ options
+ end
+
# Generates a link to a SCM revision
# Options:
# * :text - Link text (default to the formatted revision)
@@ -147,5 +155,15 @@ module OpenProject
v3_paths.url_for(:attachment_content, attachment.id)
end
end
+
+ def url_to_file_link(file_link, only_path: true)
+ v3_paths = API::V3::Utilities::PathHelper::ApiV3Path
+
+ if only_path
+ v3_paths.file_link_open(file_link.id)
+ else
+ v3_paths.url_for(:file_link_open, file_link.id)
+ end
+ end
end
end
diff --git a/modules/storages/config/locales/en.yml b/modules/storages/config/locales/en.yml
index d6db727f8bd..57fde3fc3ea 100644
--- a/modules/storages/config/locales/en.yml
+++ b/modules/storages/config/locales/en.yml
@@ -12,6 +12,7 @@ en:
activerecord:
models:
+ file_link: "File link"
storages/storage: "Storage"
attributes:
storages/storage:
diff --git a/spec/lib/journal_formatter/file_link_spec.rb b/spec/lib/journal_formatter/file_link_spec.rb
new file mode 100644
index 00000000000..ed09fa58b40
--- /dev/null
+++ b/spec/lib/journal_formatter/file_link_spec.rb
@@ -0,0 +1,96 @@
+# frozen_string_literal: true
+
+#-- 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 OpenProject::JournalFormatter::FileLink do
+ let(:work_package) { create(:work_package) }
+ let(:journal) { instance_double(Journal, journable: work_package) }
+ let(:file_link) { create(:file_link, container: work_package) }
+ let(:key) { "file_links_#{file_link.id}" }
+
+ subject(:instance) { described_class.new(journal) }
+
+ describe '#render' do
+ context 'having the first value being nil, and the second an file link name as string' do
+ context 'as HTML' do
+ it 'adds a file link added text' do
+ link = "#{Setting.protocol}://#{Setting.host_name}/api/v3/file_links/#{file_link.id}/open"
+ expect(instance.render(key, [nil, file_link.origin_name]))
+ .to eq(I18n.t(:text_journal_added,
+ label: "<strong>#{I18n.t(:'activerecord.models.file_link')}</strong>",
+ value: "<a href=\"#{link}\">#{file_link.origin_name}</a>"))
+ end
+
+ context 'with a configured relative url root' do
+ before { allow(OpenProject::Configuration).to receive(:rails_relative_url_root).and_return('/blubs') }
+
+ it 'adds an file link added text' do
+ link = "#{Setting.protocol}://#{Setting.host_name}/blubs/api/v3/file_links/#{file_link.id}/open"
+ expect(instance.render(key, [nil, file_link.origin_name]))
+ .to eq(I18n.t(:text_journal_added,
+ label: "<strong>#{I18n.t(:'activerecord.models.file_link')}</strong>",
+ value: "<a href=\"#{link}\">#{file_link.origin_name}</a>"))
+ end
+ end
+ end
+
+ context 'as plain text' do
+ it 'adds a file link added text' do
+ message = I18n.t(:text_journal_added,
+ label: I18n.t(:'activerecord.models.file_link'),
+ value: file_link.id)
+
+ expect(instance.render(key, [nil, file_link.id.to_s], html: false)).to eq(message)
+ end
+ end
+ end
+
+ context 'having the first value being an id as string, and the second nil' do
+ context 'as HTML' do
+ it 'adds a file link remove text' do
+ message = I18n.t(:text_journal_deleted,
+ label: "<strong>#{I18n.t(:'activerecord.models.file_link')}</strong>",
+ old: "<strike><i>#{file_link.id}</i></strike>")
+
+ expect(instance.render(key, [file_link.id.to_s, nil])).to eq(message)
+ end
+ end
+
+ context 'as plain text' do
+ it 'adds a file link removed' do
+ message = I18n.t(:text_journal_deleted, label: Storages::FileLink.model_name.human, old: file_link.id)
+
+ expect(instance.render(key, [file_link.id.to_s, nil], html: false)).to eq(message)
+ end
+ end
+ end
+ 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 f94a5a41cf9..28cf4dc328c 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
@@ -414,6 +414,27 @@ RSpec.describe WorkPackage do
end
end
+ describe 'file_links', with_settings: { journal_aggregation_time_minutes: 0 } do
+ let(:file_link) { build(:file_link) }
+ let(:file_link_id) { "file_links_#{file_link.id}" }
+
+ before do
+ work_package.file_links << file_link
+ work_package.save!
+ end
+
+ context 'for the new file link' do
+ subject(:journal_details) { work_package.last_journal.details }
+
+ it { is_expected.to have_key file_link_id }
+ it { expect(journal_details[file_link_id]).to eq([nil, file_link.origin_name]) }
+ end
+
+ context 'when attachment saved w/o change' do
+ it { expect { file_link.save! }.not_to change(Journal, :count) }
+ end
+ end
+
context 'for only journal notes adding' do
subject do
work_package.add_journal(user: User.current, notes: 'some notes')
@@ -741,12 +762,16 @@ RSpec.describe WorkPackage do
project:,
type:,
custom_field_values: { custom_field.id => 5 },
- attachments: [attachment])
+ attachments: [attachment],
+ file_links: [file_link])
end
let(:attachment) { build(:attachment) }
+ let(:file_link) { build(:file_link) }
+
let!(:journal) { work_package.journals.first }
let!(:customizable_journals) { journal.customizable_journals }
let!(:attachable_journals) { journal.attachable_journals }
+ let!(:storable_journals) { journal.storable_journals }
before do
work_package.destroy
@@ -771,5 +796,10 @@ RSpec.describe WorkPackage do
expect(Journal::AttachableJournal.find_by(id: attachable_journals.map(&:id)))
.to be_nil
end
+
+ it 'removes the storable journals' do
+ expect(Journal::StorableJournal.find_by(id: attachable_journals.map(&:id)))
+ .to be_nil
+ end
end
end