Class: JsonapiCompliable::Util::RenderOptions Private

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonapi_compliable/util/render_options.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Generate the options we end up passing to jsonapi-rb}

Class Method Summary collapse

Class Method Details

.generate(object, query_hash, overrides = {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/jsonapi_compliable/util/render_options.rb', line 6

def self.generate(object, query_hash, overrides = {})
  resolved = object.respond_to?(:resolve) ? object.resolve : object

  inferrer = ::Hash.new do |h, k|
    names = k.to_s.split('::')
    klass = names.pop
    h[k] = [*names, "Serializable#{klass}"].join('::').safe_constantize
  end

  fields = query_hash[:fields].dup
  extra_fields = query_hash[:extra_fields]

  # Ensure fields doesnt clobber extra fields
  if extra_fields.any? && fields.any?
    extra_fields.each { |k,v| fields[k] = fields[k].to_a + v }
  end

  options            = {}
  options[:class]    = inferrer
  options[:include]  = query_hash[:include]
  options[:jsonapi]  = resolved
  options[:fields]   = fields
  options.merge!(overrides)
  options[:meta]   ||= {}
  options[:expose] ||= {}
  options[:expose][:extra_fields] = extra_fields

  if object.respond_to?(:resolve_stats)
    stats = object.resolve_stats
    options[:meta][:stats] = stats unless stats.empty?
  end

  options
end