debian-mirror-gitlab/lib/banzai/object_renderer.rb

101 lines
3.5 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2016-08-24 12:49:21 +05:30
module Banzai
2016-11-03 12:29:30 +05:30
# Class for rendering multiple objects (e.g. Note instances) in a single pass,
# using +render_field+ to benefit from caching in the database. Rendering and
# redaction are both performed.
2016-08-24 12:49:21 +05:30
#
2016-11-03 12:29:30 +05:30
# The unredacted HTML is generated according to the usual +render_field+
# policy, so specify the pipeline and any other context options on the model.
#
# The *redacted* (i.e., suitable for use) HTML is placed in an attribute
# named "redacted_<foo>", where <foo> is the name of the cache field for the
# chosen attribute.
#
# As an example, rendering the attribute `note` would place the unredacted
# HTML into `note_html` and the redacted HTML into `redacted_note_html`.
2016-08-24 12:49:21 +05:30
class ObjectRenderer
2018-05-09 12:01:36 +05:30
attr_reader :context
2016-08-24 12:49:21 +05:30
2018-05-09 12:01:36 +05:30
# default_project - A default Project to use for redacting Markdown.
2016-08-24 12:49:21 +05:30
# user - The user viewing the Markdown/HTML documents, if any.
2018-03-17 18:26:18 +05:30
# redaction_context - A Hash containing extra attributes to use during redaction
2018-05-09 12:01:36 +05:30
def initialize(default_project: nil, user: nil, redaction_context: {})
@context = RenderContext.new(default_project, user)
2018-03-17 18:26:18 +05:30
@redaction_context = base_context.merge(redaction_context)
2016-08-24 12:49:21 +05:30
end
# Renders and redacts an Array of objects.
#
2016-11-03 12:29:30 +05:30
# objects - The objects to render.
2016-08-24 12:49:21 +05:30
# attribute - The attribute containing the raw Markdown to render.
#
# Returns the same input objects.
def render(objects, attribute)
2017-08-17 22:00:37 +05:30
documents = render_documents(objects, attribute)
documents = post_process_documents(documents, objects, attribute)
2016-08-24 12:49:21 +05:30
redacted = redact_documents(documents)
objects.each_with_index do |object, index|
redacted_data = redacted[index]
2018-03-17 18:26:18 +05:30
object.__send__("redacted_#{attribute}_html=", redacted_data[:document].to_html(save_options).html_safe) # rubocop:disable GitlabSecurity/PublicSend
object.user_visible_reference_count = redacted_data[:visible_reference_count] if object.respond_to?(:user_visible_reference_count)
2018-11-08 19:23:39 +05:30
object.total_reference_count = redacted_data[:total_reference_count] if object.respond_to?(:total_reference_count)
2016-08-24 12:49:21 +05:30
end
end
2017-08-17 22:00:37 +05:30
private
def render_documents(objects, attribute)
pipeline = HTML::Pipeline.new([])
objects.map do |object|
2018-05-09 12:01:36 +05:30
document = pipeline.to_document(Banzai.render_field(object, attribute))
context.associate_document(document, object)
document
2017-08-17 22:00:37 +05:30
end
end
def post_process_documents(documents, objects, attribute)
# Called here to populate cache, refer to IssuableExtractor docs
2018-05-09 12:01:36 +05:30
IssuableExtractor.new(context).extract(documents)
2017-08-17 22:00:37 +05:30
documents.zip(objects).map do |document, object|
2018-05-09 12:01:36 +05:30
pipeline_context = context_for(document, object, attribute)
Banzai::Pipeline[:post_process].to_document(document, pipeline_context)
2017-08-17 22:00:37 +05:30
end
2016-08-24 12:49:21 +05:30
end
# Redacts the list of documents.
#
# Returns an Array containing the redacted documents.
def redact_documents(documents)
2018-05-09 12:01:36 +05:30
redactor = Redactor.new(context)
2016-08-24 12:49:21 +05:30
redactor.redact(documents)
end
# Returns a Banzai context for the given object and attribute.
2018-05-09 12:01:36 +05:30
def context_for(document, object, attribute)
@redaction_context.merge(object.banzai_render_context(attribute)).merge(
project: context.project_for_node(document)
)
2016-08-24 12:49:21 +05:30
end
def base_context
2018-03-17 18:26:18 +05:30
{
2018-05-09 12:01:36 +05:30
current_user: context.current_user,
2017-08-17 22:00:37 +05:30
skip_redaction: true
2018-03-17 18:26:18 +05:30
}
end
def save_options
return {} unless @redaction_context[:xhtml]
{ save_with: Nokogiri::XML::Node::SaveOptions::AS_XHTML }
2016-08-24 12:49:21 +05:30
end
end
end