* use correct serializer for singular embeds in api (almost fixes #998) * fix has_many api embeds too! * unused arg
This commit is contained in:
parent
460de840b6
commit
a9f19815e4
1 changed files with 18 additions and 2 deletions
|
@ -35,13 +35,29 @@ module Api
|
||||||
Pundit.policy_scope(scope[:current_user], object.send(attr))&.map(&:id) || []
|
Pundit.policy_scope(scope[:current_user], object.send(attr))&.map(&:id) || []
|
||||||
end
|
end
|
||||||
has_many(attr, opts.merge(if: -> { embeds.include?(key) })) do
|
has_many(attr, opts.merge(if: -> { embeds.include?(key) })) do
|
||||||
Pundit.policy_scope(scope[:current_user], object.send(attr)) || []
|
list = Pundit.policy_scope(scope[:current_user], object.send(attr)) || []
|
||||||
|
child_serializer = "Api::V2::#{attr.to_s.singularize.camelize}Serializer".constantize
|
||||||
|
resource = ActiveModelSerializers::SerializableResource.new(
|
||||||
|
list,
|
||||||
|
each_serializer: child_serializer,
|
||||||
|
scope: scope.merge(embeds: [])
|
||||||
|
)
|
||||||
|
resource.as_json
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
id_opts = opts.merge(key: "#{key}_id")
|
id_opts = opts.merge(key: "#{key}_id")
|
||||||
attribute("#{attr}_id".to_sym,
|
attribute("#{attr}_id".to_sym,
|
||||||
id_opts.merge(unless: -> { embeds.include?(key) }))
|
id_opts.merge(unless: -> { embeds.include?(key) }))
|
||||||
attribute(key, opts.merge(if: -> { embeds.include?(key) }))
|
attribute(key, opts.merge(if: -> { embeds.include?(key) })) do |serializer|
|
||||||
|
object = serializer.object.send(key)
|
||||||
|
child_serializer = "Api::V2::#{object.class.name}Serializer".constantize
|
||||||
|
resource = ActiveModelSerializers::SerializableResource.new(
|
||||||
|
object,
|
||||||
|
serializer: child_serializer,
|
||||||
|
scope: scope.merge(embeds: [])
|
||||||
|
)
|
||||||
|
resource.as_json
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue