From 2fbfd68172cb9bb764007337a754a70a18cd76da Mon Sep 17 00:00:00 2001 From: Daniel Uber Date: Tue, 15 Feb 2022 09:56:57 -0600 Subject: [PATCH] Render social previews for tags with one of the two custom colors set (#16557) * 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 --- app/controllers/social_previews_controller.rb | 1 + app/views/social_previews/tag.html.erb | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app/controllers/social_previews_controller.rb b/app/controllers/social_previews_controller.rb index 8d90f3432..019eb6868 100644 --- a/app/controllers/social_previews_controller.rb +++ b/app/controllers/social_previews_controller.rb @@ -38,6 +38,7 @@ class SocialPreviewsController < ApplicationController 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 diff --git a/app/views/social_previews/tag.html.erb b/app/views/social_previews/tag.html.erb index 88854a358..398783b1e 100644 --- a/app/views/social_previews/tag.html.erb +++ b/app/views/social_previews/tag.html.erb @@ -1,6 +1,6 @@ -<% accent_color = Color::CompareHex.new([@tag.bg_color_hex, @tag.text_color_hex]).biggest %> -<% color = Color::CompareHex.new([@tag.bg_color_hex, @tag.text_color_hex]).brightness(1.4) %> -<% dark_color = Color::CompareHex.new([@tag.bg_color_hex, @tag.text_color_hex]).brightness(0.7) %> +<% accent_color = @compare_hex.biggest %> +<% color = @compare_hex.brightness(1.4) %> +<% dark_color = @compare_hex.brightness(0.7) %> <% not_so_rand = Random.new(@tag.id) # Using ID as seed ensures we get the same "random" numbers on each page load, Improves caching %>