docbrown/app/controllers/social_previews_controller.rb
Michael Kohl 7f75f99560
[deploy] Rename classified listings (#7910)
* Change models and related files

* Update controllers and specs

* More renaming

* Seek and destroy, I mean search and replace

* Round up the stragglers

* Ground control to Major Travis...

* More fixes

* PR feedback

* Various fixes

* Rename view

* Fix list query builder

* Unify request specs

* Fix some API spec errors

* Fix remaining API specs

* Make spec conform to API

* Fix leftover problems

* Fix JS tests

* Fix column name in select

* Fix API specs

* Fix search specs

* Paging Mr. Travis
2020-05-27 13:35:09 +00:00

65 lines
1.8 KiB
Ruby

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