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

Set snapshot through attachment service

Oliver Günther9e1dc1d50e4e
diff --git a/app/services/attachments/build_service.rb b/app/services/attachments/build_service.rb
index 937efe160d9..9a725f571bf 100644
--- a/app/services/attachments/build_service.rb
+++ b/app/services/attachments/build_service.rb
@@ -31,12 +31,21 @@ module Attachments
private
def 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.errors
service_result.success = false
end
service_result
end
+
+ def add_to_association(attachment)
+ return unless attachment.container
+
+ attachment.container.association(:attachments).add_to_target(attachment)
+ end
end
end
diff --git a/modules/bim/app/contracts/bim/bcf/viewpoints/create_contract.rb b/modules/bim/app/contracts/bim/bcf/viewpoints/create_contract.rb
index 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::Bcf
attribute :uuid
attribute :issue
+ attribute :snapshot
attribute :json_viewpoint do
validate_json_viewpoint_present
validate_json_viewpoint_hash
diff --git a/modules/bim/app/models/bim/bcf/viewpoint.rb b/modules/bim/app/models/bim/bcf/viewpoint.rb
index 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::Bcf
def 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')
.result
end
diff --git a/modules/bim/app/services/bim/bcf/viewpoints/set_attributes_service.rb b/modules/bim/app/services/bim/bcf/viewpoints/set_attributes_service.rb
index 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::Bcf
def set_snapshot
return 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: user
end
def 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.rb
index 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 Bim
model.attachments << ifc_attachment
else
- 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')
end
end
end
diff --git a/modules/bim/spec/services/bcf/viewpoints/set_attributes_service_spec.rb b/modules/bim/spec/services/bcf/viewpoints/set_attributes_service_spec.rb
index 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 do
Bim::Bcf::Viewpoint.new
end
+ 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 do
expect(viewpoint.attachments.first.file.read)
.to eql 'Hello World!'
- expect(viewpoint.attachments.first.file.filename)
+ expect(viewpoint.attachments.first.filename)
.to eql 'snapshot.png'
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 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 do
context 'for a new record' do
let(:model) do
- Bim::IfcModels::IfcModel.new
+ Bim::IfcModels::IfcModel.new project: project
end
context 'with an ifc_attachment' do