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
Set snapshot through attachment service
Oliver Günther9e1dc1d50e4e
diff --git a/app/services/attachments/build_service.rb b/app/services/attachments/build_service.rbindex 937efe160d9..9a725f571bf 100644--- a/app/services/attachments/build_service.rb+++ b/app/services/attachments/build_service.rb@@ -31,12 +31,21 @@ module Attachmentsprivatedef persist(service_result)- unless service_result.result.valid?- service_result.errors = service_result.result.errors+ attachment = service_result.result+ add_to_association(attachment)++ unless attachment.valid?+ service_result.errors = attachment.errorsservice_result.success = falseendservice_resultend++ def add_to_association(attachment)+ return unless attachment.container++ attachment.container.association(:attachments).add_to_target(attachment)+ endendenddiff --git a/modules/bim/app/contracts/bim/bcf/viewpoints/create_contract.rb b/modules/bim/app/contracts/bim/bcf/viewpoints/create_contract.rbindex 6f568b08231..7edea108901 100644--- a/modules/bim/app/contracts/bim/bcf/viewpoints/create_contract.rb+++ b/modules/bim/app/contracts/bim/bcf/viewpoints/create_contract.rb@@ -84,6 +84,7 @@ module Bim::Bcfattribute :uuidattribute :issue+ attribute :snapshotattribute :json_viewpoint dovalidate_json_viewpoint_presentvalidate_json_viewpoint_hashdiff --git a/modules/bim/app/models/bim/bcf/viewpoint.rb b/modules/bim/app/models/bim/bcf/viewpoint.rbindex 2112850cef8..aaa3bff718b 100644--- a/modules/bim/app/models/bim/bcf/viewpoint.rb+++ b/modules/bim/app/models/bim/bcf/viewpoint.rb@@ -41,8 +41,12 @@ module Bim::Bcfdef snapshot=(file)snapshot&.destroy- Attachments::CreateService- .new(user: User.current)+ build_snapshot file+ end++ def build_snapshot(file, user: User.current)+ ::Attachments::BuildService+ .new(user: user).call(file: file, container: self, filename: file.original_filename, description: 'snapshot').resultenddiff --git a/modules/bim/app/services/bim/bcf/viewpoints/set_attributes_service.rb b/modules/bim/app/services/bim/bcf/viewpoints/set_attributes_service.rbindex fd861246f6d..c9cce4c2ac9 100644--- a/modules/bim/app/services/bim/bcf/viewpoints/set_attributes_service.rb+++ b/modules/bim/app/services/bim/bcf/viewpoints/set_attributes_service.rb@@ -44,8 +44,9 @@ module Bim::Bcfdef set_snapshotreturn unless snapshot_data_complete? && snapshot_content_type+ name = "snapshot.#{snapshot_extension}"file = OpenProject::Files- .create_uploaded_file(name: "snapshot.#{snapshot_extension}",+ .create_uploaded_file(name: name,content_type: snapshot_content_type,content: snapshot_binary_contents,binary: true)@@ -54,7 +55,8 @@ module Bim::Bcf# to update existing viewpoints as the snapshot method will# delete any existing snapshot right away while the expectation# on a SetAttributesService is to not perform persisted changes.- model.snapshot = file+ model.snapshot&.mark_for_destruction+ model.build_snapshot file, user: userenddef snapshot_data_complete?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.rbindex 0c070c89696..b703ceee161 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@@ -75,11 +75,9 @@ module Bimmodel.attachments << ifc_attachmentelse- call = ::Attachments::BuildService+ ::Attachments::BuildService.new(user: user)- .call(file: ifc_attachment, filename: ifc_attachment.original_filename, description: 'ifc')-- model.association(:attachments).add_to_target(call.result)+ .call(file: ifc_attachment, container: model, filename: ifc_attachment.original_filename, description: 'ifc')endendenddiff --git a/modules/bim/spec/services/bcf/viewpoints/set_attributes_service_spec.rb b/modules/bim/spec/services/bcf/viewpoints/set_attributes_service_spec.rbindex 7bfc3783d4b..2a813b0b39a 100644--- a/modules/bim/spec/services/bcf/viewpoints/set_attributes_service_spec.rb+++ b/modules/bim/spec/services/bcf/viewpoints/set_attributes_service_spec.rb@@ -60,6 +60,13 @@ describe Bim::Bcf::Viewpoints::SetAttributesService, type: :model doBim::Bcf::Viewpoint.newend+ before do+ allow(viewpoint)+ .to receive(:attachments_addable?)+ .with(user, any_args)+ .and_return true+ end+describe 'call' do# We only expect the service to be called for new records. As viewpoints# are immutable.@@ -113,7 +120,7 @@ describe Bim::Bcf::Viewpoints::SetAttributesService, type: :model doexpect(viewpoint.attachments.first.file.read).to eql 'Hello World!'- expect(viewpoint.attachments.first.file.filename)+ expect(viewpoint.attachments.first.filename).to eql 'snapshot.png'enddiff --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 fdfbf353a2a..72427087cdf 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@@ -109,7 +109,7 @@ describe Bim::IfcModels::SetAttributesService, type: :model docontext 'for a new record' dolet(:model) do- Bim::IfcModels::IfcModel.new+ Bim::IfcModels::IfcModel.new project: projectendcontext 'with an ifc_attachment' do