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/meta.rb,
lib/active_model_serializers/adapter/json_api/link.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

Direct Known Subclasses

ActiveModel::Serializer::Adapter::JsonApi

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_check, #cached_name, inherited, transform, transform_key_casing!

Constructor Details

- (JsonApi) initialize(serializer, options = {})

Returns a new instance of JsonApi



34
35
36
37
38
# File 'lib/active_model_serializers/adapter/json_api.rb', line 34

def initialize(serializer, options = {})
  super
  @include_tree = ActiveModel::Serializer::IncludeTree.from_include_args(options[:include])
  @fieldset = options[:fieldset] || ActiveModel::Serializer::Fieldset.new(options.delete(:fields))
end

Class Method Details

+ (Object) default_key_transform



40
41
42
# File 'lib/active_model_serializers/adapter/json_api.rb', line 40

def self.default_key_transform
  :dash
end

Instance Method Details

- (Object) failure_document(options)

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


151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/active_model_serializers/adapter/json_api.rb', line 151

def failure_document(options)
  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, options)
    end
  else
    hash[:errors] = Error.resource_errors(serializer, options)
  end
  hash
end

- (Object) fragment_cache(cached_hash, non_cached_hash)



174
175
176
177
178
179
180
181
182
183
184
# File 'lib/active_model_serializers/adapter/json_api.rb', line 174

def fragment_cache(cached_hash, non_cached_hash)
  root = false if instance_options.include?(:include)
  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

- (Object) serializable_hash(options = nil)



46
47
48
49
50
51
52
53
54
# File 'lib/active_model_serializers/adapter/json_api.rb', line 46

def serializable_hash(options = nil)
  options ||= {}
  document = if serializer.success?
               success_document(options)
             else
               failure_document(options)
             end
  self.class.transform_key_casing!(document, options)
end

- (Object) success_document(options)

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? }


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
# File 'lib/active_model_serializers/adapter/json_api.rb', line 71

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

  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, options))
  end

  hash
end