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.
Instance Method Summary collapse
-
#image_lookup(image_or_id, post_id) ⇒ Image
Resolves an image reference to its Image record.
-
#render_blog_images ⇒ ActiveSupport::SafeBuffer
Builds table rows listing every image referenced by each published post.
Instance Method Details
#image_lookup(image_or_id, post_id) ⇒ Image
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_images ⇒ ActiveSupport::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.
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 = 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) << content_tag(:tr) do content_tag(:td, link_to(image_tag(img.image_url(size: "150x150")), img)) + content_tag(:td) do if image_alt.present? content_tag(:span, image_alt) elsif img.title.present? fa_icon('database', family: :regular, text: img.title) end end + content_tag(:td, link_to(post.id, post)) end end end end |