* Initialize compare hex once in controller, fallback if color missing This PR adds a fallback color choice when there are missing bg_color_hex or text_color_hex (this is likely going to become more common as we only show the bg_color_hex in the form). The choices (black and white) may not be correct - this "custom, or default" value choice should likely be made in a decorator or on the model instead. Addresses an error reported in https://app.honeybadger.io/projects/66984/faults/83671065 * typo: Attribute name is text_color_hex
69 lines
2 KiB
Ruby
69 lines
2 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).select(: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.select(: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])
|
|
@compare_hex = Color::CompareHex.new([@tag.bg_color_hex || "#000000", @tag.text_color_hex || "#ffffff"])
|
|
|
|
set_respond
|
|
end
|
|
|
|
def comment
|
|
@comment = Comment.find(params[:id])
|
|
|
|
badge_ids = Tag.where(name: @comment.commentable&.decorate&.cached_tag_list_array).pluck(:badge_id)
|
|
@tag_badges = Badge.where(id: badge_ids)
|
|
|
|
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: I18n.t("social_previews_controller.fonts")), status: :found
|
|
end
|
|
end
|
|
end
|
|
end
|