-
Notifications
You must be signed in to change notification settings - Fork 4k
/
social_previews_controller.rb
65 lines (51 loc) · 1.82 KB
/
social_previews_controller.rb
1
2
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
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
class SocialPreviewsController < ApplicationController
# No authorization required for entirely public controller
PNG_CSS = "body { transform: scale(0.3); } .preview-div-wrapper { overflow: unset; margin: 5vw; }".freeze
def article
@article = Article.find(params[:id])
@tag_badges = Badge.where(id: Tag.where(name: @article.decorate.cached_tag_list_array).pluck(:badge_id))
not_found unless @article.published
template = @article.tags.
where.not(social_preview_template: nil).
where.not(social_preview_template: "article").
select(:social_preview_template).first&.social_preview_template
# make sure that the template exists
template = "article" unless Tag.social_preview_templates.include?(template)
set_respond "social_previews/articles/#{template}"
end
def user
@user = User.find(params[:id])
@tag_badges = Badge.where(id: @user.badge_achievements.pluck(:badge_id))
set_respond
end
def listing
@listing = Listing.find(params[:id]).decorate
set_respond
end
def organization
@user = Organization.find(params[:id])
@tag_badges = [] # Orgs don't have badges, but they could!
set_respond "user"
end
def tag
@tag = Tag.find(params[:id])
set_respond
end
def comment
@comment = Comment.find(params[:id])
@tag_badges = Badge.where(id: Tag.where(name: @comment.commentable&.decorate&.cached_tag_list_array).pluck(:badge_id))
set_respond
end
private
def set_respond(template = nil)
respond_to do |format|
format.html do
render template, layout: false
end
format.png do
html = render_to_string(template, formats: :html, layout: false)
redirect_to HtmlCssToImage.fetch_url(html: html, css: PNG_CSS, google_fonts: "Roboto|Roboto+Condensed"), status: :found
end
end
end
end