2016-09-24 03:00:46 +00:00
|
|
|
# frozen_string_literal: true
|
2017-11-06 16:15:18 +00:00
|
|
|
|
2016-09-21 17:22:40 +00:00
|
|
|
module Api
|
|
|
|
module V2
|
|
|
|
class ApplicationSerializer < ActiveModel::Serializer
|
|
|
|
def self.embeddable
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
|
|
|
|
def embeds
|
2016-09-24 04:27:34 +00:00
|
|
|
# subclasses can override self.embeddable, and then it will whitelist
|
|
|
|
# scope[:embeds] based on the contents. That way scope[:embeds] can just pull
|
|
|
|
# from params and the whitelisting happens here
|
2016-09-21 17:22:40 +00:00
|
|
|
@embeds ||= (scope[:embeds] || []).select { |e| self.class.embeddable.keys.include?(e) }
|
|
|
|
end
|
|
|
|
|
2017-03-09 19:24:52 +00:00
|
|
|
# Here's an example object that could be passed in self.embeddable: {
|
|
|
|
# creator: {
|
|
|
|
# serializer: UserSerializer,
|
|
|
|
# },
|
|
|
|
# collaborators: {
|
|
|
|
# serializer: UserSerializer
|
|
|
|
# },
|
|
|
|
# topic: {},
|
|
|
|
# synapses: {}
|
|
|
|
# }
|
|
|
|
# The key has to be in embeddable or it won't show in the response, and the serializer is
|
|
|
|
# only needed if the key doesn't match a serializer
|
2016-09-21 17:22:40 +00:00
|
|
|
def self.embed_dat
|
|
|
|
embeddable.each_pair do |key, opts|
|
2017-03-09 19:24:52 +00:00
|
|
|
is_plural = key.to_s.pluralize == key.to_s
|
|
|
|
id_key = key.to_s.singularize + (is_plural ? '_ids' : '_id')
|
|
|
|
serializer = opts.delete(:serializer) || "Api::V2::#{key.to_s.singularize.camelize}Serializer".constantize
|
|
|
|
if is_plural
|
|
|
|
attribute(id_key.to_sym, opts.merge(unless: -> { embeds.include?(key) })) do
|
|
|
|
Pundit.policy_scope(scope[:current_user], object.send(key))&.map(&:id) || []
|
2016-10-26 11:56:30 +00:00
|
|
|
end
|
2017-03-09 19:24:52 +00:00
|
|
|
has_many(key, opts.merge(if: -> { embeds.include?(key) })) do
|
|
|
|
list = Pundit.policy_scope(scope[:current_user], object.send(key)) || []
|
2017-01-19 19:49:40 +00:00
|
|
|
resource = ActiveModelSerializers::SerializableResource.new(
|
|
|
|
list,
|
2017-03-09 19:24:52 +00:00
|
|
|
each_serializer: serializer,
|
2017-01-19 19:49:40 +00:00
|
|
|
scope: scope.merge(embeds: [])
|
|
|
|
)
|
2017-03-09 19:24:52 +00:00
|
|
|
# resource.as_json will return e.g. { users: [ ... ] } for collaborators
|
|
|
|
# since we can't get the :users key, convert to an array and use .first.second to get the needed values
|
|
|
|
resource&.as_json&.to_a&.first&.second
|
2016-09-21 17:22:40 +00:00
|
|
|
end
|
|
|
|
else
|
2017-03-09 19:24:52 +00:00
|
|
|
attribute(id_key.to_sym, opts.merge(unless: -> { embeds.include?(key) }))
|
|
|
|
attribute(key, opts.merge(if: -> { embeds.include?(key) })) do |parent_serializer|
|
|
|
|
object = parent_serializer.object.send(key)
|
|
|
|
next nil if object.nil?
|
2017-01-19 19:49:40 +00:00
|
|
|
resource = ActiveModelSerializers::SerializableResource.new(
|
|
|
|
object,
|
2017-03-09 19:24:52 +00:00
|
|
|
serializer: serializer,
|
2017-01-19 19:49:40 +00:00
|
|
|
scope: scope.merge(embeds: [])
|
|
|
|
)
|
2017-03-09 19:24:52 +00:00
|
|
|
resource&.as_json&.to_a&.first&.second
|
2017-01-19 19:49:40 +00:00
|
|
|
end
|
2016-09-21 17:22:40 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|