Use Rails native attachment validations
They were introduced in Rails 6.0.
This commit is contained in:
@@ -5,9 +5,8 @@ module Attachable
|
|||||||
has_one_attached :attachment
|
has_one_attached :attachment
|
||||||
attr_accessor :cached_attachment
|
attr_accessor :cached_attachment
|
||||||
|
|
||||||
validate :attachment_presence
|
|
||||||
|
|
||||||
validates :attachment,
|
validates :attachment,
|
||||||
|
presence: true,
|
||||||
file_content_type: {
|
file_content_type: {
|
||||||
allow: ->(record) { record.accepted_content_types },
|
allow: ->(record) { record.accepted_content_types },
|
||||||
if: -> { association_class && attachment.attached? },
|
if: -> { association_class && attachment.attached? },
|
||||||
@@ -63,12 +62,4 @@ module Attachable
|
|||||||
def file_path
|
def file_path
|
||||||
ActiveStorage::Blob.service.path_for(attachment.blob.key)
|
ActiveStorage::Blob.service.path_for(attachment.blob.key)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def attachment_presence
|
|
||||||
unless attachment.attached?
|
|
||||||
errors.add(:attachment, I18n.t("errors.messages.blank"))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user