Custom social preview template for tags (#6290) [deploy]
* Custom social preview template for tags * Added index on tags social_preview_template + small refactoring * Add checks if index exists to tags migration
This commit is contained in:
parent
946a94e560
commit
df93e8d41e
11 changed files with 51 additions and 7 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -47,6 +47,10 @@
|
|||
<%= f.label :category %>
|
||||
<%= f.select(:category, options_for_select(Tag.valid_categories, @tag.category)) %>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<%= f.label :social_preview_template %>
|
||||
<%= f.select(:social_preview_template, Tag.social_preview_templates) %>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<%= f.label :alias_for %>
|
||||
<%= f.text_field :alias_for, class: "form-control" %>
|
||||
|
|
|
|||
|
|
@ -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") %>
|
||||
<style>
|
||||
|
|
@ -114,7 +114,7 @@
|
|||
top: 4.5vw;
|
||||
}
|
||||
</style>
|
||||
<% font_size = font_size * 0.9 %>
|
||||
<% font_size *= 0.9 %>
|
||||
<div class="preview-info-header">
|
||||
<% if @article.comments_count > 0 %>
|
||||
#discuss <img src="<%= asset_path "comments-bubble.png" %>" /><%= @article.comments_count %>
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
class AddSocialPreviewTemplateToTags < ActiveRecord::Migration[5.2]
|
||||
def change
|
||||
add_column :tags, :social_preview_template, :string, default: "article"
|
||||
end
|
||||
end
|
||||
|
|
@ -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
|
||||
|
|
@ -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|
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -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}"
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue