Class: ActiveModelSerializers::Adapter::JsonApi

Inherits:
Base
  • Object
show all
Extended by:
ActiveSupport::Autoload
Defined in:
lib/active_model_serializers/adapter/json_api.rb,
lib/active_model_serializers/adapter/json_api/link.rb,
lib/active_model_serializers/adapter/json_api/meta.rb,
lib/active_model_serializers/adapter/json_api/error.rb,
lib/active_model_serializers/adapter/json_api/jsonapi.rb,
lib/active_model_serializers/adapter/json_api/relationship.rb,
lib/active_model_serializers/adapter/json_api/deserialization.rb,
lib/active_model_serializers/adapter/json_api/pagination_links.rb,
lib/active_model_serializers/adapter/json_api/resource_identifier.rb

Defined Under Namespace

Modules: Deserialization, Error, Jsonapi Classes: Link, Meta, PaginationLinks, Relationship, ResourceIdentifier

Instance Attribute Summary

Attributes inherited from Base

#instance_options, #serializer

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#as_json, cache_key, #cache_key, inherited, transform, transform_key_casing!

Constructor Details

#initialize(serializer, options = {}) ⇒ JsonApi

Returns a new instance of JsonApi.



53
54
55
56
57
# File 'lib/active_model_serializers/adapter/json_api.rb', line 53

def initialize(serializer, options = {})
  super
  @include_directive = JSONAPI::IncludeDirective.new(options[:include], allow_wildcard: true)
  @fieldset = options[:fieldset] || ActiveModel::Serializer::Fieldset.new(options.delete(:fields))
end

Class Method Details

.default_key_transformObject



38
39
40
# File 'lib/active_model_serializers/adapter/json_api.rb', line 38

def self.default_key_transform
  :dash
end

.fragment_cache(cached_hash, non_cached_hash, root = true) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/active_model_serializers/adapter/json_api.rb', line 42

def self.fragment_cache(cached_hash, non_cached_hash, root = true)
  core_cached       = cached_hash.first
  core_non_cached   = non_cached_hash.first
  no_root_cache     = cached_hash.delete_if { |key, _value| key == core_cached[0] }
  no_root_non_cache = non_cached_hash.delete_if { |key, _value| key == core_non_cached[0] }
  cached_resource   = (core_cached[1]) ? core_cached[1].deep_merge(core_non_cached[1]) : core_non_cached[1]
  hash = root ? { root => cached_resource } : cached_resource

  hash.deep_merge no_root_non_cache.deep_merge no_root_cache
end

Instance Method Details

#failure_documentObject

JSON API Errors TODO: look into caching definition:

 toplevel_errors array (required)
 toplevel_meta
 toplevel_jsonapi

structure:

{
  errors: toplevel_errors,
  meta: toplevel_meta,
  jsonapi: toplevel_jsonapi
}.reject! {|_,v| v.nil? }

prs:

https://github.com/rails-api/active_model_serializers/pull/1004


174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/active_model_serializers/adapter/json_api.rb', line 174

def failure_document
  hash = {}
  # PR Please :)
  # Jsonapi.add!(hash)

  # toplevel_errors
  # definition:
  #   array of unique items of type 'error'
  # structure:
  #   [
  #     error,
  #     error
  #   ]
  if serializer.respond_to?(:each)
    hash[:errors] = serializer.flat_map do |error_serializer|
      Error.resource_errors(error_serializer, instance_options)
    end
  else
    hash[:errors] = Error.resource_errors(serializer, instance_options)
  end
  hash
end

#fragment_cache(cached_hash, non_cached_hash) ⇒ Object



70
71
72
73
# File 'lib/active_model_serializers/adapter/json_api.rb', line 70

def fragment_cache(cached_hash, non_cached_hash)
  root = !instance_options.include?(:include)
  self.class.fragment_cache(cached_hash, non_cached_hash, root)
end

#serializable_hashObject



61
62
63
64
65
66
67
68
# File 'lib/active_model_serializers/adapter/json_api.rb', line 61

def serializable_hash(*)
  document = if serializer.success?
               success_document
             else
               failure_document
             end
  self.class.transform_key_casing!(document, instance_options)
end

#success_documentObject

Primary data definition:

 toplevel_data (required)
 toplevel_included
 toplevel_meta
 toplevel_links
 toplevel_jsonapi

structure:

{
  data: toplevel_data,
  included: toplevel_included,
  meta: toplevel_meta,
  links: toplevel_links,
  jsonapi: toplevel_jsonapi
}.reject! {|_,v| v.nil? }

rubocop:disable Metrics/CyclomaticComplexity



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/active_model_serializers/adapter/json_api.rb', line 91

def success_document
  is_collection = serializer.respond_to?(:each)
  serializers = is_collection ? serializer : [serializer]
  primary_data, included = resource_objects_for(serializers)

  hash = {}
  # toplevel_data
  # definition:
  #   oneOf
  #     resource
  #     array of unique items of type 'resource'
  #     null
  #
  # description:
  #   The document's "primary data" is a representation of the resource or collection of resources
  #   targeted by a request.
  #
  #   Singular: the resource object.
  #
  #   Collection: one of an array of resource objects, an array of resource identifier objects, or
  #   an empty array ([]), for requests that target resource collections.
  #
  #   None: null if the request is one that might correspond to a single resource, but doesn't currently.
  # structure:
  #  if serializable_resource.resource?
  #    resource
  #  elsif serializable_resource.collection?
  #    [
  #      resource,
  #      resource
  #    ]
  #  else
  #    nil
  #  end
  hash[:data] = is_collection ? primary_data : primary_data[0]
  # toplevel_included
  #   alias included
  # definition:
  #   array of unique items of type 'resource'
  #
  # description:
  #   To reduce the number of HTTP requests, servers **MAY** allow
  #   responses that include related resources along with the requested primary
  #   resources. Such responses are called "compound documents".
  # structure:
  #     [
  #       resource,
  #       resource
  #     ]
  hash[:included] = included if included.any?

  Jsonapi.add!(hash)

  if instance_options[:links]
    hash[:links] ||= {}
    hash[:links].update(instance_options[:links])
  end

  if is_collection && serializer.paginated?
    hash[:links] ||= {}
    hash[:links].update(pagination_links_for(serializer))
  end

  hash[:meta] = instance_options[:meta] unless instance_options[:meta].blank?

  hash
end