Module: JSONAPI::ResourceActions::Serialization

Extended by:
ActiveSupport::Concern
Includes:
IncludePreloading
Defined in:
lib/json_api/controllers/concerns/resource_actions/serialization.rb

Instance Method Summary collapse

Instance Method Details

#serialize_collection(resources) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/json_api/controllers/concerns/resource_actions/serialization.rb', line 22

def serialize_collection(resources)
  includes = parse_include_param
  fields = parse_fields_param
  resources = scope_with_includes(resources)
  resources_array = resources.to_a

  preload_included_resource_associations(resources_array, includes)

  data, all_included = serialize_resources_with_includes(resources_array, includes, fields)
  build_collection_response(data, all_included)
end

#serialize_resource(resource) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/json_api/controllers/concerns/resource_actions/serialization.rb', line 9

def serialize_resource(resource)
  includes = parse_include_param
  # On show and update the record arrives through `load_resource_record`, which loaded
  # the include tree via `scope_with_includes`. Each included resource's own `records`
  # preloads still have to be applied, or every included record pays them one query at
  # a time. On create the tree is unloaded, so this finds no targets and does nothing.
  preload_included_resource_associations([resource], includes)
  cache = build_include_filter_cache([resource], includes)
  serializer = JSONAPI::Serializer.new(resource, authorization_context: self, include_filter_cache: cache,
                                                 namespace: jsonapi_namespace,)
  serializer.to_hash(include: includes, fields: parse_fields_param, document_meta: jsonapi_document_meta)
end