metamaps--metamaps/app/models/attachment.rb

42 lines
1 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2017-11-25 19:23:47 +00:00
class Attachment < ApplicationRecord
belongs_to :attachable, polymorphic: true
has_attached_file :file,
styles: lambda { |a|
if a.instance.image?
{
thumb: 'x128#',
medium: 'x320>'
}
else
{}
end
}
validates :attachable, presence: true
validates_attachment_content_type :file, content_type: Attachable.allowed_types
validates_attachment_size :file, in: 0.megabytes..5.megabytes
def image?
Attachable.image_types.include?(file.instance.file_content_type)
end
def audio?
Attachable.audio_types.include?(file.instance.file_content_type)
end
def text?
Attachable.text_types.include?(file.instance.file_content_type)
end
def pdf?
Attachable.pdf_types.include?(file.instance.file_content_type)
end
def document?
text? || pdf?
end
end