Class: RivetCms::MediaAsset

Inherits:
ApplicationRecord show all
Includes:
OrganizationScoped
Defined in:
app/models/rivet_cms/media_asset.rb

Constant Summary collapse

THUMBNAIL_SIZE =
[ 480, 480 ].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.kind_for(content_type) ⇒ Object



68
69
70
71
72
73
74
# File 'app/models/rivet_cms/media_asset.rb', line 68

def self.kind_for(content_type)
  case content_type.to_s
  when /\Aimage\// then :image
  when /\Avideo\// then :video
  else :file
  end
end

Instance Method Details

#embedded_in_content?Boolean

Rich text embeds media by URL (which contains the blob signed id), so field references alone don't tell us an asset is in use.

Returns:

  • (Boolean)


61
62
63
64
65
66
# File 'app/models/rivet_cms/media_asset.rb', line 61

def embedded_in_content?
  return false unless file.attached?

  pattern = file.blob.signed_id.gsub(/[!%_]/) { |c| "!#{c}" }
  ContentValue.where("text_value LIKE ? ESCAPE '!'", "%#{pattern}%").exists?
end

#thumbnail_urlObject

Resized representation for grid/picker cells: a variant for images, a preview (first page/frame) for PDFs and videos when the host has poppler or ffmpeg installed. Nil when neither applies so callers fall back to a glyph. Processing happens lazily on first request.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/models/rivet_cms/media_asset.rb', line 40

def thumbnail_url
  return nil unless file.attached?

  representation =
    if image? && file.blob.variable?
      file.variant(resize_to_limit: THUMBNAIL_SIZE)
    elsif file.blob.previewable?
      file.preview(resize_to_limit: THUMBNAIL_SIZE)
    end
  return nil unless representation

  helpers = Rails.application.routes.url_helpers
  if RivetCms.media_host.present?
    helpers.rails_representation_url(representation, host: RivetCms.media_host)
  else
    helpers.rails_representation_path(representation, only_path: true)
  end
end

#urlObject



25
26
27
28
29
30
31
32
33
34
# File 'app/models/rivet_cms/media_asset.rb', line 25

def url
  return nil unless file.attached?

  helpers = Rails.application.routes.url_helpers
  if RivetCms.media_host.present?
    helpers.rails_blob_url(file, host: RivetCms.media_host)
  else
    helpers.rails_blob_path(file, only_path: true)
  end
end