From eca1714a26f54d82b78435f9d9ce9b8dd54372b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Tue, 26 Jul 2022 02:03:32 +0200 Subject: [PATCH] Use Rails native attachment validations They were introduced in Rails 6.0. --- app/models/concerns/attachable.rb | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/app/models/concerns/attachable.rb b/app/models/concerns/attachable.rb index cc457f934..87bd92363 100644 --- a/app/models/concerns/attachable.rb +++ b/app/models/concerns/attachable.rb @@ -5,9 +5,8 @@ module Attachable has_one_attached :attachment attr_accessor :cached_attachment - validate :attachment_presence - validates :attachment, + presence: true, file_content_type: { allow: ->(record) { record.accepted_content_types }, if: -> { association_class && attachment.attached? }, @@ -63,12 +62,4 @@ module Attachable def file_path ActiveStorage::Blob.service.path_for(attachment.blob.key) end - - private - - def attachment_presence - unless attachment.attached? - errors.add(:attachment, I18n.t("errors.messages.blank")) - end - end end