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
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.rbnew file mode 100644index 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+enddiff --git a/app/services/attachments/create_service.rb b/app/services/attachments/create_service.rbindex 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::Createdef error_wrapped_callyieldrescue StandardError => e+ binding.prylog_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.rbindex 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 Bimmodule IfcModelsclass 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_modelsbelongs_to :projectbelongs_to :uploader, class_name: 'User', foreign_key: 'uploader_id'@@ -23,7 +25,11 @@ module Bimenddelete_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}" }endenddiff --git a/modules/bim/app/services/bim/ifc_models/set_attributes_service.rb b/modules/bim/app/services/bim/ifc_models/set_attributes_service.rbindex 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 Bimsupermodel.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&.authorendend@@ -75,7 +75,11 @@ module Bimmodel.attachments << ifc_attachmentelse- 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)endendenddiff --git a/modules/bim/spec/bcf/bcf_xml/issue_writer_spec.rb b/modules/bim/spec/bcf/bcf_xml/issue_writer_spec.rbindex 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 dobefore doallow(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)endshared_examples_for "writes Topic" do@@ -174,6 +172,9 @@ describe ::OpenProject::Bim::BcfXml::IssueWriter doendit '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)enddiff --git a/modules/bim/spec/factories/ifc_model_factory.rb b/modules/bim/spec/factories/ifc_model_factory.rbindex 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 doproject factory: :projectuploader factory: :useris_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+ endendfactory :ifc_model_minimal_converted do@@ -44,12 +52,19 @@ FactoryBot.define doproject factory: :projectuploader factory: :useris_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+ endendendenddiff --git a/modules/bim/spec/features/bim_navigation_spec.rb b/modules/bim/spec/features/bim_navigation_spec.rbindex 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: roleend- let!(:model) do+ let(:model) doFactoryBot.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' dobefore do- login_as(user)model_page.visit!model_page.finished_loadingendcontext 'deep link on the page' dobefore do- login_as(user)model_page.visit!model_page.finished_loadingdiff --git a/modules/bim/spec/features/model_management_spec.rb b/modules/bim/spec/features/model_management_spec.rbindex 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: roleend- let(:model) do+ let!(:model) doFactoryBot.create(:ifc_model_minimal_converted,project: project,uploader: user,is_default: true)end- let(:model2) do+ let!(:model2) doFactoryBot.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.rbindex 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: roleend- let(:model) do+ let!(:model) doFactoryBot.create(:ifc_model_minimal_converted,project: project,uploader: user)@@ -92,6 +92,8 @@ describe 'model viewer',endcontext 'in a project with no model' do+ let!(:model) { nil }+it 'shows a warning that no IFC models exist yet' dologin_as uservisit 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.rbindex 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 doenddescribe '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) doFileHelpers.mock_uploaded_file name: "model.ifc", content_type: 'application/binary', binary: trueend+ 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]+ endit 'replaces the previous attachment' doexpect(ifc_attachment).to be_present@@ -31,7 +37,7 @@ describe ::Bim::IfcModels::IfcModel, type: :model doexpect { 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_presentexpect(subject.xkt_attachment).not_to be_presentexpect(subject).not_to be_convertedenddiff --git a/modules/bim/spec/services/ifc_models/set_attributes_service_spec.rb b/modules/bim/spec/services/ifc_models/set_attributes_service_spec.rbindex 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) docontract = double('contract_class')@@ -58,10 +61,8 @@ describe Bim::IfcModels::SetAttributesService, type: :model doendlet(: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)endbefore do@@ -72,7 +73,7 @@ describe Bim::IfcModels::SetAttributesService, type: :model dodescribe 'call' dolet(:call_attributes) do{- project_id: project.id+ project_id: other_project.id}end@@ -96,7 +97,7 @@ describe Bim::IfcModels::SetAttributesService, type: :model dosubjectexpect(model.attributes.slice(*model.changed).symbolize_keys)- .to eql call_attributes+ .to eql call_attributes.merge(uploader_id: user.id)endit 'does not persist the model' do@@ -167,6 +168,8 @@ describe Bim::IfcModels::SetAttributesService, type: :model doendit 'marks existing attachments for destruction' do+ ifc_attachment = model.ifc_attachment+subjectexpect(ifc_attachment)