Stripping tags from "tags.short_summary" column (#16248)
On the DEV.to database, I ran the following SQL: ```sql SELECT name, short_summary FROM tags WHERE short_summary LIKE '%<%'; ``` There were three tags with a `<` in them: - changelog - cnc2018 - javascript This PR will tidy up our short summary as part of saving a tag. It will also tidy up the existing tags.
This commit is contained in:
parent
41e6b7f858
commit
fc8158a5ba
4 changed files with 35 additions and 0 deletions
|
|
@ -52,6 +52,7 @@ class Tag < ActsAsTaggableOn::Tag
|
|||
validate :validate_name, if: :name?
|
||||
|
||||
before_validation :evaluate_markdown
|
||||
before_validation :tidy_short_summary
|
||||
before_validation :pound_it
|
||||
|
||||
before_save :calculate_hotness_score
|
||||
|
|
@ -131,6 +132,10 @@ class Tag < ActsAsTaggableOn::Tag
|
|||
|
||||
private
|
||||
|
||||
def tidy_short_summary
|
||||
self.short_summary = ActionController::Base.helpers.strip_tags(short_summary)
|
||||
end
|
||||
|
||||
def evaluate_markdown
|
||||
self.rules_html = MarkdownProcessor::Parser.new(rules_markdown).evaluate_markdown
|
||||
self.wiki_body_html = MarkdownProcessor::Parser.new(wiki_body_markdown).evaluate_markdown
|
||||
|
|
|
|||
|
|
@ -0,0 +1,11 @@
|
|||
module DataUpdateScripts
|
||||
class StrippingHtmlTagsFromTagShortSummary
|
||||
def run
|
||||
Tag.where("short_summary LIKE '%<%'").find_each do |tag|
|
||||
# Choosing to skip validations and mimic the newly added before_validation behavior.
|
||||
new_short_summary = ActionController::Base.helpers.strip_tags(tag.short_summary)
|
||||
tag.update_columns(short_summary: new_short_summary)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
require "rails_helper"
|
||||
require Rails.root.join(
|
||||
"lib/data_update_scripts/20220121114445_stripping_html_tags_from_tag_short_summary.rb",
|
||||
)
|
||||
|
||||
describe DataUpdateScripts::StrippingHtmlTagsFromTagShortSummary do
|
||||
it "updates a tag that had HTML elements in it's short summary" do
|
||||
tag = create(:tag)
|
||||
tag.update_columns(short_summary: "<p>Welcome to the <a href='#'>tag</a>.</p>")
|
||||
described_class.new.run
|
||||
expect(tag.reload.short_summary).to eq("Welcome to the tag.")
|
||||
end
|
||||
end
|
||||
|
|
@ -111,6 +111,12 @@ RSpec.describe Tag, type: :model do
|
|||
end
|
||||
end
|
||||
|
||||
it "strips HTML tags from short_summary before saving" do
|
||||
tag.short_summary = "<p>Hello <strong>World</strong>.</p>"
|
||||
tag.save
|
||||
expect(tag.short_summary).to eq("Hello World.")
|
||||
end
|
||||
|
||||
it "turns markdown into HTML before saving" do
|
||||
tag.rules_markdown = "Hello [Google](https://google.com)"
|
||||
tag.save
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue