Module: Decidim::MailerHelper

Included in:
ApplicationMailer, NewslettersHelper
Defined in:
app/helpers/decidim/mailer_helper.rb

Overview

Helper that provides methods to render order selector and links

Instance Method Summary collapse

Instance Method Details

#decidim_transform_image_urls(content, host) ⇒ String

Transforms relative image URLs in HTML content to absolute URLs using the provided host. This is used in emails (newsletters and notifications) to ensure images display correctly in email clients.

Parameters:

  • content (String)
    • HTML content with img tags

  • host (String)
    • the Decidim::Organization host to use for the root URL

Returns:

  • (String)
    • the content with transformed image URLs



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/helpers/decidim/mailer_helper.rb', line 14

def decidim_transform_image_urls(content, host)
  return content if host.blank? || content.blank?

  root_url = if Decidim.storage_cdn_host.present?
               Decidim.storage_cdn_host.chomp("/")
             else
               Decidim::EngineRouter.new("decidim", {}).root_url(host:).chomp("/")
             end

  content.gsub(/src\s*=\s*(['"])([^'"]*)\1/) do
    quote = Regexp.last_match(1)
    src_value = Regexp.last_match(2)

    if src_value.blank? || src_value.start_with?("http://", "https://", "data:", "//", "cid:")
      %(src=#{quote}#{src_value}#{quote})
    else
      normalized_src = src_value.start_with?("/") ? src_value : "/#{src_value}"
      %(src=#{quote}#{root_url}#{normalized_src}#{quote})
    end
  end
end