Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions lib/jsonapi/resource_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ def initialize(primary_resource_klass, options = {})
@primary_class_name = primary_resource_klass._type
@fields = options.fetch(:fields, {})
@include = options.fetch(:include, [])
@include_links = options.fetch(:include_links, [])
@include_relationship_links = options.fetch(:include_relationship_links, [])
@include_directives = options[:include_directives]
@key_formatter = options.fetch(:key_formatter, JSONAPI.configuration.key_formatter)
@id_formatter = ValueFormatter.value_formatter_for(:id)
Expand Down Expand Up @@ -47,6 +49,7 @@ def serialize_to_hash(source)

included_objects = []
primary_objects = []

@included_objects.each_value do |objects|
objects.each_value do |object|
if object[:primary]
Expand Down Expand Up @@ -109,6 +112,15 @@ def process_primary(source, include_directives)
end
end

def include_links_for?(source)
if primary_class_name.to_s == source._model.class.name.downcase.pluralize
# when the object is the top level primary object
!!@include_links
else
!!@include_relationship_links
end
end

# Returns a serialized hash for the source model
def object_hash(source, include_directives)
obj_hash = {}
Expand All @@ -120,7 +132,7 @@ def object_hash(source, include_directives)

obj_hash['type'] = format_key(source.class._type.to_s)

links = links_hash(source)
links = include_links_for?(source) ? links_hash(source) : []
obj_hash['links'] = links unless links.empty?

attributes = attributes_hash(source)
Expand Down Expand Up @@ -209,7 +221,9 @@ def relationships_hash(source, include_directives)
resources = (include_linkage || include_linked_children) && [source.public_send(name)].flatten.compact

if field_set.include?(name)
hash[format_key(name)] = link_object(source, relationship, include_linkage)
relationship_object = link_object(source, relationship, include_linkage)
relationship_object.delete(:links) unless @include_relationship_links
hash[format_key(name)] = relationship_object
end

# If the object has been serialized once it will be in the related objects list,
Expand Down