diff --git a/modules/ifc_models/app/contracts/ifc_models/base_contract.rb b/modules/ifc_models/app/contracts/ifc_models/base_contract.rb index fd2da3ad6e..339c47e551 100644 --- a/modules/ifc_models/app/contracts/ifc_models/base_contract.rb +++ b/modules/ifc_models/app/contracts/ifc_models/base_contract.rb @@ -33,6 +33,7 @@ module IFCModels to: :model attribute :title + attribute :is_default attribute :project attribute :uploader attribute :is_default 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 d0f39743c8..bbdc00408b 100644 --- a/modules/ifc_models/app/services/ifc_models/create_service.rb +++ b/modules/ifc_models/app/services/ifc_models/create_service.rb @@ -28,18 +28,18 @@ module IFCModels class CreateService < ::BaseServices::Create - protected - def before_perform(params) - ifc_attachment = params.delete('ifc_attachment') + attr_accessor :ifc_attachment - super(params).tap do |result| - result.success = add_attachment(result, ifc_attachment) - end + 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 @@ -53,16 +53,16 @@ module IFCModels ## # Add the IFC attachment file after saving - def add_attachment(result, file) + def add_attachment(result) return unless result.success? model = result.result - if file && file.size.positive? - model.ifc_attachment = file - model.title = file.original_filename + 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, t('ifc_models.could_not_save_file')) + result.errors.add(:ifc_attachment, I18n.t('ifc_models.could_not_save_file')) false end end diff --git a/modules/ifc_models/spec/services/create_service_spec.rb b/modules/ifc_models/spec/services/create_service_spec.rb new file mode 100644 index 0000000000..f2924313f4 --- /dev/null +++ b/modules/ifc_models/spec/services/create_service_spec.rb @@ -0,0 +1,46 @@ +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(::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