reusable embedding code
This commit is contained in:
parent
c7343761aa
commit
8375b32915
11 changed files with 98 additions and 27 deletions
|
@ -34,6 +34,16 @@ module Api
|
|||
"Api::V1::#{resource_name.camelize}Serializer".constantize
|
||||
end
|
||||
|
||||
def default_scope
|
||||
{
|
||||
embeds: embeds
|
||||
}
|
||||
end
|
||||
|
||||
def embeds
|
||||
(params[:embed] || '').split(',').map(&:to_sym)
|
||||
end
|
||||
|
||||
def token_user
|
||||
token = params[:access_token]
|
||||
access_token = Token.find_by_token(token)
|
||||
|
|
30
app/serializers/api/v1/application_serializer.rb
Normal file
30
app/serializers/api/v1/application_serializer.rb
Normal file
|
@ -0,0 +1,30 @@
|
|||
module Api
|
||||
module V1
|
||||
class ApplicationSerializer < ActiveModel::Serializer
|
||||
def self.embeddable
|
||||
{}
|
||||
end
|
||||
|
||||
def embeds
|
||||
@embeds ||= (scope[:embeds] || []).select { |e| self.class.embeddable.keys.include?(e) }
|
||||
end
|
||||
|
||||
def self.embed_dat
|
||||
embeddable.each_pair do |key, opts|
|
||||
attr = opts.delete(:attr) || key
|
||||
binding.pry if key == :topic2
|
||||
if attr.to_s.pluralize == attr.to_s
|
||||
attribute "#{attr.to_s.singularize}_ids".to_sym, opts.merge(unless: -> { embeds.include?(key) }) do
|
||||
object.send(attr).map(&:id)
|
||||
end
|
||||
has_many attr, opts.merge(if: -> { embeds.include?(key) })
|
||||
else
|
||||
id_opts = opts.merge(key: "#{key}_id")
|
||||
attribute "#{attr}_id".to_sym, id_opts.merge(unless: -> { embeds.include?(key) })
|
||||
attribute key, opts.merge(if: -> { embeds.include?(key) })
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,6 +1,6 @@
|
|||
module Api
|
||||
module V1
|
||||
class EventSerializer < ActiveModel::Serializer
|
||||
class EventSerializer < ApplicationSerializer
|
||||
attributes :id, :sequence_id, :kind, :map_id, :created_at
|
||||
|
||||
has_one :actor, serializer: UserSerializer, root: 'users'
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
module Api
|
||||
module V1
|
||||
class MapSerializer < ActiveModel::Serializer
|
||||
class MapSerializer < ApplicationSerializer
|
||||
attributes :id,
|
||||
:name,
|
||||
:desc,
|
||||
|
@ -9,11 +9,19 @@ module Api
|
|||
:created_at,
|
||||
:updated_at
|
||||
|
||||
has_many :topics, serializer: TopicSerializer
|
||||
has_many :synapses, serializer: SynapseSerializer
|
||||
has_many :mappings, serializer: MappingSerializer
|
||||
has_many :contributors, root: :users, serializer: UserSerializer
|
||||
has_many :collaborators, root: :users, serializer: UserSerializer
|
||||
def self.embeddable
|
||||
{
|
||||
topics: {},
|
||||
synapses: {},
|
||||
mappings: {},
|
||||
contributors: { serializer: UserSerializer },
|
||||
collaborators: { serializer: UserSerializer }
|
||||
}
|
||||
end
|
||||
|
||||
self.class_eval do
|
||||
embed_dat
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,21 +1,24 @@
|
|||
module Api
|
||||
module V1
|
||||
class MappingSerializer < ActiveModel::Serializer
|
||||
class MappingSerializer < ApplicationSerializer
|
||||
attributes :id,
|
||||
:xloc,
|
||||
:yloc,
|
||||
:created_at,
|
||||
:updated_at,
|
||||
:mappable_id,
|
||||
:mappable_type
|
||||
|
||||
has_one :user, serializer: UserSerializer
|
||||
has_one :map, serializer: MapSerializer
|
||||
attribute :xloc, if: -> { object.mappable_type == 'Topic' }
|
||||
attribute :yloc, if: -> { object.mappable_type == 'Topic' }
|
||||
|
||||
def filter(keys)
|
||||
keys.delete(:xloc) unless object.mappable_type == 'Topic'
|
||||
keys.delete(:yloc) unless object.mappable_type == 'Topic'
|
||||
keys
|
||||
def self.embeddable
|
||||
{
|
||||
user: {},
|
||||
map: {}
|
||||
}
|
||||
end
|
||||
|
||||
self.class_eval do
|
||||
embed_dat
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
module Api
|
||||
module V1
|
||||
class MetacodeSerializer < ActiveModel::Serializer
|
||||
class MetacodeSerializer < ApplicationSerializer
|
||||
attributes :id,
|
||||
:name,
|
||||
:manual_icon,
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
module Api
|
||||
module V1
|
||||
class SynapseSerializer < ActiveModel::Serializer
|
||||
class SynapseSerializer < ApplicationSerializer
|
||||
attributes :id,
|
||||
:desc,
|
||||
:category,
|
||||
|
@ -9,9 +9,21 @@ module Api
|
|||
:created_at,
|
||||
:updated_at
|
||||
|
||||
has_one :topic1, root: :topics, serializer: TopicSerializer
|
||||
has_one :topic2, root: :topics, serializer: TopicSerializer
|
||||
has_one :user, serializer: UserSerializer
|
||||
#has_one :topic1, root: :topics, serializer: TopicSerializer
|
||||
#has_one :topic2, root: :topics, serializer: TopicSerializer
|
||||
#has_one :user, serializer: UserSerializer
|
||||
|
||||
def self.embeddable
|
||||
{
|
||||
topic1: { attr: :node1, serializer: TopicSerializer },
|
||||
topic2: { attr: :node2, serializer: TopicSerializer },
|
||||
user: {}
|
||||
}
|
||||
end
|
||||
|
||||
self.class_eval do
|
||||
embed_dat
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
module Api
|
||||
module V1
|
||||
class TokenSerializer < ActiveModel::Serializer
|
||||
class TokenSerializer < ApplicationSerializer
|
||||
attributes :id,
|
||||
:token,
|
||||
:description,
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
module Api
|
||||
module V1
|
||||
class TopicSerializer < ActiveModel::Serializer
|
||||
class TopicSerializer < ApplicationSerializer
|
||||
attributes :id,
|
||||
:name,
|
||||
:desc,
|
||||
|
@ -9,8 +9,16 @@ module Api
|
|||
:created_at,
|
||||
:updated_at
|
||||
|
||||
has_one :user, serializer: UserSerializer
|
||||
has_one :metacode, serializer: MetacodeSerializer
|
||||
def self.embeddable
|
||||
{
|
||||
user: {},
|
||||
metacode: {}
|
||||
}
|
||||
end
|
||||
|
||||
self.class_eval do
|
||||
embed_dat
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
module Api
|
||||
module V1
|
||||
class UserSerializer < ActiveModel::Serializer
|
||||
class UserSerializer < ApplicationSerializer
|
||||
attributes :id,
|
||||
:name,
|
||||
:avatar,
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
module Api
|
||||
module V1
|
||||
class WebhookSerializer < ActiveModel::Serializer
|
||||
class WebhookSerializer < ApplicationSerializer
|
||||
attributes :text, :username, :icon_url # , :attachments
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue