metamaps--metamaps/app/models/concerns/attachable.rb

51 lines
897 B
Ruby
Raw Normal View History

2017-01-23 13:21:44 -05:00
# frozen_string_literal: true
module Attachable
extend ActiveSupport::Concern
2017-01-23 13:30:11 -05:00
included do
has_many :attachments, as: :attachable, dependent: :destroy
end
2017-01-23 13:21:44 -05:00
def images
2017-01-23 13:43:51 -05:00
attachments.where(file_content_type: image_types)
2017-01-23 13:21:44 -05:00
end
def audios
2017-01-23 13:43:51 -05:00
attachments.where(file_content_type: audio_types)
2017-01-23 13:21:44 -05:00
end
def texts
2017-01-23 13:43:51 -05:00
attachments.where(file_content_type: text_types)
2017-01-23 13:21:44 -05:00
end
def pdfs
2017-01-23 13:43:51 -05:00
attachments.where(file_content_type: pdf_types)
2017-01-23 13:21:44 -05:00
end
def documents
2017-01-23 13:43:51 -05:00
attachments.where(file_content_type: text_types + pdf_types)
2017-01-23 13:21:44 -05:00
end
class << self
def image_types
['image/png', 'image/gif', 'image/jpeg']
end
def audio_types
['audio/ogg', 'audio/mp3']
end
def text_types
2017-01-23 13:42:13 -05:00
['text/plain']
2017-01-23 13:21:44 -05:00
end
def pdf_types
['application/pdf']
end
def allowed_types
image_types + audio_types + text_types + pdf_types
end
end
end