Skip to content

Commit ee95988

Browse files
committed
polymorphic_relationships
1 parent 9d05d83 commit ee95988

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

lib/jsonapi/serializer.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ defmodule JSONAPI.Serializer do
8080

8181
@spec encode_relationships(Conn.t(), document(), tuple(), list()) :: tuple()
8282
def encode_relationships(conn, doc, {view, data, _, _} = view_info, options) do
83-
view.relationships()
83+
view.resource_relationships(data)
8484
|> Enum.filter(&assoc_loaded?(Map.get(data, get_data_key(&1))))
8585
|> Enum.map_reduce(doc, &build_relationships(conn, view_info, &1, &2, options))
8686
end

lib/jsonapi/view.ex

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ defmodule JSONAPI.View do
157157
@callback relationships() :: [
158158
{atom(), t() | {t(), :include} | {atom(), t()} | {atom(), t(), :include}}
159159
]
160+
@callback polymorphic_relationships(data()) :: [
161+
{atom(), t() | {t(), :include} | {atom(), t()} | {atom(), t(), :include}}
162+
]
160163
@callback type() :: resource_type()
161164
@callback polymorphic_type(data()) :: resource_type()
162165
@callback url_for(data(), Conn.t() | nil) :: String.t()
@@ -259,6 +262,9 @@ defmodule JSONAPI.View do
259262
@impl View
260263
def relationships, do: []
261264

265+
@impl View
266+
def polymorphic_relationships(_data), do: []
267+
262268
cond do
263269
@resource_type ->
264270
@impl View
@@ -314,6 +320,14 @@ defmodule JSONAPI.View do
314320
end
315321
end
316322

323+
def resource_relationships(data) do
324+
if @polymorphic_resource do
325+
polymorphic_relationships(data)
326+
else
327+
relationships()
328+
end
329+
end
330+
317331
defoverridable View
318332

319333
def index(models, conn, _params, meta \\ nil, options \\ []),

0 commit comments

Comments
 (0)