Class: ActiveCanvas::ContentRenderer

Inherits:
Object
  • Object
show all
Defined in:
app/services/active_canvas/content_renderer.rb

Overview

Resolves persisted media references (<img data-ac-media-id=ā€œNā€>) to fresh URLs at render time. We persist a stable id, never a time-limited URL, so saved content never rots.

Class Method Summary collapse

Class Method Details

.resolve(html) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/services/active_canvas/content_renderer.rb', line 6

def self.resolve(html)
  return html if html.blank?

  fragment = Nokogiri::HTML5.fragment(html)
  nodes = fragment.css("img[data-ac-media-id]")
  return html if nodes.empty?

  ids = nodes.map { |n| n["data-ac-media-id"] }.uniq
  media_by_id = Media.where(id: ids)
                     .includes(file_attachment: :blob)
                     .index_by { |m| m.id.to_s }

  nodes.each do |node|
    media = media_by_id[node["data-ac-media-id"].to_s]
    fresh_url = media&.url
    node["src"] = fresh_url if fresh_url # unknown id or orphaned media: leave the node as-is
  end

  fragment.to_html
end