Module: JsonapiSerializer::Polymorphic
Instance Method Summary
collapse
Methods included from Common
#serializable_hash, #serialized_json, #type
Methods included from Utils
#apply_splat, #key_intersect, #normalize_fields, #normalize_includes
Instance Method Details
#attributes_hash(record) ⇒ Object
34
35
36
|
# File 'lib/jsonapi_serializer/polymorphic.rb', line 34
def attributes_hash(record)
serializer_for(record).attributes_hash(record)
end
|
#id_hash(record) ⇒ Object
30
31
32
|
# File 'lib/jsonapi_serializer/polymorphic.rb', line 30
def id_hash(record)
serializer_for(record).id_hash(record)
end
|
#initialize(opts = {}) ⇒ Object
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/jsonapi_serializer/polymorphic.rb', line 15
def initialize(opts = {})
super(opts)
unless self.class.meta_inherited
poly_fields = [*opts.dig(:fields, @type)].map { |f| JsonapiSerializer.key_transform(f) }
if self.class.meta_poly.present?
@poly = self.class.meta_poly.each_with_object({}) do |poly_class, hash|
serializer = poly_class.constantize.new(opts.merge poly_fields: poly_fields)
hash[serializer.type] = serializer
end
else
raise "You have to create at least one children serializer for polymorphic #{self.class.name}"
end
end
end
|
#record_hash(record, context = {}) ⇒ Object
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/jsonapi_serializer/polymorphic.rb', line 42
def record_hash(record, context = {})
hash = id_hash(record)
attributes = attributes_hash(record)
hash[:attributes] = attributes if attributes.present?
relationships = relationships_hash(record, context)
hash[:relationships] = relationships if relationships.present?
hash
end
|
#relationships_hash(record, context = {}) ⇒ Object
38
39
40
|
# File 'lib/jsonapi_serializer/polymorphic.rb', line 38
def relationships_hash(record, context = {})
serializer_for(record).relationships_hash(record, context)
end
|