Skip to content

Commit cb515f8

Browse files
committed
wip
1 parent 100c0b3 commit cb515f8

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

lib/active_model/serializer/association.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ def reflection
3434
reflection_options.fetch(:reflection)
3535
end
3636

37+
def to_many?
38+
reflection.name == :has_many
39+
end
40+
41+
def belongs_to?
42+
reflection.foreign_key_on == :self
43+
end
44+
3745
def serializer
3846
association_value = reflection_options.fetch(:association_value_proc).call
3947
reflection_options.fetch(:serializer_proc).call(association_value, options.dup)

lib/active_model_serializers/adapter/json_api.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ def relationships_for(serializer, requested_associations)
440440
serializer.associations(include_directive).each_with_object({}) do |association, hash|
441441
hash[association.key] = Relationship.new(
442442
serializer,
443-
association.serializer,
443+
association,
444444
instance_options,
445445
options: association.options,
446446
links: association.links,

lib/active_model_serializers/adapter/json_api/relationship.rb

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ class Relationship
66
# {http://jsonapi.org/format/#document-links Document Links}
77
# {http://jsonapi.org/format/#document-resource-object-linkage Document Resource Relationship Linkage}
88
# {http://jsonapi.org/format/#document-meta Docment Meta}
9-
def initialize(parent_serializer, serializer, serializable_resource_options, args = {})
9+
def initialize(parent_serializer, association, serializable_resource_options, args = {})
1010
@object = parent_serializer.object
1111
@scope = parent_serializer.scope
1212
@association_options = args.fetch(:options, {})
1313
@serializable_resource_options = serializable_resource_options
14-
@data = data_for(serializer)
14+
@data = data_for(association)
1515
@links = args.fetch(:links, {}).each_with_object({}) do |(key, value), hash|
1616
result = Link.new(parent_serializer, value).as_json
1717
hash[key] = result if result
@@ -38,13 +38,23 @@ def as_json
3838

3939
private
4040

41-
def data_for(serializer)
42-
if serializer.respond_to?(:each)
41+
def data_for(association)
42+
if association.to_many?
43+
serializer = association.serializer
4344
serializer.map { |s| ResourceIdentifier.new(s, serializable_resource_options).as_json }
4445
elsif association_options[:virtual_value]
4546
association_options[:virtual_value]
46-
elsif serializer && serializer.object
47-
ResourceIdentifier.new(serializer, serializable_resource_options).as_json
47+
elsif association.belongs_to?
48+
foreign_key = association.reflection.foreign_key
49+
if @object.respond_to?(foreign_key)
50+
{
51+
id: @object.send(foreign_key),
52+
type: association.reflection.type.to_s
53+
}
54+
else
55+
serializer && serializer.object &&
56+
ResourceIdentifier.new(serializer, serializable_resource_options).as_json
57+
end
4858
end
4959
end
5060
end

0 commit comments

Comments
 (0)