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

40 lines
917 B
Ruby
Raw Normal View History

2017-01-22 16:28:18 -05:00
# frozen_string_literal: true
2017-01-22 15:23:24 -05:00
class Attachment < ApplicationRecord
belongs_to :attachable, polymorphic: true
has_attached_file :file,
2017-01-22 16:28:18 -05:00
styles: lambda { |a|
if a.instance.image?
{
small: 'x200>',
medium: 'x300>',
large: 'x400>'
}
else
{}
end
}
2017-01-22 15:23:24 -05:00
2017-01-23 13:21:44 -05:00
validates_attachment_content_type :file, content_type: Attachable.allowed_types
2017-01-22 15:23:24 -05:00
2017-01-22 16:28:18 -05:00
def image?
2017-01-23 13:21:44 -05:00
file.instance.file_content_type =~ %r{\Aimage}
end
def audio?
file.instance.file_content_type ~= %r{\Aaudio}
end
def text?
file.instance.file_content_type ~= %r{\Atext}
end
def pdf?
file.instance.file_content_type == 'application/pdf'
end
def document?
text? || pdf?
2017-01-22 15:23:24 -05:00
end
end