Module: Advertisements::QuestionHelper

Includes:
Magick
Defined in:
app/helpers/advertisements/question_helper.rb

Instance Method Summary collapse

Instance Method Details

#question_ad(question) ⇒ Object

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/BlockLength



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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/helpers/advertisements/question_helper.rb', line 8

def question_ad(question)
  Rails.cache.fetch "posts/#{question.id}/ad", expires_in: 60.minutes do
    ad = Image.new(600, 500)
    ad.background_color = 'white'

    upper_bar = Draw.new
    upper_bar.fill '#4B68FF'
    upper_bar.rectangle 0, 0, 600, 130
    upper_bar.draw ad

    answer = Draw.new
    answer.font_family = 'Roboto'
    answer.font_weight = 700
    answer.font = './app/assets/imgfonts/Roboto-Bold.ttf'
    answer.pointsize = 40
    answer.gravity = CenterGravity
    answer.annotate ad, 600, 50, 0, 10, 'Could you answer' do |img|
      img.fill = 'white'
    end
    answer.annotate ad, 600, 50, 0, 70, 'this question?' do |img|
      img.fill = 'white'
    end

    icon_path = SiteSetting.find_by(name: 'SiteLogoPath', community: question.community).typed
    if icon_path.present?
      icon = community_icon(icon_path)
      icon.resize_to_fit!(175, 75)
      ad.composite!(icon, SouthWestGravity, 20, 15, SrcAtopCompositeOp)
    else
      community_name = Draw.new
      community_name.font_family = 'Roboto'
      community_name.font_weight = 700
      community_name.font = './app/assets/imgfonts/Roboto-Bold.ttf'
      community_name.pointsize = 25
      community_name.gravity = SouthWestGravity
      community_name.annotate ad, 0, 0, 20, 20, question.community.name do |img|
        img.fill = '#4B68FF'
      end
    end

    community_url = Draw.new
    community_url.font_family = 'Roboto'
    community_url.font_weight = 700
    community_url.font = './app/assets/imgfonts/Roboto-Bold.ttf'
    community_url.pointsize = 20
    community_url.gravity = SouthEastGravity
    community_url.annotate ad, 0, 0, 20, 20, question.community.host do |img|
      img.fill = '#666666'
    end

    title = Draw.new
    title.font_family = 'Roboto'
    title.font_weight = 900
    title.font = './app/assets/imgfonts/Roboto-Black-FRLHebrew.ttf'
    title.pointsize = 50
    title.gravity = NorthGravity
    position = 0
    if question.title.length > 60
      title.pointsize = 35
      wrap_text(do_rtl_witchcraft(question.title), 500, 35).split("\n").each do |line|
        title.annotate ad, 500, 100, 50, 135 + (position * 55), line do |img|
          img.fill = '#333333'
        end
        position += 1
      end
    else
      wrap_text(do_rtl_witchcraft(question.title), 500, 55).split("\n").each do |line|
        title.annotate ad, 500, 100, 50, 160 + (position * 70), line do |img|
          img.fill = '#333333'
        end
        position += 1
      end
    end

    ad.format = 'PNG'
    ad.border!(2, 2, 'black')
  end
end