lib/jsonapi/resource_serializer.rb in jsonapi-resources-0.10.0.beta3 vs lib/jsonapi/resource_serializer.rb in jsonapi-resources-0.10.0.beta4
- old
+ new
@@ -57,11 +57,11 @@
included_objects.push(serialized_resource)
end
end
end
- fail "To Many primary objects for show" if (primary_objects.count > 1)
+ fail "Too many primary objects for show" if (primary_objects.count > 1)
primary_hash = { 'data' => primary_objects[0] }
primary_hash['included'] = included_objects if included_objects.size > 0
primary_hash
end
@@ -91,28 +91,23 @@
def serialize_related_resource_set_to_hash_plural(resource_set, _source_resource)
return serialize_resource_set_to_hash_plural(resource_set)
end
- def serialize_to_links_hash(source, requested_relationship, resource_ids)
+ def serialize_to_relationship_hash(source, requested_relationship, resource_ids)
if requested_relationship.is_a?(JSONAPI::Relationship::ToOne)
data = to_one_linkage(resource_ids[0])
else
data = to_many_linkage(resource_ids)
end
- {
- 'links' => {
- 'self' => self_link(source, requested_relationship),
- 'related' => related_link(source, requested_relationship)
- },
- 'data' => data
- }
- end
+ rel_hash = { 'data': data }
- def query_link(query_params)
- link_builder.query_link(query_params)
+ links = default_relationship_links(source, requested_relationship)
+ rel_hash['links'] = links unless links.blank?
+
+ rel_hash
end
def format_key(key)
@key_formatter.format(key)
end
@@ -138,11 +133,10 @@
class_name: self.class.name,
seriserialization_options: serialization_options.sort.map(&:as_json),
supplying_attribute_fields: supplying_attribute_fields(resource_klass).sort,
supplying_relationship_fields: supplying_relationship_fields(resource_klass).sort,
link_builder_base_url: link_builder.base_url,
- route_formatter_class: link_builder.route_formatter.uncached.class.name,
key_formatter_class: key_formatter.uncached.class.name,
always_include_to_one_linkage_data: always_include_to_one_linkage_data,
always_include_to_many_linkage_data: always_include_to_many_linkage_data
}
end
@@ -163,11 +157,11 @@
obj_hash['links'] = source.links_json if source.links_json
obj_hash['attributes'] = source.attributes_json if source.attributes_json
relationships = cached_relationships_hash(source, fetchable_fields, relationship_data)
- obj_hash['relationships'] = relationships unless relationships.nil? || relationships.empty?
+ obj_hash['relationships'] = relationships unless relationships.blank?
obj_hash['meta'] = source.meta_json if source.meta_json
else
# TODO Should this maybe be using @id_formatter instead, for consistency?
id_format = source.class._attribute_options(:id)[:format]
@@ -182,11 +176,11 @@
attributes = attributes_hash(source, fetchable_fields)
obj_hash['attributes'] = attributes unless attributes.empty?
relationships = relationships_hash(source, fetchable_fields, relationship_data)
- obj_hash['relationships'] = relationships unless relationships.nil? || relationships.empty?
+ obj_hash['relationships'] = relationships unless relationships.blank?
meta = meta_hash(source)
obj_hash['meta'] = meta unless meta.empty?
end
@@ -197,11 +191,11 @@
def supplying_attribute_fields(resource_klass)
@_supplying_attribute_fields.fetch resource_klass do
attrs = Set.new(resource_klass._attributes.keys.map(&:to_sym))
cur = resource_klass
- while cur != JSONAPI::Resource
+ while !cur.root? # do not traverse beyond the first root resource
if @fields.has_key?(cur._type)
attrs &= @fields[cur._type]
break
end
cur = cur.superclass
@@ -212,11 +206,11 @@
def supplying_relationship_fields(resource_klass)
@_supplying_relationship_fields.fetch resource_klass do
relationships = Set.new(resource_klass._relationships.keys.map(&:to_sym))
cur = resource_klass
- while cur != JSONAPI::Resource
+ while !cur.root? # do not traverse beyond the first root resource
if @fields.has_key?(cur._type)
relationships &= @fields[cur._type]
break
end
cur = cur.superclass
@@ -247,11 +241,13 @@
(meta.is_a?(Hash) && meta) || {}
end
def links_hash(source)
links = custom_links_hash(source)
- links['self'] = link_builder.self_link(source) unless links.key?('self')
+ if !links.key?('self') && !source.class.exclude_link?(:self)
+ links['self'] = link_builder.self_link(source)
+ end
links.compact
end
def custom_links_hash(source)
custom_links = source.custom_links(custom_generation_options)
@@ -272,11 +268,12 @@
else
rids = relationship_data[name]
end
end
- hash[format_key(name)] = link_object(source, relationship, rids, include_data)
+ ro = relationship_object(source, relationship, rids, include_data)
+ hash[format_key(name)] = ro unless ro.blank?
end
end
end
def cached_relationships_hash(source, fetchable_fields, relationship_data)
@@ -321,10 +318,17 @@
def related_link(source, relationship)
link_builder.relationships_related_link(source, relationship)
end
+ def default_relationship_links(source, relationship)
+ links = {}
+ links['self'] = self_link(source, relationship) unless relationship.exclude_link?(:self)
+ links['related'] = related_link(source, relationship) unless relationship.exclude_link?(:related)
+ links.compact
+ end
+
def to_many_linkage(rids)
linkage = []
rids && rids.each do |details|
id = details.id
@@ -344,39 +348,39 @@
'type' => format_key(rid.resource_klass._type),
'id' => @id_formatter.format(rid.id),
}
end
- def link_object_to_one(source, relationship, rid, include_data)
+ def relationship_object_to_one(source, relationship, rid, include_data)
link_object_hash = {}
- link_object_hash['links'] = {}
- link_object_hash['links']['self'] = self_link(source, relationship)
- link_object_hash['links']['related'] = related_link(source, relationship)
+
+ links = default_relationship_links(source, relationship)
+
+ link_object_hash['links'] = links unless links.blank?
link_object_hash['data'] = to_one_linkage(rid) if include_data
link_object_hash
end
- def link_object_to_many(source, relationship, rids, include_data)
+ def relationship_object_to_many(source, relationship, rids, include_data)
link_object_hash = {}
- link_object_hash['links'] = {}
- link_object_hash['links']['self'] = self_link(source, relationship)
- link_object_hash['links']['related'] = related_link(source, relationship)
+
+ links = default_relationship_links(source, relationship)
+ link_object_hash['links'] = links unless links.blank?
link_object_hash['data'] = to_many_linkage(rids) if include_data
link_object_hash
end
- def link_object(source, relationship, rid, include_data)
+ def relationship_object(source, relationship, rid, include_data)
if relationship.is_a?(JSONAPI::Relationship::ToOne)
- link_object_to_one(source, relationship, rid, include_data)
+ relationship_object_to_one(source, relationship, rid, include_data)
elsif relationship.is_a?(JSONAPI::Relationship::ToMany)
- link_object_to_many(source, relationship, rid, include_data)
+ relationship_object_to_many(source, relationship, rid, include_data)
end
end
def generate_link_builder(primary_resource_klass, options)
LinkBuilder.new(
base_url: options.fetch(:base_url, ''),
- route_formatter: options.fetch(:route_formatter, JSONAPI.configuration.route_formatter),
primary_resource_klass: primary_resource_klass,
)
end
end
end