Skip to content

Commit 5f4a3a5

Browse files
committed
notes
1 parent f2801a2 commit 5f4a3a5

File tree

9 files changed

+150
-2
lines changed

9 files changed

+150
-2
lines changed

lib/active_model/serializer/association.rb

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ def initialize(*)
2626
options[:namespace] ||= parent_serializer_options[:namespace]
2727
end
2828

29+
def collection?
30+
reflection.to_many?
31+
end
32+
33+
def belongs_to?
34+
reflection.foreign_key_on == :self
35+
end
36+
37+
def polymorphic_type
38+
"#{reflection.type.to_s.singularize}_type"
39+
end
40+
2941
# @return [Symbol]
3042
def key
3143
options.fetch(:key, name)
@@ -89,7 +101,17 @@ def serializer_class
89101
return @serializer_class if defined?(@serializer_class)
90102
serializer_for_options = { namespace: options[:namespace] }
91103
serializer_for_options[:serializer] = options[:serializer] if options.key?(:serializer)
92-
@serializer_class = parent_serializer.class.serializer_for(object, serializer_for_options)
104+
resource_class =
105+
if collection?
106+
[]
107+
elsif reflection.options[:class_name] # due to namespacing, without a hint, we cannot rely on constantizing
108+
reflection.class_name.safe_constantize or object.class
109+
else
110+
object.class
111+
end
112+
113+
parent_serializer_class = parent_serializer.class
114+
@serializer_class = parent_serializer_class.serializer_for(resource_class, serializer_for_options)
93115
end
94116
end
95117
end

lib/active_model/serializer/belongs_to_reflection.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ module ActiveModel
22
class Serializer
33
# @api private
44
class BelongsToReflection < SingularReflection
5+
def self.type; :belongs_to; end
6+
7+
def foreign_key_on
8+
:self
9+
end
510
end
611
end
712
end

lib/active_model/serializer/collection_reflection.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ module ActiveModel
22
class Serializer
33
# @api private
44
class CollectionReflection < Reflection
5+
def to_many?
6+
true
7+
end
58
end
69
end
710
end

lib/active_model/serializer/concerns/associations.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,60 @@ def associate(reflection)
8383
# +default_include_directive+ config value when not provided)
8484
# @return [Enumerator<Association>]
8585
#
86+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:**=>{}}}, :include_slice=>nil}
87+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:*=>{:*=>{}}}}, :include_slice=>nil}
88+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:*=>{}}}, :include_slice=>nil}
89+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:*=>{}}}, :include_slice=>{:*=>{:*=>{}}}}
90+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:*=>{}}}, :include_slice=>{:*=>{}}}
91+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:*=>{}}}, :include_slice=>{:author=>{:*=>{}}}}
92+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:*=>{}}}, :include_slice=>{:author=>{:posts=>{}}}}
93+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:*=>{}}}, :include_slice=>{:author=>{:roles=>{}}}}
94+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:*=>{}}}, :include_slice=>{:author=>{}, :" comments"=>{}}}
95+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:*=>{}}}, :include_slice=>{:author=>{}}}
96+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:*=>{}}}, :include_slice=>{:bio=>{}, :posts=>{}}}
97+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:*=>{}}}, :include_slice=>{:bio=>{}}}
98+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:*=>{}}}, :include_slice=>{:comments=>{:author=>{}}}}
99+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:*=>{}}}, :include_slice=>{:comments=>{}, :author=>{:bio=>{}}}}
100+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:*=>{}}}, :include_slice=>{:comments=>{}, :author=>{}}}
101+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:*=>{}}}, :include_slice=>{:comments=>{}}}
102+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:*=>{}}}, :include_slice=>{:post=>{}}}
103+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:*=>{}}}, :include_slice=>{:posts=>{:tags=>{}}}}
104+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:*=>{}}}, :include_slice=>{:posts=>{}}}
105+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:*=>{}}}, :include_slice=>{:reviews=>{}}}
106+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:*=>{}}}, :include_slice=>{:roles=>{}}}
107+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:*=>{}}}, :include_slice=>{:tags=>{}, :posts=>{:tags=>{}}}}
108+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:*=>{}}}, :include_slice=>{:tags=>{}}}
109+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:*=>{}}}, :include_slice=>{:writer=>{}, :articles=>{}}}
110+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:*=>{}}}, :include_slice=>{}}
111+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:author=>{:*=>{}}}}, :include_slice=>nil}
112+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:author=>{:posts=>{}}}}, :include_slice=>nil}
113+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:author=>{:roles=>{}}}}, :include_slice=>nil}
114+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:author=>{}, :" comments"=>{}}}, :include_slice=>nil}
115+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:author=>{}}}, :include_slice=>nil}
116+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:author=>{}}}, :include_slice=>{:comments=>{}}}
117+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:author=>{}}}, :include_slice=>{}}
118+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:bio=>{}, :posts=>{}}}, :include_slice=>nil}
119+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:bio=>{}}}, :include_slice=>nil}
120+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:body=>{}}}, :include_slice=>{}}
121+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:comments=>{:author=>{}}}}, :include_slice=>nil}
122+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:comments=>{}, :author=>{:bio=>{}}}}, :include_slice=>nil}
123+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:comments=>{}, :author=>{}}}, :include_slice=>nil}
124+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:comments=>{}}}, :include_slice=>nil}
125+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:id=>{}, :post=>{}, :author=>{}}}, :include_slice=>{}}
126+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:post=>{}}}, :include_slice=>nil}
127+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:posts=>{:comments=>{:author=>{}}}}}, :include_slice=>nil}
128+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:posts=>{:comments=>{}}}}, :include_slice=>nil}
129+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:posts=>{:tags=>{}}}}, :include_slice=>nil}
130+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:posts=>{}}}, :include_slice=>nil}
131+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:reviews=>{}}}, :include_slice=>nil}
132+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:roles=>{}}}, :include_slice=>nil}
133+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:tags=>{}, :posts=>{:tags=>{}}}}, :include_slice=>nil}
134+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:tags=>{}}}, :include_slice=>nil}
135+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:title=>{}, :comments=>{}, :blog=>{}, :author=>{}}}, :include_slice=>{}}
136+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:title=>{}}}, :include_slice=>{}}
137+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{:writer=>{}, :articles=>{}}}, :include_slice=>nil}
138+
# {:include_directive=>{:options=>{:allow_wildcard=>true}, :hash=>{}}, :include_slice=>nil}
139+
# {:include_directive=>{:options=>{}, :hash=>{:address=>{}}}, :include_slice=>nil}
86140
def associations(include_directive = ActiveModelSerializers.default_include_directive, include_slice = nil)
87141
include_slice ||= include_directive
88142
return Enumerator.new unless object

lib/active_model/serializer/has_many_reflection.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module ActiveModel
22
class Serializer
33
# @api private
44
class HasManyReflection < CollectionReflection
5+
def self.type; :has_many; end
56
end
67
end
78
end

lib/active_model/serializer/has_one_reflection.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module ActiveModel
22
class Serializer
33
# @api private
44
class HasOneReflection < SingularReflection
5+
def self.type; :has_one; end
56
end
67
end
78
end

lib/active_model/serializer/reflection.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,29 +34,51 @@ class Serializer
3434
# So you can inspect reflections in your Adapters.
3535
#
3636
class Reflection < Field
37+
attr_reader :foreign_key, :type, :class_name
38+
39+
def self.type; raise NotImplementedError; end
40+
3741
def initialize(*)
3842
super
3943
options[:links] = {}
4044
options[:include_data_value] = Serializer.config.include_data_default
4145
options[:association_meta] = nil
46+
# class_name and type should be used in more cases for inferring a serializer, rather that requiring an instantiated associated object. This can move us in a direction where a serializer on its own an only serialize itself and its associations and only an adapter can get it to serialize deeper. And each association on the serializer can have the serializer set else is inferred.
47+
@class_name = options.fetch(:class_name, name.to_s.camelize.singularize)
48+
@type = class_name.underscore.pluralize.to_sym
49+
@foreign_key =
50+
if self.class.type == :has_many
51+
"#{name.to_s.singularize}_ids".to_sym
52+
else
53+
"#{name}_id".to_sym
54+
end
55+
end
56+
57+
# @api private
58+
def foreign_key_on
59+
:related
4260
end
4361

62+
# @api public
4463
def link(name, value = nil, &block)
4564
options[:links][name] = block || value
4665
:nil
4766
end
4867

4968
# DONOTMERGE: using options[:meta] conflicted downstream. I haven't cleaned that up yet
69+
# @api public
5070
def meta(value = nil, &block)
5171
options[:association_meta] = block || value
5272
:nil
5373
end
5474

75+
# @api public
5576
def include_data(value = true)
5677
options[:include_data_value] = value
5778
:nil
5879
end
5980

81+
# @api private
6082
def include_data?(include_slice)
6183
include_data_value = options[:include_data_value]
6284
if include_data_value == :if_sideloaded

lib/active_model/serializer/singular_reflection.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ module ActiveModel
22
class Serializer
33
# @api private
44
class SingularReflection < Reflection
5+
def to_many?
6+
false
7+
end
58
end
69
end
710
end

lib/active_model_serializers/adapter/json_api.rb

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,44 @@ def resource_object_for(serializer, include_slice = {})
303303
resource_object
304304
end
305305

306-
requested_associations = fieldset.fields_for(resource_object[:type]) || '*'
306+
requested_associations = fieldset.fields_for(resource_object[:type])
307+
# STDERR.puts([requested_associations, fieldset.fields, resource_object[:type], include_slice.to_hash].inspect)
308+
# include_directive = JSONAPI::IncludeDirective.new(
309+
# requested_associations,
310+
# allow_wildcard: true
311+
# )
312+
# serializer.associations(include_directive, include_slice).each_with_object({}) do |association, hash|
313+
# [ [:author], {:posts=>[:author], :comments=>[:body]}, "posts"]
314+
# [ [:author], {:posts=>[:author]}, "posts"]
315+
# [ [:body], {:posts=>[:author], :comments=>[:body]}, "comments"]
316+
# [ [:id, :post, :author], {:comment=>[:id, :post, :author]}, "comments"]
317+
# [ [:title, :comments, :blog, :author], {:post=>[:title, :comments, :blog, :author]}, "posts"]
318+
# [ [:title, :comments, :blog, :author], {:posts=>[:title, :comments, :blog, :author]}, "posts"]
319+
# [ [:title], {:posts=>[:title]}, "posts"]
320+
# [ nil, {:comment=>[:id, :post, :author]}, "posts"]
321+
# [ nil, {:post=>[:title, :comments, :blog, :author]}, "comments"]
322+
# [ nil, {}, "Posts"]
323+
# [ nil, {}, "active-model-serializer-adapter-json-api-include-param-test-include-param-authors"]
324+
# [ nil, {}, "active-model-serializers-adapter-json-api-links-test-link-authors"]
325+
# [ nil, {}, "active_model-serializer-adapter-json_api-include_param_test-include_param_authors"]
326+
# [ nil, {}, "alerts"]
327+
# [ nil, {}, "ar-models-posts"]
328+
# [ nil, {}, "author"]
329+
# [ nil, {}, "authors"]
330+
# [ nil, {}, "bios"]
331+
# [ nil, {}, "blogs"]
332+
# [ nil, {}, "comments"]
333+
# [ nil, {}, "nested-posts"]
334+
# [ nil, {}, "pictures"]
335+
# [ nil, {}, "posts"]
336+
# [ nil, {}, "profile"]
337+
# [ nil, {}, "profiles"]
338+
# [ nil, {}, "roles"]
339+
# [ nil, {}, "tags"]
340+
# [ nil, {}, "test-models"]
341+
# [ nil, {}, "users"]
342+
# [ nil, {}, "virtual-values"]
343+
requested_associations ||= '*'
307344
relationships = relationships_for(serializer, requested_associations, include_slice)
308345
resource_object[:relationships] = relationships if relationships.any?
309346

0 commit comments

Comments
 (0)