Class: Pressroom::Blocks::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/pressroom/blocks/renderer.rb

Overview

Walks a stored document and renders each block through its partial.

The strategy is injected rather than read from configuration so this layer stays independent of the engine. Callers pass Pressroom.config.resolved_unknown_block_strategy.

Constant Summary collapse

STRATEGIES =
%i[raise skip].freeze

Instance Method Summary collapse

Constructor Details

#initialize(registry:, sanitizer:, strategy: :raise, logger: nil) ⇒ Renderer

Returns a new instance of Renderer.



15
16
17
18
19
20
# File 'lib/pressroom/blocks/renderer.rb', line 15

def initialize(registry:, sanitizer:, strategy: :raise, logger: nil)
  @registry = registry
  @sanitizer = sanitizer
  @strategy = strategy
  @logger = logger
end

Instance Method Details

#blocks_of(document) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/pressroom/blocks/renderer.rb', line 29

def blocks_of(document)
  case document
  when Array then document.select { |item| item.is_a?(Hash) }
  when Hash  then Array(document["blocks"] || document[:blocks])
                    .select { |item| item.is_a?(Hash) }
  else []
  end
end

#render(document, view_context) ⇒ Object



22
23
24
25
26
27
# File 'lib/pressroom/blocks/renderer.rb', line 22

def render(document, view_context)
  blocks_of(document)
    .filter_map { |block| render_block(block, view_context) }
    .join
    .html_safe
end