Class: Panda::Core::FileCategorizer

Inherits:
Object
  • Object
show all
Defined in:
app/services/panda/core/file_categorizer.rb

Constant Summary collapse

ATTACHMENT_CATEGORY_MAP =

Maps [record_type, attachment_name] to category slug

{
  ["Panda::Core::User", "avatar"] => "user-avatars",
  ["Panda::CMS::Page", "og_image"] => "page-images",
  ["Panda::CMS::Post", "og_image"] => "post-images",
  ["Panda::CMS::FormSubmission", "files"] => "form-uploads",
  ["Panda::Social::InstagramPost", "image"] => "social-media"
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.custom_mappingsObject



40
41
42
# File 'app/services/panda/core/file_categorizer.rb', line 40

def self.custom_mappings
  @custom_mappings || {}
end

.register_mapping(record_type, attachment_name, category_slug) ⇒ Object

Register additional mappings from other engines (e.g. panda-cms-pro)



35
36
37
38
# File 'app/services/panda/core/file_categorizer.rb', line 35

def self.register_mapping(record_type, attachment_name, category_slug)
  @custom_mappings ||= {}
  @custom_mappings[[record_type, attachment_name]] = category_slug
end

Instance Method Details

#categorize_attachment(attachment) ⇒ Object

Categorize a blob by looking up its attachment's record type and name



16
17
18
19
20
21
# File 'app/services/panda/core/file_categorizer.rb', line 16

def categorize_attachment(attachment)
  slug = slug_for_attachment(attachment)
  return unless slug

  categorize_blob(attachment.blob, category_slug: slug)
end

#categorize_blob(blob, category_slug:) ⇒ Object

Directly assign a blob to a category by slug



24
25
26
27
28
29
30
31
32
# File 'app/services/panda/core/file_categorizer.rb', line 24

def categorize_blob(blob, category_slug:)
  category = Panda::Core::FileCategory.find_by(slug: category_slug)
  return unless category

  Panda::Core::FileCategorization.find_or_create_by!(
    file_category: category,
    blob_id: blob.id
  )
end