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.



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

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

Instance Method Details

#jsonObject



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

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

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



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/jbuilder/schema/renderer.rb', line 20

def render(object = nil, title: nil, description: nil, assigns: nil, **options)
  partial_path = object.respond_to?(:to_partial_path_for_jbuilder_schema) ? object.to_partial_path_for_jbuilder_schema : object.to_partial_path
  options.merge! partial: partial_path, object: object if object

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

  @view_renderer.assign assigns if assigns
  @view_renderer.render(options)
end

#yamlObject



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

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