lib/jsonapi/resource_serializer.rb in jsonapi-resources-0.0.14 vs lib/jsonapi/resource_serializer.rb in jsonapi-resources-0.0.15

- old
+ new

@@ -125,13 +125,19 @@ unless requested.nil? fields = requested & fields end fields.each_with_object({}) do |name, hash| - hash[format_key(name)] = format_value(source.send(name), - source.class._attribute_options(name)[:format], - source) + format = source.class._attribute_options(name)[:format] + if format == :default && name == :id + format = 'id' + end + hash[format_key(name)] = format_value( + source.send(name), + format, + source + ) end end # Returns a hash of links for the requested associations for a resource, filtered by the resource # class's fetchable method @@ -146,14 +152,13 @@ field_set = Set.new(fields) included_associations = source.fetchable_fields & associations.keys associations.each_with_object({}) do |(name, association), hash| if included_associations.include? name - foreign_key = association.foreign_key if field_set.include?(name) - hash[format_key(name)] = source.send(foreign_key) + hash[format_key(name)] = foreign_key_value(source, association) end ia = requested_associations.is_a?(Hash) ? requested_associations[name] : nil include_linked_object = ia && ia[:include] @@ -194,9 +199,21 @@ end def already_serialized?(type, id) type = format_key(type) return @linked_objects.key?(type) && @linked_objects[type].key?(id) + end + + # Extracts the foreign key value for an association. + def foreign_key_value(source, association) + foreign_key = association.foreign_key + value = source.send(foreign_key) + + if association.is_a?(JSONAPI::Association::HasMany) + value.map { |value| IdValueFormatter.format(value, {}) } + elsif association.is_a?(JSONAPI::Association::HasOne) + IdValueFormatter.format(value, {}) + end end # Sets that an object should be included in the primary document of the response. def set_primary(type, id) type = format_key(type)