lib/jsonapi/resource_serializer.rb in jsonapi-resources-0.9.10 vs lib/jsonapi/resource_serializer.rb in jsonapi-resources-0.9.11
- old
+ new
@@ -285,11 +285,10 @@
relationships.each_with_object({}) do |(name, relationship), hash|
ia = include_directives[:include_related][name]
include_linkage = ia && ia[:include]
include_linked_children = ia && !ia[:include_related].empty?
- options = { filters: ia && ia[:include_filters] || {} }
if field_set.include?(name)
ro = relationship_object(source, relationship, include_linkage)
hash[format_key(name)] = ro unless ro.blank?
end
@@ -298,10 +297,11 @@
# through the relationships.
if include_linkage || include_linked_children
resources = if source.preloaded_fragments.has_key?(format_key(name))
source.preloaded_fragments[format_key(name)].values
else
+ options = { filters: ia && ia[:include_filters] || {} }
[source.public_send(name, options)].flatten(1).compact
end
resources.each do |resource|
next if self_referential_and_already_in_source(resource)
id = resource.id
@@ -466,19 +466,23 @@
end
end
# Extracts the foreign key value for a to_one relationship.
def foreign_key_value(source, relationship)
- related_resource_id = if source.preloaded_fragments.has_key?(format_key(relationship.name))
- source.preloaded_fragments[format_key(relationship.name)].values.first.try(:id)
- elsif source.respond_to?("#{relationship.name}_id")
- # If you have direct access to the underlying id, you don't have to load the relationship
- # which can save quite a lot of time when loading a lot of data.
- # This does not apply to e.g. has_one :through relationships.
- source.public_send("#{relationship.name}_id")
- else
- source.public_send(relationship.name).try(:id)
- end
+ # If you have changed the key_name, don't even try to look at `"#{relationship.name}_id"`
+ # just load the association and call the custom key_name
+ foreign_key_type_changed = relationship.options[:foreign_key_type_changed] || false
+ related_resource_id =
+ if source.preloaded_fragments.has_key?(format_key(relationship.name))
+ source.preloaded_fragments[format_key(relationship.name)].values.first.try(:id)
+ elsif !foreign_key_type_changed && source.respond_to?("#{relationship.name}_id")
+ # If you have direct access to the underlying id, you don't have to load the relationship
+ # which can save quite a lot of time when loading a lot of data.
+ # This does not apply to e.g. has_one :through relationships.
+ source.public_send("#{relationship.name}_id")
+ else
+ source.public_send(relationship.name).try(:id)
+ end
return nil unless related_resource_id
@id_formatter.format(related_resource_id)
end
def foreign_key_types_and_values(source, relationship)