Class: Jbuilder::Schema::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/jbuilder/schema/renderer.rb

Constant Summary collapse

@@view_renderer =
ActionView::Base.with_empty_template_cache

Instance Method Summary collapse

Constructor Details

#initialize(paths, default_locals = nil) ⇒ Renderer

Returns a new instance of Renderer.



8
9
10
11
# File 'lib/jbuilder/schema/renderer.rb', line 8

def initialize(paths, default_locals = nil)
  @view_renderer = @@view_renderer.with_view_paths(paths)
  @default_locals = default_locals
end

Instance Method Details

#jsonObject



17
18
19
# File 'lib/jbuilder/schema/renderer.rb', line 17

def json(...)
  normalize(render(...)).to_json
end

#original_render(options = {}, locals = {}) ⇒ Object

Thin wrapper around the regular Jbuilder JSON output render, which also parses it into a hash.



46
47
48
# File 'lib/jbuilder/schema/renderer.rb', line 46

def original_render(options = {}, locals = {})
  JSON.parse @view_renderer.render(options, locals)
end

#render(object = nil, title: nil, description: nil, assigns: nil, **options) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/jbuilder/schema/renderer.rb', line 21

def render(object = nil, title: nil, description: nil, assigns: nil, **options)
  @view_renderer.assign assigns if assigns

  partial_path = %i[to_partial_path_for_jbuilder_schema to_partial_path].map { object.public_send(_1) if object.respond_to?(_1) }.compact.first
  if partial_path
    options[:partial] = partial_path
    options[:object] = object
  end

  json = if partial_path
    original_render(options.dup, options.dup)
  else
    original_render(object || options.dup, options.dup)
  end

  options[:locals] ||= {}
  options[:locals].merge! @default_locals if @default_locals
  options[:locals][:__jbuilder_schema_options] = {json: json, object: object, title: title, description: description}

  @view_renderer.render(options).then do |result|
    result.respond_to?(:unwrap_target!) ? result.unwrap_target! : result
  end
end

#yamlObject



13
14
15
# File 'lib/jbuilder/schema/renderer.rb', line 13

def yaml(...)
  normalize(render(...)).to_yaml
end