Class: SimpleJsonApi::ResourceSerializer
Overview
The Serializer will serialize a model
Instance Attribute Summary
Attributes inherited from Serializer
#_builder, #_each_serializer, #_object
Attributes included from DSL
#_attributes, #_default_fields, #_required_associations
Instance Method Summary
collapse
Methods inherited from Serializer
#associated_object, for, for_polymorphic, for_regular, includes, #initialize, register_serializer
Methods included from DSL
#attribute, #belongs_to, #default_attributes, #default_fields, #has_many, #has_one, #inherited, #required_associations, #serializes
Instance Method Details
#_associations ⇒ Object
84
85
86
|
# File 'lib/simple_json_api/resource_serializer.rb', line 84
def _associations
self.class._associations
end
|
#_fields ⇒ Object
23
24
25
|
# File 'lib/simple_json_api/resource_serializer.rb', line 23
def _fields
@_fields ||= _get_fields
end
|
#_get_fields ⇒ Object
If default_fields is not specified, use all_fields
28
29
30
|
# File 'lib/simple_json_api/resource_serializer.rb', line 28
def _get_fields
builder_fields
end
|
#_root_name ⇒ Object
11
12
13
|
# File 'lib/simple_json_api/resource_serializer.rb', line 11
def _root_name
self.class._root_name
end
|
#attribute_values(hash) ⇒ Object
39
40
41
42
43
|
# File 'lib/simple_json_api/resource_serializer.rb', line 39
def attribute_values(hash)
_fields.each do |attribute, _attr_opts|
hash[attribute] = send(attribute).to_s
end
end
|
#id ⇒ Object
19
20
21
|
# File 'lib/simple_json_api/resource_serializer.rb', line 19
def id
_object.try(:id) || _object.try(:guid)
end
|
#includes(association) ⇒ Object
76
77
78
79
80
81
82
|
# File 'lib/simple_json_api/resource_serializer.rb', line 76
def includes(association)
if association.polymorphic
[]
else
Serializer.includes(association)
end
end
|
#link(association) ⇒ Object
52
53
54
55
56
57
58
|
# File 'lib/simple_json_api/resource_serializer.rb', line 52
def link(association)
if association.type == :has_many
link_many(association)
else
link_one(association)
end
end
|
#link_many(association) ⇒ Object
60
61
62
63
|
# File 'lib/simple_json_api/resource_serializer.rb', line 60
def link_many(association)
resource = send(association.name, includes(association))
{ linkage: linkage_values(resource) }
end
|
#link_one(association) ⇒ Object
65
66
67
68
|
# File 'lib/simple_json_api/resource_serializer.rb', line 65
def link_one(association)
resource = send(association.name)
{ linkage: resource.typed_json_id } if resource
end
|
#link_values(root) ⇒ Object
45
46
47
48
49
50
|
# File 'lib/simple_json_api/resource_serializer.rb', line 45
def link_values(root)
root[:self] = href if self.class.method_defined? :href
self.class._associations.each do |association|
root[association.key] = link(association)
end
end
|
#linkage_values(resource) ⇒ Object
70
71
72
73
74
|
# File 'lib/simple_json_api/resource_serializer.rb', line 70
def linkage_values(resource)
resource.map do |obj|
obj.typed_json_id
end
end
|
#serialize ⇒ Object
32
33
34
35
36
37
|
# File 'lib/simple_json_api/resource_serializer.rb', line 32
def serialize
hash = {}
attribute_values(hash)
link_values(hash[:links] = {})
Resource.new(hash)
end
|
#type ⇒ Object
15
16
17
|
# File 'lib/simple_json_api/resource_serializer.rb', line 15
def type
_root_name
end
|