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

move most of the logic into the SetAttributesService

ulferts3a06d7d5cbc0
diff --git a/modules/ifc_models/app/services/ifc_models/create_service.rb b/modules/ifc_models/app/services/ifc_models/create_service.rb
index bbdc00408bc..d977c145c4a 100644
--- a/modules/ifc_models/app/services/ifc_models/create_service.rb
+++ b/modules/ifc_models/app/services/ifc_models/create_service.rb
@@ -30,16 +30,7 @@ module IFCModels
class CreateService < ::BaseServices::Create
protected
- attr_accessor :ifc_attachment
-
- def before_perform(params)
- self.ifc_attachment = params.delete(:ifc_attachment)
- super(params)
- end
-
def after_perform(call)
- call.success = add_attachment(call)
-
if call.success?
IFCConversionJob.perform_later(call.result)
end
@@ -50,21 +41,5 @@ module IFCModels
def instance(_params)
::IFCModels::IFCModel.new
end
-
- ##
- # Add the IFC attachment file after saving
- def add_attachment(result)
- return unless result.success?
-
- model = result.result
- if ifc_attachment && ifc_attachment.size.positive?
- model.ifc_attachment = ifc_attachment
- model.title = ifc_attachment.original_filename
- model.save
- else
- result.errors.add(:ifc_attachment, I18n.t('ifc_models.could_not_save_file'))
- false
- end
- end
end
end
diff --git a/modules/ifc_models/app/services/ifc_models/set_attributes_service.rb b/modules/ifc_models/app/services/ifc_models/set_attributes_service.rb
index d82bedfb1b7..91efd36849c 100644
--- a/modules/ifc_models/app/services/ifc_models/set_attributes_service.rb
+++ b/modules/ifc_models/app/services/ifc_models/set_attributes_service.rb
@@ -30,10 +30,44 @@
module IFCModels
class SetAttributesService < ::BaseServices::SetAttributes
+ protected
+
+ def set_attributes(params)
+ set_ifc_attachment(params.delete(:ifc_attachment))
+
+ super
+
+ change_by_system do
+ model.uploader = model.ifc_attachment&.author if model.ifc_attachment&.new_record?
+ end
+ end
+
def set_default_attributes(params)
- model.uploader = user
- model.is_default = params[:is_default]
- model.title = params[:title]
+ set_title
+ end
+
+ def validate_and_result
+ super.tap do |call|
+ # map errors on attachments to better display them
+ if call.errors[:attachments].any?
+ model.ifc_attachment.errors.details.each do |_, errors|
+ errors.each do |error|
+ call.errors.add(:attachments, error[:error], **error.except(:error))
+ end
+ end
+ end
+ end
+ end
+
+ def set_title
+ model.title = model.ifc_attachment&.file&.filename&.gsub(/\.\w+$/, '')
+ end
+
+ def set_ifc_attachment(ifc_attachment)
+ return unless ifc_attachment
+
+ model.attachments.each(&:mark_for_destruction)
+ model.attach_files('first' => { 'file' => ifc_attachment, 'description' => 'ifc' })
end
end
end
diff --git a/modules/ifc_models/app/services/ifc_models/update_service.rb b/modules/ifc_models/app/services/ifc_models/update_service.rb
index 25d8dddf28f..1f2c1f02a73 100644
--- a/modules/ifc_models/app/services/ifc_models/update_service.rb
+++ b/modules/ifc_models/app/services/ifc_models/update_service.rb
@@ -30,41 +30,24 @@ module IFCModels
class UpdateService < ::BaseServices::Update
protected
- attr_accessor :ifc_attachment
-
def before_perform(params)
- self.ifc_attachment = params.delete('ifc_attachment')
- super(params)
+ super.tap do |call|
+ @ifc_attachment_replaced = call.success? && model.ifc_attachment.new_record?
+ end
end
def after_perform(call)
- call.success = replace_attachment(call)
-
- if @ifc_attachment_replaced && call.success?
- IFCConversionJob.perform_later(call.result)
+ if call.success?
+ # As the attachments association does not have the autosave option, we need to remove the
+ # attachments ourselves
+ model.attachments.select(&:marked_for_destruction?).each(&:destroy)
+
+ if @ifc_attachment_replaced
+ IFCConversionJob.perform_later(call.result)
+ end
end
call
end
-
- ##
- # Replace the IFC attachment file after saving
- def replace_attachment(result)
- return unless result.success?
-
- model = result.result
- # Uploading an IFC file is optional
- if ifc_attachment && ifc_attachment.size.positive?
- model.ifc_attachment = ifc_attachment
- @ifc_attachment_replaced = true
- end
-
- if model.save
- true
- else
- result.errors.add(:ifc_attachment, t('ifc_models.could_not_save_file'))
- false
- end
- end
end
end
diff --git a/modules/ifc_models/spec/services/create_service_spec.rb b/modules/ifc_models/spec/services/create_service_spec.rb
deleted file mode 100644
index 9d5dd8fe323..00000000000
--- a/modules/ifc_models/spec/services/create_service_spec.rb
+++ /dev/null
@@ -1,47 +0,0 @@
-require 'spec_helper'
-
-describe IFCModels::CreateService do
- let(:user) { FactoryBot.build_stubbed :admin }
- let(:ifc_attachment) { FileHelpers.mock_uploaded_file name: "model.ifc", content_type: 'application/binary', binary: true }
-
- let(:instance) { described_class.new(user: user) }
- subject { instance.call(params) }
-
- describe '#call' do
- let (:contract_result) { }
-
- context 'when user is allowed' do
- let(:params) do
- {
- is_default: true,
- ifc_attachment: ifc_attachment
- }
- end
-
- it 'returns a model from the params and queues conversion' do
- expect(subject).to be_success
-
- model = subject.result
- expect(model.ifc_attachment).to be_present
- expect(model.is_default).to be_truthy
- expect(::IFCModels::IFCConversionJob)
- .to have_been_enqueued
- .with(model)
- end
- end
-
- context 'when contract does not validate' do
- before do
- allow(instance)
- .to(receive(:validate_contract))
- .and_return(ServiceResult.new(success: false))
- end
-
- let(:params) { { title: 'foo' } }
-
- it 'returns the service result from contract' do
- expect(subject).not_to be_success
- end
- end
- end
-end
\ No newline at end of file
diff --git a/modules/ifc_models/spec/services/ifc_models/create_service_spec.rb b/modules/ifc_models/spec/services/ifc_models/create_service_spec.rb
new file mode 100644
index 00000000000..0a05d80a9b5
--- /dev/null
+++ b/modules/ifc_models/spec/services/ifc_models/create_service_spec.rb
@@ -0,0 +1,148 @@
+require 'spec_helper'
+
+describe IFCModels::CreateService do
+ let(:user) { FactoryBot.build_stubbed(:user) }
+ let(:contract_class) do
+ double('contract_class', '<=': true)
+ end
+ let(:model_valid) { true }
+ let(:instance) do
+ described_class.new(user: user,
+ contract_class: contract_class)
+ end
+ let(:call_attributes) { { name: 'Some name', identifier: 'Some identifier' } }
+ let(:set_attributes_success) do
+ true
+ end
+ let(:set_attributes_errors) do
+ double('set_attributes_errors')
+ end
+ let(:set_attributes_result) do
+ ServiceResult.new result: created_model,
+ success: set_attributes_success,
+ errors: set_attributes_errors
+ end
+ let!(:created_model) do
+ model = FactoryBot.build_stubbed(:ifc_model)
+
+ allow(IFCModels::IFCModel)
+ .to receive(:new)
+ .and_return(model)
+
+ allow(model)
+ .to receive(:save)
+ .and_return(model_valid)
+
+ model
+ end
+ let!(:set_attributes_service) do
+ service = double('set_attributes_service_instance')
+
+ allow(IFCModels::SetAttributesService)
+ .to receive(:new)
+ .with(user: user,
+ model: created_model,
+ contract_class: contract_class)
+ .and_return(service)
+
+ allow(service)
+ .to receive(:call)
+ .and_return(set_attributes_result)
+ end
+ let(:conversion_job) do
+ double('ifc_conversion_job').tap do |job|
+ allow(job)
+ .to receive(:perform_later)
+
+ stub_const('IFCModels::IFCConversionJob', job)
+ end
+ end
+
+ describe 'call' do
+ subject { instance.call(call_attributes) }
+
+ it 'is successful' do
+ expect(subject.success?).to be_truthy
+ end
+
+ it 'returns the result of the SetAttributesService' do
+ expect(subject)
+ .to eql set_attributes_result
+ end
+
+ it 'persists the model' do
+ expect(created_model)
+ .to receive(:save)
+ .and_return(model_valid)
+
+ subject
+ end
+
+ it 'returns the model' do
+ expect(subject.result)
+ .to eql created_model
+ end
+
+ it 'queues a conversion job' do
+ expect(conversion_job)
+ .to receive(:perform_later)
+ .with(created_model)
+
+ subject
+ end
+
+ context 'if the SetAttributeService is unsuccessful' do
+ let(:set_attributes_success) { false }
+
+ it 'is unsuccessful' do
+ expect(subject.success?).to be_falsey
+ end
+
+ it 'returns the result of the SetAttributesService' do
+ expect(subject)
+ .to eql set_attributes_result
+ end
+
+ it 'does not persist the changes' do
+ expect(created_model)
+ .to_not receive(:save)
+
+ subject
+ end
+
+ it "exposes the contract's errors" do
+ subject
+
+ expect(subject.errors).to eql set_attributes_errors
+ end
+
+ it 'queues no conversion job' do
+ expect(conversion_job)
+ .not_to receive(:perform_later)
+
+ subject
+ end
+ end
+
+ context 'when the model is invalid' do
+ let(:model_valid) { false }
+
+ it 'is unsuccessful' do
+ expect(subject.success?).to be_falsey
+ end
+
+ it "exposes the model's errors" do
+ subject
+
+ expect(subject.errors).to eql created_model.errors
+ end
+
+ it 'queues no conversion job' do
+ expect(conversion_job)
+ .not_to receive(:perform_later)
+
+ subject
+ end
+ end
+ end
+end
\ No newline at end of file
diff --git a/modules/ifc_models/spec/services/ifc_models/set_attributes_service_spec.rb b/modules/ifc_models/spec/services/ifc_models/set_attributes_service_spec.rb
new file mode 100644
index 00000000000..019f76f3753
--- /dev/null
+++ b/modules/ifc_models/spec/services/ifc_models/set_attributes_service_spec.rb
@@ -0,0 +1,184 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2020 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-2017 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.
+#++
+
+require 'spec_helper'
+
+describe IFCModels::SetAttributesService, type: :model do
+ let(:user) { FactoryBot.build_stubbed(:user) }
+ let(:other_user) { FactoryBot.build_stubbed(:user) }
+ let(:contract_class) do
+ contract = double('contract_class')
+
+ allow(contract)
+ .to receive(:new)
+ .with(model, user, options: { changed_by_system: changed_by_system })
+ .and_return(contract_instance)
+
+ contract
+ end
+ let(:changed_by_system) { [] }
+ let(:contract_instance) do
+ double('contract_instance', validate: contract_valid, errors: contract_errors)
+ end
+ let(:contract_valid) { true }
+ let(:contract_errors) do
+ double('contract_errors')
+ end
+ let(:model_valid) { true }
+ let(:instance) do
+ described_class.new(user: user,
+ model: model,
+ contract_class: contract_class)
+ 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)
+ end
+
+ before do
+ # required for the attachments
+ login_as(user)
+ end
+
+ describe 'call' do
+ let(:call_attributes) do
+ {
+ is_default: true,
+ project_id: project.id
+ }
+ end
+
+ before do
+ allow(model)
+ .to receive(:valid?)
+ .and_return(model_valid)
+
+ expect(contract_instance)
+ .to receive(:validate)
+ .and_return(contract_valid)
+ end
+
+ subject { instance.call(call_attributes) }
+
+ it 'is successful' do
+ expect(subject.success?).to be_truthy
+ end
+
+ it 'sets the attributes' do
+ subject
+
+ expect(model.attributes.slice(*model.changed).symbolize_keys)
+ .to eql call_attributes
+ end
+
+ it 'does not persist the model' do
+ expect(model)
+ .not_to receive(:save)
+
+ subject
+ end
+
+ context 'for a new record' do
+ let(:model) do
+ IFCModels::IFCModel.new
+ end
+
+ context 'with an ifc_attachment' do
+ let(:changed_by_system) { %w(uploader_id) }
+
+ let(:call_attributes) do
+ {
+ ifc_attachment: ifc_file
+ }
+ end
+
+ it 'is successful' do
+ expect(subject.success?).to be_truthy
+ end
+
+ it 'sets the title to the attachment`s filename' do
+ subject
+
+ expect(model.title)
+ .to eql 'model_2'
+ end
+
+ it 'sets the uploader to the attachment`s author (which is the current user)' do
+ subject
+
+ expect(model.uploader)
+ .to eql user
+ end
+ end
+ end
+
+ context 'for an existing model' do
+ context 'with an ifc_attachment' do
+ let(:changed_by_system) { %w(uploader_id) }
+
+ let(:call_attributes) do
+ {
+ ifc_attachment: ifc_file
+ }
+ end
+
+ it 'is successful' do
+ expect(subject.success?).to be_truthy
+ end
+
+ it 'does not alter the title' do
+ title_before = model.title
+
+ subject
+
+ expect(model.title)
+ .to eql title_before
+ end
+
+ it 'sets the uploader to the attachment`s author (which is the current user)' do
+ subject
+
+ expect(model.uploader)
+ .to eql user
+ end
+
+ it 'marks existing attachments for destruction' do
+ subject
+
+ expect(ifc_attachment)
+ .to be_marked_for_destruction
+ end
+ end
+ end
+ end
+end
diff --git a/modules/ifc_models/spec/services/ifc_models/update_service_spec.rb b/modules/ifc_models/spec/services/ifc_models/update_service_spec.rb
new file mode 100644
index 00000000000..f60dd7bba99
--- /dev/null
+++ b/modules/ifc_models/spec/services/ifc_models/update_service_spec.rb
@@ -0,0 +1,201 @@
+require 'spec_helper'
+
+describe IFCModels::UpdateService do
+ let(:user) { FactoryBot.build_stubbed(:user) }
+ let(:contract_class) do
+ double('contract_class', '<=': true)
+ end
+ let(:model_valid) { true }
+ let(:instance) do
+ described_class.new(user: user,
+ model: model,
+ contract_class: contract_class)
+ end
+ let(:call_attributes) { { name: 'Some name', identifier: 'Some identifier' } }
+ let(:set_attributes_success) do
+ true
+ end
+ let(:set_attributes_errors) do
+ double('set_attributes_errors')
+ end
+ let(:set_attributes_result) do
+ ServiceResult.new result: model,
+ success: set_attributes_success,
+ errors: set_attributes_errors
+ end
+ let!(:model) do
+ FactoryBot.build_stubbed(:ifc_model).tap do |m|
+ allow(m)
+ .to receive(:save)
+ .and_return(model_valid)
+ allow(m)
+ .to receive(:ifc_attachment)
+ .and_return(ifc_attachment)
+ allow(m)
+ .to receive(:attachments)
+ .and_return(attachments)
+ end
+ end
+ let!(:set_attributes_service) do
+ service = double('set_attributes_service_instance')
+
+ allow(IFCModels::SetAttributesService)
+ .to receive(:new)
+ .with(user: user,
+ model: model,
+ contract_class: contract_class)
+ .and_return(service)
+
+ allow(service)
+ .to receive(:call)
+ .and_return(set_attributes_result)
+ end
+ let(:conversion_job) do
+ double('ifc_conversion_job').tap do |job|
+ allow(job)
+ .to receive(:perform_later)
+
+ stub_const('IFCModels::IFCConversionJob', job)
+ end
+ end
+ let(:ifc_attachment) { FactoryBot.build_stubbed(:attachment) }
+ let(:other_attachment) do
+ FactoryBot.build_stubbed(:attachment).tap do |a|
+ allow(a)
+ .to receive(:marked_for_destruction?)
+ .and_return(attachment_marked_for_destruction)
+
+ allow(a)
+ .to receive(:destroy)
+ end
+ end
+ let(:attachment_marked_for_destruction) { false }
+ let(:attachments) { [ifc_attachment, other_attachment] }
+
+ describe 'call' do
+ subject { instance.call(call_attributes) }
+
+ it 'is successful' do
+ expect(subject.success?).to be_truthy
+ end
+
+ it 'returns the result of the SetAttributesService' do
+ expect(subject)
+ .to eql set_attributes_result
+ end
+
+ it 'persists the model' do
+ expect(model)
+ .to receive(:save)
+ .and_return(model_valid)
+
+ subject
+ end
+
+ it 'returns the model' do
+ expect(subject.result)
+ .to eql model
+ end
+
+ it 'schedules no conversion job' do
+ expect(conversion_job)
+ .not_to receive(:perform_later)
+
+ subject
+ end
+
+ it 'leaves attachments not marked for destruction' do
+ expect(other_attachment)
+ .not_to receive(:destroy)
+
+ subject
+ end
+
+ context 'if the attachment is altered' do
+ let(:attachment_marked_for_destruction) { true }
+ before do
+ allow(ifc_attachment)
+ .to receive(:new_record?)
+ .and_return(true)
+ end
+
+ it 'schedules conversion job' do
+ expect(conversion_job)
+ .to receive(:perform_later)
+ .with(model)
+
+ subject
+ end
+
+ it 'destroys all attachments marked for destruction' do
+ expect(other_attachment)
+ .to receive(:destroy)
+
+ subject
+ end
+ end
+
+ context 'if the SetAttributeService is unsuccessful' do
+ let(:set_attributes_success) { false }
+
+ it 'is unsuccessful' do
+ expect(subject.success?).to be_falsey
+ end
+
+ it 'returns the result of the SetAttributesService' do
+ expect(subject)
+ .to eql set_attributes_result
+ end
+
+ it 'does not persist the changes' do
+ expect(model)
+ .to_not receive(:save)
+
+ subject
+ end
+
+ it "exposes the contract's errors" do
+ subject
+
+ expect(subject.errors).to eql set_attributes_errors
+ end
+
+ context 'if the attachment is altered' do
+ let(:attachment_marked_for_destruction) { true }
+ before do
+ allow(ifc_attachment)
+ .to receive(:new_record?)
+ .and_return(true)
+ end
+
+ it 'schedules no conversion job' do
+ expect(conversion_job)
+ .not_to receive(:perform_later)
+
+ subject
+ end
+
+ it 'does not destroy attachments marked for destruction' do
+ expect(other_attachment)
+ .not_to receive(:destroy)
+
+ subject
+ end
+ end
+ end
+
+ context 'if the model is invalid' do
+ let(:model_valid) { false }
+
+ it 'is unsuccessful' do
+ expect(subject.success?).to be_falsey
+ end
+
+ it "exposes the model's errors" do
+ subject
+
+ expect(subject.errors).to eql model.errors
+ end
+ end
+ end
+end
\ No newline at end of file
diff --git a/modules/ifc_models/spec/services/view_conversion_service_spec.rb b/modules/ifc_models/spec/services/ifc_models/view_conversion_service_spec.rb
similarity index 100%
rename from modules/ifc_models/spec/services/view_conversion_service_spec.rb
rename to modules/ifc_models/spec/services/ifc_models/view_conversion_service_spec.rb
diff --git a/modules/ifc_models/spec/services/update_service_spec.rb b/modules/ifc_models/spec/services/update_service_spec.rb
deleted file mode 100644
index 40aa2bcb8c2..00000000000
--- a/modules/ifc_models/spec/services/update_service_spec.rb
+++ /dev/null
@@ -1,82 +0,0 @@
-require 'spec_helper'
-
-describe IFCModels::UpdateService do
- let(:user) { FactoryBot.build_stubbed :admin }
- let(:ifc_attachment_version_1) { FileHelpers.mock_uploaded_file(name: "model_1.ifc", content_type: 'application/binary', binary: true) }
- let(:ifc_attachment_version_2) { FileHelpers.mock_uploaded_file(name: "model_2.ifc", content_type: 'application/binary', binary: true) }
-
- let(:ifc_model) do
- ::IFCModels::IFCModel.create(ifc_attachment: ifc_attachment_version_1,
- title: "Architecture",
- is_default: false,
- uploader: user)
- end
-
- let(:instance) { described_class.new(user: user, model: ifc_model) }
-
- subject { instance.call(params) }
-
- describe '#call' do
- let (:contract_result) { }
-
- context 'when user is allowed' do
- let(:params) do
- {
- is_default: true,
- ifc_attachment: ifc_attachment_version_2
- }.with_indifferent_access
- end
-
- it 'returns a model from the params and queues conversion' do
- expect(subject).to be_success
-
- model = subject.result
- expect(model.ifc_attachment.filename).to eq('model_2.ifc')
- expect(model.is_default).to be_truthy
- expect(::IFCModels::IFCConversionJob)
- .to have_been_enqueued
- .with(model)
- .on_queue('ifc_conversion')
- end
-
- context 'only updating title and is_default' do
- let(:params) do
- {
- is_default: true,
- title: "New title"
- }
- end
-
- it 'updates the title and is_default even without updating ifc_attachments' do
- expect(subject).to be_success
- model = subject.result
-
- expect(model.title).to eq('New title')
- expect(::IFCModels::IFCConversionJob)
- .to_not have_been_enqueued
- .with(model)
- end
- end
- end
-
- context 'when contract does not validate' do
- before do
- allow(instance)
- .to(receive(:validate_contract))
- .and_return(ServiceResult.new(success: false))
- end
-
- let(:params) { { title: 'foo' } }
-
- it 'returns the service result from contract' do
- expect(subject).not_to be_success
- model = ifc_model.reload
-
- expect(model.title).to_not eq('foo')
- expect(::IFCModels::IFCConversionJob)
- .to_not have_been_enqueued
- .with(model)
- end
- end
- end
-end
\ No newline at end of file