mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
As shown here : https://github.com/json-api/json-api/blob/gh-pages/format/index.md#resource-relationships-
40 lines
933 B
Ruby
40 lines
933 B
Ruby
module ActiveModel
|
|
class Serializer
|
|
class Association
|
|
class HasMany < Association
|
|
def initialize(name, *args)
|
|
super
|
|
@root_key = @embedded_key
|
|
@key ||= case CONFIG.default_key_type
|
|
when :name then name.to_s.pluralize
|
|
else "#{name.to_s.singularize}_ids"
|
|
end
|
|
|
|
end
|
|
|
|
def serializer_class(object, _)
|
|
if use_array_serializer?
|
|
ArraySerializer
|
|
else
|
|
serializer_from_options
|
|
end
|
|
end
|
|
|
|
def options
|
|
if use_array_serializer?
|
|
{ each_serializer: serializer_from_options }.merge! super
|
|
else
|
|
super
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def use_array_serializer?
|
|
!serializer_from_options ||
|
|
serializer_from_options && !(serializer_from_options <= ArraySerializer)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end |