From 4165674987959ed1b7cedf26f6b28515d62e9d9a Mon Sep 17 00:00:00 2001 From: Kat Date: Tue, 8 Oct 2024 10:28:50 +0100 Subject: [PATCH] Extract validations into translations file --- app/controllers/collection_resources_controller.rb | 10 +++++----- config/locales/en.yml | 8 ++++++++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/app/controllers/collection_resources_controller.rb b/app/controllers/collection_resources_controller.rb index 266be6d39..9333fe37d 100644 --- a/app/controllers/collection_resources_controller.rb +++ b/app/controllers/collection_resources_controller.rb @@ -60,7 +60,7 @@ class CollectionResourcesController < ApplicationController begin UploadCollectionResourcesService.upload_collection_resource(filename, file) rescue StandardError - @collection_resource.errors.add(:file, "There was an error uploading this file.") + @collection_resource.errors.add(:file, :error_uploading) return render "collection_resources/edit" end @@ -101,8 +101,8 @@ private end def validate_file(file) - return @collection_resource.errors.add(:file, "Select which file to upload") unless file - return @collection_resource.errors.add(:file, "The file is above 100MB") if file.size > 100.megabytes + return @collection_resource.errors.add(:file, :blank) unless file + return @collection_resource.errors.add(:file, :above_100_mb) if file.size > 100.megabytes argv = %W[file --brief --mime-type -- #{file.path}] output = `#{argv.shelljoin}` @@ -110,11 +110,11 @@ private case @collection_resource.resource_type when "paper_form" unless output.match?(/application\/pdf/) - @collection_resource.errors.add(:file, "The paper form must be a PDF.") + @collection_resource.errors.add(:file, :must_be_pdf) end when "bulk_upload_template", "bulk_upload_specification" unless output.match?(/application\/vnd\.ms-excel|application\/vnd\.openxmlformats-officedocument\.spreadsheetml\.sheet/) - @collection_resource.errors.add(:file, "The #{@collection_resource.short_display_name.downcase} must be a Microsoft Excel file.") + @collection_resource.errors.add(:file, :must_be_xlsx, resource: @collection_resource.short_display_name.downcase) end end end diff --git a/config/locales/en.yml b/config/locales/en.yml index c666746da..aa211feba 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -116,6 +116,14 @@ en: attributes: confirm_soft_errors: blank: You must select if there are errors in these fields + collection_resource: + attributes: + file: + error_uploading: There was an error uploading this file. + blank: Select which file to upload. + above_100_mb: The file is above 100MB. + must_be_pdf: The paper form must be a PDF. + must_be_xlsx: The %{resource} must be a Microsoft Excel file. activerecord: attributes: