fix has_many api embeds too!
This commit is contained in:
parent
a6fadc482f
commit
9e2b7e267e
1 changed files with 16 additions and 5 deletions
|
@ -34,17 +34,28 @@ module Api
|
||||||
opts.merge(unless: -> { embeds.include?(key) })) do
|
opts.merge(unless: -> { embeds.include?(key) })) do
|
||||||
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 |serializer|
|
||||||
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) })) do |serializer|
|
attribute(key, opts.merge(if: -> { embeds.include?(key) })) do |serializer|
|
||||||
child_object = serializer.object.send(key)
|
object = serializer.object.send(key)
|
||||||
child_serializer = "Api::V2::#{child_object.class.name}Serializer".constantize
|
child_serializer = "Api::V2::#{object.class.name}Serializer".constantize
|
||||||
resource = child_serializer.new(child_object, scope: serializer.scope.merge(embeds: []))
|
resource = ActiveModelSerializers::SerializableResource.new(
|
||||||
|
object,
|
||||||
|
serializer: child_serializer,
|
||||||
|
scope: scope.merge(embeds: [])
|
||||||
|
)
|
||||||
resource.as_json
|
resource.as_json
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue