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

Replace ifc models attachment handling with services

Oliver Günther582579729edf
diff --git a/app/services/attachments/build_service.rb b/app/services/attachments/build_service.rb
new file mode 100644
index 00000000000..937efe160d9
--- /dev/null
+++ b/app/services/attachments/build_service.rb
@@ -0,0 +1,42 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 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 docs/COPYRIGHT.rdoc for more details.
+#++
+
+module Attachments
+ class BuildService < ::BaseServices::Create
+ private
+
+ def persist(service_result)
+ unless service_result.result.valid?
+ service_result.errors = service_result.result.errors
+ service_result.success = false
+ end
+
+ service_result
+ end
+ end
+end
diff --git a/app/services/attachments/create_service.rb b/app/services/attachments/create_service.rb
index 33f00f1586c..772b5ef3f2d 100644
--- a/app/services/attachments/create_service.rb
+++ b/app/services/attachments/create_service.rb
@@ -68,6 +68,7 @@ class Attachments::CreateService < ::BaseServices::Create
def error_wrapped_call
yield
rescue StandardError => e
+ binding.pry
log_attachment_saving_error(e)
message =
diff --git a/modules/bim/app/models/bim/ifc_models/ifc_model.rb b/modules/bim/app/models/bim/ifc_models/ifc_model.rb
index 1ae55c12cac..807606a8bd1 100644
--- a/modules/bim/app/models/bim/ifc_models/ifc_model.rb
+++ b/modules/bim/app/models/bim/ifc_models/ifc_model.rb
@@ -1,7 +1,9 @@
module Bim
module IfcModels
class IfcModel < ActiveRecord::Base
- acts_as_attachable delete_permission: :manage_ifc_models, view_permission: :view_ifc_models
+ acts_as_attachable delete_permission: :manage_ifc_models,
+ add_permission: :manage_ifc_models,
+ view_permission: :view_ifc_models
belongs_to :project
belongs_to :uploader, class_name: 'User', foreign_key: 'uploader_id'
@@ -23,7 +25,11 @@ module Bim
end
delete_attachment name
- attach_files('first' => { 'file' => file, 'description' => name })
+ call = ::Attachments::CreateService
+ .new(user: User.current)
+ .call(file: file, container: self, filename: file.original_filename, description: name)
+
+ call.on_failure { Rails.logger.error "Failed to add #{name} attachment: #{call.message}" }
end
end
diff --git a/modules/bim/app/services/bim/ifc_models/set_attributes_service.rb b/modules/bim/app/services/bim/ifc_models/set_attributes_service.rb
index 80661c043bb..0c070c89696 100644
--- a/modules/bim/app/services/bim/ifc_models/set_attributes_service.rb
+++ b/modules/bim/app/services/bim/ifc_models/set_attributes_service.rb
@@ -39,7 +39,7 @@ module Bim
super
model.change_by_system do
- model.uploader = model.ifc_attachment&.author if model.ifc_attachment&.new_record? || model.ifc_attachment&.pending_direct_upload?
+ model.uploader = model.ifc_attachment&.author
end
end
@@ -75,7 +75,11 @@ module Bim
model.attachments << ifc_attachment
else
- model.attach_files('first' => { 'file' => ifc_attachment, 'description' => 'ifc' })
+ call = ::Attachments::BuildService
+ .new(user: user)
+ .call(file: ifc_attachment, filename: ifc_attachment.original_filename, description: 'ifc')
+
+ model.association(:attachments).add_to_target(call.result)
end
end
end
diff --git a/modules/bim/spec/bcf/bcf_xml/issue_writer_spec.rb b/modules/bim/spec/bcf/bcf_xml/issue_writer_spec.rb
index de0e0c659e5..f23db8f7932 100644
--- a/modules/bim/spec/bcf/bcf_xml/issue_writer_spec.rb
+++ b/modules/bim/spec/bcf/bcf_xml/issue_writer_spec.rb
@@ -103,9 +103,7 @@ describe ::OpenProject::Bim::BcfXml::IssueWriter do
before do
allow(User).to receive(:current).and_return current_user
-
- bcf_issue.comments.first.journal.update_attribute('journable_id', work_package.id)
- FactoryBot.create(:work_package_journal, notes: "Some note created in OP.", journable_id: work_package.id)
+ bcf_issue.comments.first.journal.update_columns(journable_id: work_package.id, version: 2)
end
shared_examples_for "writes Topic" do
@@ -174,6 +172,9 @@ describe ::OpenProject::Bim::BcfXml::IssueWriter do
end
it 'creates BCF comments for comments that were created within OP.' do
+ work_package.journal_notes = 'Some note created in OP.'
+ work_package.save!
+
expect(subject.at('/Markup/Comment[2]/Comment').content).to eql("Some note created in OP.")
expect(Bim::Bcf::Comment.count).to eql(2)
end
diff --git a/modules/bim/spec/factories/ifc_model_factory.rb b/modules/bim/spec/factories/ifc_model_factory.rb
index 3162fd232cb..afa99ca606f 100644
--- a/modules/bim/spec/factories/ifc_model_factory.rb
+++ b/modules/bim/spec/factories/ifc_model_factory.rb
@@ -32,11 +32,19 @@ FactoryBot.define do
project factory: :project
uploader factory: :user
is_default { true }
- ifc_attachment do
- Rack::Test::UploadedFile.new(
- File.join(Rails.root, "modules/bim/spec/fixtures/files/minimal.ifc"),
- 'application/binary'
- )
+ transient do
+ ifc_attachment do
+ Rack::Test::UploadedFile.new(
+ File.join(Rails.root, "modules/bim/spec/fixtures/files/minimal.ifc"),
+ 'application/binary'
+ )
+ end
+
+ callback(:after_create) do |model, evaluator|
+ User.system.run_given do
+ model.ifc_attachment = evaluator.ifc_attachment
+ end
+ end
end
factory :ifc_model_minimal_converted do
@@ -44,12 +52,19 @@ FactoryBot.define do
project factory: :project
uploader factory: :user
is_default { true }
+ transient do
+ xkt_attachment do
+ Rack::Test::UploadedFile.new(
+ File.join(Rails.root, "modules/bim/spec/fixtures/files/minimal.xkt"),
+ 'application/binary'
+ )
+ end
+ end
- xkt_attachment do
- Rack::Test::UploadedFile.new(
- File.join(Rails.root, "modules/bim/spec/fixtures/files/minimal.xkt"),
- 'application/binary'
- )
+ callback(:after_create) do |model, evaluator|
+ User.system.run_given do
+ model.xkt_attachment = evaluator.xkt_attachment
+ end
end
end
end
diff --git a/modules/bim/spec/features/bim_navigation_spec.rb b/modules/bim/spec/features/bim_navigation_spec.rb
index e444666caff..0f2a3eb0064 100644
--- a/modules/bim/spec/features/bim_navigation_spec.rb
+++ b/modules/bim/spec/features/bim_navigation_spec.rb
@@ -44,7 +44,7 @@ describe 'BIM navigation spec',
member_through_role: role
end
- let!(:model) do
+ let(:model) do
FactoryBot.create(:ifc_model_minimal_converted,
project: project,
uploader: user)
@@ -56,16 +56,19 @@ describe 'BIM navigation spec',
let(:model_tree) { ::Components::XeokitModelTree.new }
let(:destroy_modal) { Components::WorkPackages::DestroyModal.new }
+ before do
+ login_as user
+ model
+ end
+
shared_examples 'can switch from split to viewer to list-only' do
before do
- login_as(user)
model_page.visit!
model_page.finished_loading
end
context 'deep link on the page' do
before do
- login_as(user)
model_page.visit!
model_page.finished_loading
diff --git a/modules/bim/spec/features/model_management_spec.rb b/modules/bim/spec/features/model_management_spec.rb
index 79049dce541..e45344f7570 100644
--- a/modules/bim/spec/features/model_management_spec.rb
+++ b/modules/bim/spec/features/model_management_spec.rb
@@ -44,14 +44,14 @@ describe 'model management',
member_through_role: role
end
- let(:model) do
+ let!(:model) do
FactoryBot.create(:ifc_model_minimal_converted,
project: project,
uploader: user,
is_default: true)
end
- let(:model2) do
+ let!(:model2) do
FactoryBot.create(:ifc_model_minimal_converted,
project: project,
uploader: user)
diff --git a/modules/bim/spec/features/model_viewer_spec.rb b/modules/bim/spec/features/model_viewer_spec.rb
index 44ea7c9868d..66eb1932a89 100644
--- a/modules/bim/spec/features/model_viewer_spec.rb
+++ b/modules/bim/spec/features/model_viewer_spec.rb
@@ -43,7 +43,7 @@ describe 'model viewer',
member_through_role: role
end
- let(:model) do
+ let!(:model) do
FactoryBot.create(:ifc_model_minimal_converted,
project: project,
uploader: user)
@@ -92,6 +92,8 @@ describe 'model viewer',
end
context 'in a project with no model' do
+ let!(:model) { nil }
+
it 'shows a warning that no IFC models exist yet' do
login_as user
visit defaults_bcf_project_ifc_models_path(project)
diff --git a/modules/bim/spec/models/ifc_model_spec.rb b/modules/bim/spec/models/ifc_model_spec.rb
index 4dc28cbc1d5..6542dc65d66 100644
--- a/modules/bim/spec/models/ifc_model_spec.rb
+++ b/modules/bim/spec/models/ifc_model_spec.rb
@@ -16,11 +16,17 @@ describe ::Bim::IfcModels::IfcModel, type: :model do
end
describe 'ifc_attachment=' do
- subject { FactoryBot.create :ifc_model_minimal_converted, project: FactoryBot.create(:project) }
+ let(:project) { FactoryBot.create(:project, enabled_module_names: %i[bim]) }
let(:ifc_attachment) { subject.ifc_attachment }
let(:new_attachment) do
FileHelpers.mock_uploaded_file name: "model.ifc", content_type: 'application/binary', binary: true
end
+ subject { FactoryBot.create :ifc_model_minimal_converted, project: project }
+ current_user do
+ FactoryBot.create :user,
+ member_in_project: project,
+ member_with_permissions: %i[manage_ifc_models]
+ end
it 'replaces the previous attachment' do
expect(ifc_attachment).to be_present
@@ -31,7 +37,7 @@ describe ::Bim::IfcModels::IfcModel, type: :model do
expect { ifc_attachment.reload }.to raise_error(ActiveRecord::RecordNotFound)
expect(subject.ifc_attachment).not_to eq ifc_attachment
- expect(subject.ifc_attachment).not_to be_present
+ expect(subject.ifc_attachment).to be_present
expect(subject.xkt_attachment).not_to be_present
expect(subject).not_to be_converted
end
diff --git a/modules/bim/spec/services/ifc_models/set_attributes_service_spec.rb b/modules/bim/spec/services/ifc_models/set_attributes_service_spec.rb
index 533d859149c..fdfbf353a2a 100644
--- a/modules/bim/spec/services/ifc_models/set_attributes_service_spec.rb
+++ b/modules/bim/spec/services/ifc_models/set_attributes_service_spec.rb
@@ -31,7 +31,10 @@
require 'spec_helper'
describe Bim::IfcModels::SetAttributesService, type: :model do
- let(:user) { FactoryBot.build_stubbed(:user) }
+ shared_let(:project) { FactoryBot.create(:project, enabled_module_names: %i[bim]) }
+ shared_let(:other_project) { FactoryBot.create(:project, enabled_module_names: %i[bim]) }
+ shared_let(:user) { FactoryBot.create(:user, member_in_project: project, member_with_permissions: %i[manage_ifc_models]) }
+
let(:other_user) { FactoryBot.build_stubbed(:user) }
let(:contract_class) do
contract = double('contract_class')
@@ -58,10 +61,8 @@ describe Bim::IfcModels::SetAttributesService, type: :model do
end
let(:call_attributes) { {} }
let(:ifc_file) { FileHelpers.mock_uploaded_file(name: "model_2.ifc", content_type: 'application/binary', binary: true) }
- let(:ifc_attachment) { FactoryBot.build_stubbed(:attachment) }
- let(:project) { FactoryBot.build_stubbed(:project) }
let(:model) do
- FactoryBot.build_stubbed(:ifc_model, attachments: [ifc_attachment], uploader: other_user)
+ FactoryBot.create(:ifc_model, project: project, uploader: other_user)
end
before do
@@ -72,7 +73,7 @@ describe Bim::IfcModels::SetAttributesService, type: :model do
describe 'call' do
let(:call_attributes) do
{
- project_id: project.id
+ project_id: other_project.id
}
end
@@ -96,7 +97,7 @@ describe Bim::IfcModels::SetAttributesService, type: :model do
subject
expect(model.attributes.slice(*model.changed).symbolize_keys)
- .to eql call_attributes
+ .to eql call_attributes.merge(uploader_id: user.id)
end
it 'does not persist the model' do
@@ -167,6 +168,8 @@ describe Bim::IfcModels::SetAttributesService, type: :model do
end
it 'marks existing attachments for destruction' do
+ ifc_attachment = model.ifc_attachment
+
subject
expect(ifc_attachment)