Module: BlogImageChecksHelper

Defined in:
app/helpers/blog_image_checks_helper.rb

Overview

View helper: blog image checks.

Renders an audit table of every image_tag reference embedded in published
blog post bodies, resolving each referenced file to an Image record so
editors can spot missing alt text and broken references.

See Also:

Instance Method Summary collapse

Instance Method Details

#image_lookup(image_or_id, post_id) ⇒ Image

Resolves an image reference to its Image record.

Accepts an Image instance (returned as-is), an Integer id, or a file name
whose basename (minus extension) is treated as the image's friendly id.
On lookup failure, reports the error in production and falls back to the
placeholder image.

Parameters:

  • image_or_id (Image, Integer, String)

    image record, id, or file name

  • post_id (Integer)

    id of the Post being audited (used in error reports)

Returns:

  • (Image)

    the resolved image, or the placeholder on lookup failure



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/helpers/blog_image_checks_helper.rb', line 61

def image_lookup(image_or_id, post_id)
  if image_or_id.is_a?(Image)
    image_or_id
  else
    # Direct id lookup
    if image_or_id.is_a?(Integer)
      friendly_id = image_or_id
    else # Extract slug from file name and desired format
      extension = File.extname(image_or_id)
      friendly_id = File.basename(image_or_id, extension)
      extension[0] = '' # Removes the .
    end
    begin
      Image.friendly.find(friendly_id)
    rescue StandardError => e
      ErrorReporting.error(e, "Image not found: #{image_or_id}. Post id #{post_id}") if Rails.env.production?
      # Fallback to placeholder
      Image.friendly.find("placeholder-b0f016")
    end
  end
end

#render_blog_imagesActiveSupport::SafeBuffer

Builds table rows listing every image referenced by each published post.

Scans each post's solution body for image_tag ... %} markers, resolves
the referenced file via #image_lookup, and renders one <tr> per image
with a thumbnail, its alt text (or the image title), and a link to the post.

Returns:

  • (ActiveSupport::SafeBuffer)

    HTML table rows for the image audit



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/helpers/blog_image_checks_helper.rb', line 18

def render_blog_images
  tags = html_escape('')
  posts = Post.published.order(created_at: :desc)
  posts.each do |post|
    regex = /image_tag(.*?)%}/

    images = post.solution.scan(regex).flatten
    images.each do |image|
      args = image.split(",").map(&:strip)
      args[0]
      image_alt = args[1]
      image_url = args[5]

      next if image_url.blank?

      img = image_lookup(image_url, post.id)

      tags << (:tr) do
        (:td, link_to(image_tag(img.image_url(size: "150x150")), img)) +
          (:td) do
            if image_alt.present?
              (:span, image_alt)
            elsif img.title.present?
              fa_icon('database', family: :regular, text: img.title)
            end
          end +
          (:td, link_to(post.id, post))
      end
    end
  end
  tags
end