From bc542773c276041721d856bbcd02b698b12ff531 Mon Sep 17 00:00:00 2001 From: Steve Taylor Date: Tue, 12 Mar 2024 13:02:48 -0700 Subject: [PATCH] Change upload validation to avoid blocking on form --- app/models/upload.rb | 6 +++++- spec/models/upload_spec.rb | 3 ++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/app/models/upload.rb b/app/models/upload.rb index 48fb9659..550fb449 100644 --- a/app/models/upload.rb +++ b/app/models/upload.rb @@ -9,7 +9,7 @@ class Upload < ApplicationRecord has_many :marc_profiles, dependent: :delete_all belongs_to :user, optional: true belongs_to :allowlisted_jwts, optional: true - validates :url, presence: true, if: proc { |upload| upload.files.blank? } + validate :url_presence_if_no_uploaded_files validates :files, presence: true, if: proc { |upload| upload.url.blank? } validate :valid_url, if: proc { |upload| upload.url.present? } scope :active, -> { where(status: %w[active processed]) } @@ -68,6 +68,10 @@ def read_marc_record_metadata(**options, &block) private + def url_presence_if_no_uploaded_files + errors.add(:url, 'A URL must be provided if a file has not been uploaded') if !files.attached? && url.blank? + end + def perform_extract_marc_record_metadata_job ExtractMarcRecordMetadataJob.perform_later(self) end diff --git a/spec/models/upload_spec.rb b/spec/models/upload_spec.rb index 68965502..73efa783 100644 --- a/spec/models/upload_spec.rb +++ b/spec/models/upload_spec.rb @@ -9,7 +9,8 @@ it 'validates that a URL or files are present' do expect do create(:upload, files: []) - end.to raise_error(ActiveRecord::RecordInvalid, "Validation failed: Url can't be blank, Files can't be blank") + end.to raise_error(ActiveRecord::RecordInvalid, + "Validation failed: Url A URL must be provided if a file has not been uploaded, Files can't be blank") end context 'with an invalid URL' do