Module: BlogImageChecksHelper

Defined in:
app/helpers/blog_image_checks_helper.rb

Instance Method Summary collapse

Instance Method Details

#image_lookup(image_or_id, post_id) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/helpers/blog_image_checks_helper.rb', line 37

def image_lookup(image_or_id, post_id)
 image = 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 => exc
            if Rails.env.production?
            ErrorReporting.error(exc, "Image not found: #{image_or_id}. Post id #{post_id}")
            end
            # Fallback to placeholder
            Image.friendly.find("placeholder-b0f016")
        end
        end
  return image
end

#render_blog_imagesObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/helpers/blog_image_checks_helper.rb', line 3

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)
        image_id = args[0]
        image_alt = args[1]
        image_url = args[5]
        
        next unless image_url.present?
        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?
                  (:i, img.title, class: 'fa fa-database')
                  end
                end +
                (:td, link_to(post.id, post))
              end
    end
    
  end
  return tags
end