Module: JsonapiSerializer::Common

Extended by:
ActiveSupport::Concern
Included in:
Base, Polymorphic
Defined in:
lib/jsonapi_serializer/common.rb

Instance Method Summary collapse

Instance Method Details

#initialize(opts = {}) ⇒ Object



8
9
10
# File 'lib/jsonapi_serializer/common.rb', line 8

def initialize(opts = {})
  @type = self.class.meta_type || guess_type
end

#serializable_hash(payload, opts = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/jsonapi_serializer/common.rb', line 16

def serializable_hash(payload, opts = {})
  hash = {}
  context = {tracker: {}, included: []}

  if payload.respond_to?(:map)
    hash[:data] = payload.map { |p| record_hash(p, context) }
  else
    hash[:data] = record_hash(payload, context)
  end

  hash[:meta] = opts[:meta] if opts.has_key? :meta
  hash[:included] = context[:included] if context[:included].present?
  hash
end

#serialized_json(payload, opts = {}) ⇒ Object



31
32
33
# File 'lib/jsonapi_serializer/common.rb', line 31

def serialized_json(payload, opts = {})
  MultiJson.dump(serializable_hash payload, opts)
end

#typeObject



12
13
14
# File 'lib/jsonapi_serializer/common.rb', line 12

def type
  @type
end