Class: Banzai::ObjectRenderer
- Inherits:
-
Object
- Object
- Banzai::ObjectRenderer
- Defined in:
- lib/banzai/object_renderer.rb
Overview
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.
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`.
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
Instance Method Summary collapse
-
#initialize(default_project: nil, user: nil, redaction_context: {}) ⇒ ObjectRenderer
constructor
default_project - A default Project to use for redacting Markdown.
-
#render(objects, attribute) ⇒ Object
Renders and redacts an Array of objects.
Constructor Details
#initialize(default_project: nil, user: nil, redaction_context: {}) ⇒ ObjectRenderer
default_project - A default Project to use for redacting Markdown. user - The user viewing the Markdown/HTML documents, if any. redaction_context - A Hash containing extra attributes to use during redaction
23 24 25 26 |
# File 'lib/banzai/object_renderer.rb', line 23 def initialize(default_project: nil, user: nil, redaction_context: {}) @context = RenderContext.new(default_project, user) @redaction_context = base_context.merge(redaction_context) end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
18 19 20 |
# File 'lib/banzai/object_renderer.rb', line 18 def context @context end |
Instance Method Details
#render(objects, attribute) ⇒ Object
Renders and redacts an Array of objects.
objects - The objects to render. attribute - The attribute containing the raw Markdown to render.
Returns the same input objects.
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/banzai/object_renderer.rb', line 34 def render(objects, attribute) documents = render_documents(objects, attribute) documents = post_process_documents(documents, objects, attribute) redacted = redact_documents(documents) objects.each_with_index do |object, index| redacted_data = redacted[index] object.__send__("redacted_#{attribute}_html=", redacted_data[:document].to_html().html_safe) # rubocop:disable GitlabSecurity/PublicSend object.user_visible_reference_count = redacted_data[:visible_reference_count] if object.respond_to?(:user_visible_reference_count) object.total_reference_count = redacted_data[:total_reference_count] if object.respond_to?(:total_reference_count) end end |