factor out a bunch of file types
This commit is contained in:
parent
4a99d74fd8
commit
52b5ecb622
3 changed files with 67 additions and 7 deletions
|
@ -15,12 +15,25 @@ class Attachment < ApplicationRecord
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
validates_attachment_content_type :file, content_type: [
|
validates_attachment_content_type :file, content_type: Attachable.allowed_types
|
||||||
%r{\Aimage/.*\Z},
|
|
||||||
%r{\Avideo/.*\Z}
|
|
||||||
]
|
|
||||||
|
|
||||||
def image?
|
def image?
|
||||||
file.instance.file_content_type =~ /image/
|
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?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
48
app/models/concerns/attachable.rb
Normal file
48
app/models/concerns/attachable.rb
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
module Attachable
|
||||||
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
|
has_many :attachments, as: :attachable, dependent: :destroy
|
||||||
|
|
||||||
|
def images
|
||||||
|
attachments.where(file_content_type: self.image_types)
|
||||||
|
end
|
||||||
|
|
||||||
|
def audios
|
||||||
|
attachments.where(file_content_type: self.audio_types)
|
||||||
|
end
|
||||||
|
|
||||||
|
def texts
|
||||||
|
attachments.where(file_content_type: self.text_types)
|
||||||
|
end
|
||||||
|
|
||||||
|
def pdfs
|
||||||
|
attachments.where(file_content_type: self.pdf_types)
|
||||||
|
end
|
||||||
|
|
||||||
|
def documents
|
||||||
|
attachments.where(file_content_type: self.text_types + self.pdf_types)
|
||||||
|
end
|
||||||
|
|
||||||
|
class << self
|
||||||
|
def image_types
|
||||||
|
['image/png', 'image/gif', 'image/jpeg']
|
||||||
|
end
|
||||||
|
|
||||||
|
def audio_types
|
||||||
|
['audio/ogg', 'audio/mp3']
|
||||||
|
end
|
||||||
|
|
||||||
|
def text_types
|
||||||
|
['text/plain', 'text/markdown']
|
||||||
|
end
|
||||||
|
|
||||||
|
def pdf_types
|
||||||
|
['application/pdf']
|
||||||
|
end
|
||||||
|
|
||||||
|
def allowed_types
|
||||||
|
image_types + audio_types + text_types + pdf_types
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -1,6 +1,7 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
class Topic < ApplicationRecord
|
class Topic < ApplicationRecord
|
||||||
include TopicsHelper
|
include TopicsHelper
|
||||||
|
include Attachable
|
||||||
|
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
belongs_to :defer_to_map, class_name: 'Map', foreign_key: 'defer_to_map_id'
|
belongs_to :defer_to_map, class_name: 'Map', foreign_key: 'defer_to_map_id'
|
||||||
|
@ -21,8 +22,6 @@ class Topic < ApplicationRecord
|
||||||
validates :permission, presence: true
|
validates :permission, presence: true
|
||||||
validates :permission, inclusion: { in: Perm::ISSIONS.map(&:to_s) }
|
validates :permission, inclusion: { in: Perm::ISSIONS.map(&:to_s) }
|
||||||
|
|
||||||
has_many :attachments, as: :attachable, dependent: :destroy
|
|
||||||
|
|
||||||
def synapses
|
def synapses
|
||||||
synapses1.or(synapses2)
|
synapses1.or(synapses2)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue