Class: ActiveModel::Serializer::Adapter::JsonApi

Inherits:
ActiveModel::Serializer::Adapter show all
Extended by:
ActiveSupport::Autoload
Defined in:
lib/active_model/serializer/adapter/json_api.rb

Defined Under Namespace

Classes: FragmentCache, PaginationLinks

Constant Summary

Constants inherited from ActiveModel::Serializer::Adapter

UnknownAdapterError

Instance Attribute Summary

Attributes inherited from ActiveModel::Serializer::Adapter

#serializer

Instance Method Summary collapse

Methods inherited from ActiveModel::Serializer::Adapter

adapter_class, adapter_map, adapters, #as_json, create, find_by_name, inherited, lookup, register

Constructor Details

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

Returns a new instance of JsonApi.

[View source]

6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/active_model/serializer/adapter/json_api.rb', line 6

def initialize(serializer, options = {})
  super
  @hash = { data: [] }

  @included = ActiveModel::Serializer::Utils.include_args_to_hash(@options[:include])
  fields = options.delete(:fields)
  if fields
    @fieldset = ActiveModel::Serializer::Fieldset.new(fields, serializer.json_key)
  else
    @fieldset = options[:fieldset]
  end
end

Instance Method Details

#fragment_cache(cached_hash, non_cached_hash) ⇒ Object

[View source]

44
45
46
47
# File 'lib/active_model/serializer/adapter/json_api.rb', line 44

def fragment_cache(cached_hash, non_cached_hash)
  root = false if @options.include?(:include)
  ActiveModel::Serializer::Adapter::JsonApi::FragmentCache.new().fragment_cache(root, cached_hash, non_cached_hash)
end

#serializable_hash(options = nil) ⇒ Object

[View source]

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/active_model/serializer/adapter/json_api.rb', line 19

def serializable_hash(options = nil)
  options ||= {}
  if serializer.respond_to?(:each)
    serializer.each do |s|
      result = self.class.new(s, @options.merge(fieldset: @fieldset)).serializable_hash(options)
      @hash[:data] << result[:data]

      if result[:included]
        @hash[:included] ||= []
        @hash[:included] |= result[:included]
      end
    end

    add_links(options)
  else
    primary_data = primary_data_for(serializer, options)
    relationships = relationships_for(serializer)
    included = included_for(serializer)
    @hash[:data] = primary_data
    @hash[:data][:relationships] = relationships if relationships.any?
    @hash[:included] = included if included.any?
  end
  @hash
end