reusable embedding code

This commit is contained in:
Devin Howard 2016-08-20 13:44:26 +08:00
parent c7343761aa
commit 8375b32915
11 changed files with 98 additions and 27 deletions

View file

@ -34,6 +34,16 @@ module Api
"Api::V1::#{resource_name.camelize}Serializer".constantize "Api::V1::#{resource_name.camelize}Serializer".constantize
end end
def default_scope
{
embeds: embeds
}
end
def embeds
(params[:embed] || '').split(',').map(&:to_sym)
end
def token_user def token_user
token = params[:access_token] token = params[:access_token]
access_token = Token.find_by_token(token) access_token = Token.find_by_token(token)

View 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

View file

@ -1,6 +1,6 @@
module Api module Api
module V1 module V1
class EventSerializer < ActiveModel::Serializer class EventSerializer < ApplicationSerializer
attributes :id, :sequence_id, :kind, :map_id, :created_at attributes :id, :sequence_id, :kind, :map_id, :created_at
has_one :actor, serializer: UserSerializer, root: 'users' has_one :actor, serializer: UserSerializer, root: 'users'

View file

@ -1,6 +1,6 @@
module Api module Api
module V1 module V1
class MapSerializer < ActiveModel::Serializer class MapSerializer < ApplicationSerializer
attributes :id, attributes :id,
:name, :name,
:desc, :desc,
@ -9,11 +9,19 @@ module Api
:created_at, :created_at,
:updated_at :updated_at
has_many :topics, serializer: TopicSerializer def self.embeddable
has_many :synapses, serializer: SynapseSerializer {
has_many :mappings, serializer: MappingSerializer topics: {},
has_many :contributors, root: :users, serializer: UserSerializer synapses: {},
has_many :collaborators, root: :users, serializer: UserSerializer mappings: {},
contributors: { serializer: UserSerializer },
collaborators: { serializer: UserSerializer }
}
end
self.class_eval do
embed_dat
end
end end
end end
end end

View file

@ -1,21 +1,24 @@
module Api module Api
module V1 module V1
class MappingSerializer < ActiveModel::Serializer class MappingSerializer < ApplicationSerializer
attributes :id, attributes :id,
:xloc,
:yloc,
:created_at, :created_at,
:updated_at, :updated_at,
:mappable_id, :mappable_id,
:mappable_type :mappable_type
has_one :user, serializer: UserSerializer attribute :xloc, if: -> { object.mappable_type == 'Topic' }
has_one :map, serializer: MapSerializer attribute :yloc, if: -> { object.mappable_type == 'Topic' }
def filter(keys) def self.embeddable
keys.delete(:xloc) unless object.mappable_type == 'Topic' {
keys.delete(:yloc) unless object.mappable_type == 'Topic' user: {},
keys map: {}
}
end
self.class_eval do
embed_dat
end end
end end
end end

View file

@ -1,6 +1,6 @@
module Api module Api
module V1 module V1
class MetacodeSerializer < ActiveModel::Serializer class MetacodeSerializer < ApplicationSerializer
attributes :id, attributes :id,
:name, :name,
:manual_icon, :manual_icon,

View file

@ -1,6 +1,6 @@
module Api module Api
module V1 module V1
class SynapseSerializer < ActiveModel::Serializer class SynapseSerializer < ApplicationSerializer
attributes :id, attributes :id,
:desc, :desc,
:category, :category,
@ -9,9 +9,21 @@ module Api
:created_at, :created_at,
:updated_at :updated_at
has_one :topic1, root: :topics, serializer: TopicSerializer #has_one :topic1, root: :topics, serializer: TopicSerializer
has_one :topic2, root: :topics, serializer: TopicSerializer #has_one :topic2, root: :topics, serializer: TopicSerializer
has_one :user, serializer: UserSerializer #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 end
end end

View file

@ -1,6 +1,6 @@
module Api module Api
module V1 module V1
class TokenSerializer < ActiveModel::Serializer class TokenSerializer < ApplicationSerializer
attributes :id, attributes :id,
:token, :token,
:description, :description,

View file

@ -1,6 +1,6 @@
module Api module Api
module V1 module V1
class TopicSerializer < ActiveModel::Serializer class TopicSerializer < ApplicationSerializer
attributes :id, attributes :id,
:name, :name,
:desc, :desc,
@ -9,8 +9,16 @@ module Api
:created_at, :created_at,
:updated_at :updated_at
has_one :user, serializer: UserSerializer def self.embeddable
has_one :metacode, serializer: MetacodeSerializer {
user: {},
metacode: {}
}
end
self.class_eval do
embed_dat
end
end end
end end
end end

View file

@ -1,6 +1,6 @@
module Api module Api
module V1 module V1
class UserSerializer < ActiveModel::Serializer class UserSerializer < ApplicationSerializer
attributes :id, attributes :id,
:name, :name,
:avatar, :avatar,

View file

@ -1,6 +1,6 @@
module Api module Api
module V1 module V1
class WebhookSerializer < ActiveModel::Serializer class WebhookSerializer < ApplicationSerializer
attributes :text, :username, :icon_url # , :attachments attributes :text, :username, :icon_url # , :attachments
end end
end end