diff --git a/app/controllers/internal/tags_controller.rb b/app/controllers/internal/tags_controller.rb index a0bac4bc4..701094a00 100644 --- a/app/controllers/internal/tags_controller.rb +++ b/app/controllers/internal/tags_controller.rb @@ -45,7 +45,8 @@ class Internal::TagsController < Internal::ApplicationController def tag_params allowed_params = %i[ supported rules_markdown short_summary pretty_name bg_color_hex - text_color_hex tag_moderator_id remove_moderator_id alias_for badge_id category + text_color_hex tag_moderator_id remove_moderator_id alias_for badge_id + category social_preview_template ] params.require(:tag).permit(allowed_params) end diff --git a/app/controllers/social_previews_controller.rb b/app/controllers/social_previews_controller.rb index 676290a6b..771fb10be 100644 --- a/app/controllers/social_previews_controller.rb +++ b/app/controllers/social_previews_controller.rb @@ -2,16 +2,20 @@ 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 - SHE_CODED_TAGS = %w[shecoded theycoded shecodedally].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.decorate.cached_tag_list_array & SHE_CODED_TAGS).any? ? "shecoded" : "article" + template = @article.tags. + where("tags.social_preview_template IS NOT NULL AND tags.social_preview_template != ?", "article"). + select(:social_preview_template).first&.social_preview_template - set_respond 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 diff --git a/app/models/tag.rb b/app/models/tag.rb index 64456c6ad..0b6103fef 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -34,6 +34,11 @@ class Tag < ActsAsTaggableOn::Tag SEARCH_SERIALIZER = Search::TagSerializer SEARCH_CLASS = Search::Tag + # possible social previews templates for articles with a particular tag + def self.social_preview_templates + Rails.root.join("app/views/social_previews/articles").children.map { |ch| File.basename(ch, ".html.erb") } + end + def submission_template_customized(param_0 = nil) submission_template&.gsub("PARAM_0", param_0) end diff --git a/app/views/internal/tags/show.html.erb b/app/views/internal/tags/show.html.erb index 476a71065..4229dbea3 100644 --- a/app/views/internal/tags/show.html.erb +++ b/app/views/internal/tags/show.html.erb @@ -47,6 +47,10 @@ <%= f.label :category %> <%= f.select(:category, options_for_select(Tag.valid_categories, @tag.category)) %> +
+ <%= f.label :social_preview_template %> + <%= f.select(:social_preview_template, Tag.social_preview_templates) %> +
<%= f.label :alias_for %> <%= f.text_field :alias_for, class: "form-control" %> diff --git a/app/views/social_previews/article.html.erb b/app/views/social_previews/articles/article.html.erb similarity index 97% rename from app/views/social_previews/article.html.erb rename to app/views/social_previews/articles/article.html.erb index fe7a536a5..fa60c6025 100644 --- a/app/views/social_previews/article.html.erb +++ b/app/views/social_previews/articles/article.html.erb @@ -106,7 +106,7 @@ <% elsif @article.title.size < 50 %> <% font_size = 8.3 %> <% else %> - <% font_size = 15 - ((((@article.title).size**0.92) + 85) / 16) %> + <% font_size = 15 - (((@article.title.size**0.92) + 85) / 16) %> <% end %> <% if @article.tag_list.include?("discuss") %> - <% font_size = font_size * 0.9 %> + <% font_size *= 0.9 %>
<% if @article.comments_count > 0 %> #discuss " /><%= @article.comments_count %> diff --git a/app/views/social_previews/shecoded.html.erb b/app/views/social_previews/articles/shecoded.html.erb similarity index 100% rename from app/views/social_previews/shecoded.html.erb rename to app/views/social_previews/articles/shecoded.html.erb diff --git a/db/migrate/20200225104037_add_social_preview_template_to_tags.rb b/db/migrate/20200225104037_add_social_preview_template_to_tags.rb new file mode 100644 index 000000000..1c6bc0f97 --- /dev/null +++ b/db/migrate/20200225104037_add_social_preview_template_to_tags.rb @@ -0,0 +1,5 @@ +class AddSocialPreviewTemplateToTags < ActiveRecord::Migration[5.2] + def change + add_column :tags, :social_preview_template, :string, default: "article" + end +end diff --git a/db/migrate/20200226081611_add_index_to_tags_social_preview_template.rb b/db/migrate/20200226081611_add_index_to_tags_social_preview_template.rb new file mode 100644 index 000000000..eced493a0 --- /dev/null +++ b/db/migrate/20200226081611_add_index_to_tags_social_preview_template.rb @@ -0,0 +1,15 @@ +class AddIndexToTagsSocialPreviewTemplate < ActiveRecord::Migration[5.2] + disable_ddl_transaction! + + def up + return if index_exists?(:tags, :social_preview_template) + + add_index :tags, :social_preview_template, algorithm: :concurrently + end + + def down + return unless index_exists?(:tags, :social_preview_template) + + remove_index :tags, :social_preview_template, algorithm: :concurrently + end +end diff --git a/db/schema.rb b/db/schema.rb index db86d31a5..dc5ef1acb 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -965,6 +965,7 @@ ActiveRecord::Schema.define(version: 2020_02_27_214321) do t.text "rules_markdown" t.string "short_summary" t.string "social_image" + t.string "social_preview_template", default: "article" t.string "submission_rules_headsup" t.text "submission_template" t.boolean "supported", default: false @@ -974,6 +975,7 @@ ActiveRecord::Schema.define(version: 2020_02_27_214321) do t.text "wiki_body_html" t.text "wiki_body_markdown" t.index ["name"], name: "index_tags_on_name", unique: true + t.index ["social_preview_template"], name: "index_tags_on_social_preview_template" end create_table "tweets", id: :serial, force: :cascade do |t| diff --git a/lib/data_update_scripts/20200225114328_update_tags_social_preview_templates.rb b/lib/data_update_scripts/20200225114328_update_tags_social_preview_templates.rb new file mode 100644 index 000000000..ca7292964 --- /dev/null +++ b/lib/data_update_scripts/20200225114328_update_tags_social_preview_templates.rb @@ -0,0 +1,7 @@ +module DataUpdateScripts + class UpdateTagsSocialPreviewTemplates + def run + Tag.where(name: %w[shecoded theycoded shecodedally]).update_all(social_preview_template: "shecoded") + end + end +end diff --git a/spec/requests/social_previews_spec.rb b/spec/requests/social_previews_spec.rb index 97d7c2101..5180c33b7 100644 --- a/spec/requests/social_previews_spec.rb +++ b/spec/requests/social_previews_spec.rb @@ -33,7 +33,8 @@ RSpec.describe "SocialPreviews", type: :request do expect(first_request_body).to eq second_request_body end - it "renders shecoded template when tagged with shecoded" do + it "renders custom template when tagged with shecoded" do + create(:tag, social_preview_template: "shecoded") she_coded_article = create(:article, tags: "shecoded") get "/social_previews/article/#{she_coded_article.id}"