* Extracting container for allowed tags & attrs Prior to this commit, we had several different locations in which we specified ALLOWED_TAGS and ALLOWED_ATTRIBUTES for HTML rendering and sanitization. Curious to see how these either intersected or didn't, I opted to create a container module that allows for us to more readily normalize these allowed tags and attributes. It's possible that we won't do any normalization, but this work helps make that easier. Ideally, I'd love us to contextualize "why did we choose the tags/attributes we chose?" But for now, I think consolidating these tags and attributes will help make adding a `details` and `summary` tag easier. This relates to forem/rfcs#296 See [Google Sheet][1] for analysis of what tags/attributes are used, the intersection and union. [1]:https://docs.google.com/spreadsheets/d/1yj-a1qus1o0o4cj-_gOMP5yteeg-_f3s5z7kvK0Y7RM/edit#gid=0 * Fixing misnamed constant * Fixing misnamed constant * Extracting additional HtmlRendering use cases * Adding comparative documentation for HTML tags * Fixing broken parameter signature * Moving constants into MarkdownProcessor
45 lines
1.8 KiB
Ruby
45 lines
1.8 KiB
Ruby
class DisplayAd < ApplicationRecord
|
|
resourcify
|
|
|
|
ALLOWED_PLACEMENT_AREAS = %w[sidebar_left sidebar_left_2 sidebar_right].freeze
|
|
ALLOWED_PLACEMENT_AREAS_HUMAN_READABLE = ["Sidebar Left (First Position)",
|
|
"Sidebar Left (Second Position)",
|
|
"Sidebar Right"].freeze
|
|
|
|
belongs_to :organization, optional: true
|
|
has_many :display_ad_events, dependent: :destroy
|
|
|
|
validates :placement_area, presence: true,
|
|
inclusion: { in: ALLOWED_PLACEMENT_AREAS }
|
|
validates :body_markdown, presence: true
|
|
before_save :process_markdown
|
|
|
|
scope :approved_and_published, -> { where(approved: true, published: true) }
|
|
|
|
def self.for_display(area)
|
|
relation = approved_and_published.where(placement_area: area).order(success_rate: :desc)
|
|
|
|
if rand(8) == 1
|
|
relation.sample
|
|
else
|
|
relation.limit(rand(1..15)).sample
|
|
end
|
|
end
|
|
|
|
def human_readable_placement_area
|
|
ALLOWED_PLACEMENT_AREAS_HUMAN_READABLE[ALLOWED_PLACEMENT_AREAS.find_index(placement_area)]
|
|
end
|
|
|
|
private
|
|
|
|
def process_markdown
|
|
renderer = Redcarpet::Render::HTMLRouge.new(hard_wrap: true, filter_html: false)
|
|
markdown = Redcarpet::Markdown.new(renderer, Constants::Redcarpet::CONFIG)
|
|
initial_html = markdown.render(body_markdown)
|
|
stripped_html = ActionController::Base.helpers.sanitize initial_html,
|
|
tags: MarkdownProcessor::AllowedTags::DISPLAY_AD,
|
|
attributes: MarkdownProcessor::AllowedAttributes::DISPLAY_AD
|
|
html = stripped_html.delete("\n")
|
|
self.processed_html = Html::Parser.new(html).prefix_all_images(350, synchronous_detail_detection: true).html
|
|
end
|
|
end
|