2016-06-21 07:35:09 -04:00
|
|
|
module Banzai
|
2016-10-06 17:52:44 -04:00
|
|
|
# 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-06-21 07:35:09 -04:00
|
|
|
#
|
2016-10-06 17:52:44 -04:00
|
|
|
# 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-06-21 07:35:09 -04:00
|
|
|
class ObjectRenderer
|
|
|
|
attr_reader :project, :user
|
|
|
|
|
2016-10-06 17:52:44 -04:00
|
|
|
# project - A Project to use for redacting Markdown.
|
2016-06-21 07:35:09 -04:00
|
|
|
# user - The user viewing the Markdown/HTML documents, if any.
|
2016-10-06 17:52:44 -04:00
|
|
|
# context - A Hash containing extra attributes to use during redaction
|
|
|
|
def initialize(project, user = nil, redaction_context = {})
|
2016-06-21 07:35:09 -04:00
|
|
|
@project = project
|
|
|
|
@user = user
|
2016-10-06 17:52:44 -04:00
|
|
|
@redaction_context = redaction_context
|
2016-06-21 07:35:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Renders and redacts an Array of objects.
|
|
|
|
#
|
2016-10-06 17:52:44 -04:00
|
|
|
# objects - The objects to render.
|
2016-06-21 07:35:09 -04:00
|
|
|
# attribute - The attribute containing the raw Markdown to render.
|
|
|
|
#
|
|
|
|
# Returns the same input objects.
|
|
|
|
def render(objects, attribute)
|
|
|
|
documents = render_objects(objects, attribute)
|
|
|
|
redacted = redact_documents(documents)
|
|
|
|
|
|
|
|
objects.each_with_index do |object, index|
|
2016-07-04 01:31:43 -04:00
|
|
|
redacted_data = redacted[index]
|
2016-10-06 17:52:44 -04:00
|
|
|
object.__send__("redacted_#{attribute}_html=", redacted_data[:document].to_html.html_safe)
|
2016-07-04 01:31:43 -04:00
|
|
|
object.user_visible_reference_count = redacted_data[:visible_reference_count]
|
2016-06-21 07:35:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Renders the attribute of every given object.
|
|
|
|
def render_objects(objects, attribute)
|
2016-07-05 11:21:13 -04:00
|
|
|
render_attributes(objects, attribute)
|
2016-06-21 07:35:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Redacts the list of documents.
|
|
|
|
#
|
|
|
|
# Returns an Array containing the redacted documents.
|
|
|
|
def redact_documents(documents)
|
|
|
|
redactor = Redactor.new(project, user)
|
|
|
|
|
2016-07-04 01:31:43 -04:00
|
|
|
redactor.redact(documents)
|
2016-06-21 07:35:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a Banzai context for the given object and attribute.
|
|
|
|
def context_for(object, attribute)
|
2016-10-06 17:52:44 -04:00
|
|
|
context = base_context.dup
|
|
|
|
context = context.merge(object.banzai_render_context(attribute))
|
2016-06-21 07:35:09 -04:00
|
|
|
context
|
|
|
|
end
|
|
|
|
|
2016-07-05 11:21:13 -04:00
|
|
|
# Renders the attributes of a set of objects.
|
2016-06-21 07:35:09 -04:00
|
|
|
#
|
2016-07-05 11:21:13 -04:00
|
|
|
# Returns an Array of `Nokogiri::HTML::Document`.
|
|
|
|
def render_attributes(objects, attribute)
|
2016-10-06 17:52:44 -04:00
|
|
|
objects.map do |object|
|
|
|
|
string = Banzai.render_field(object, attribute)
|
2016-07-05 11:21:13 -04:00
|
|
|
context = context_for(object, attribute)
|
|
|
|
|
2016-10-06 17:52:44 -04:00
|
|
|
Banzai::Pipeline[:relative_link].to_document(string, context)
|
2016-07-05 11:21:13 -04:00
|
|
|
end
|
2016-06-21 07:35:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def base_context
|
2016-10-06 17:52:44 -04:00
|
|
|
@base_context ||= @redaction_context.merge(current_user: user, project: project)
|
2016-06-21 07:35:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|