2017-01-24 20:10:40 +00:00
|
|
|
# frozen_string_literal: true
|
2017-11-25 19:23:47 +00:00
|
|
|
|
2017-01-24 20:10:40 +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
|
|
|
|
}
|
|
|
|
|
2017-11-04 17:23:11 +00:00
|
|
|
validates :attachable, presence: true
|
2017-01-24 20:10:40 +00:00
|
|
|
validates_attachment_content_type :file, content_type: Attachable.allowed_types
|
2017-11-04 17:23:11 +00:00
|
|
|
validates_attachment_size :file, in: 0.megabytes..5.megabytes
|
2017-01-24 20:10:40 +00:00
|
|
|
|
|
|
|
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
|