Use Rails native attachment validations

They were introduced in Rails 6.0.
This commit is contained in:
Javi Martín
2022-07-26 02:03:32 +02:00
parent c97e7852a4
commit eca1714a26

View File

@@ -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