Skip to content

Commit

Permalink
feat: Link Objects and Additional Controller Actions (#264)
Browse files Browse the repository at this point in the history
* link object & additional View actions

* add link object to test

---------

Co-authored-by: amoffat <[email protected]>
  • Loading branch information
16ajbm and amoffat authored Jun 25, 2024
1 parent 3d3ca63 commit def58a9
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
39 changes: 38 additions & 1 deletion lib/jsonapi/view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ defmodule JSONAPI.View do
@type t :: module()
@type data :: any()
@type field :: atom()
@type links :: %{atom() => String.t()}
@type link_object :: %{required(:href) => String.t(), optional(:meta) => meta()}
@type links :: %{atom() => String.t() | link_object()}
@type meta :: %{atom() => String.t()}
@type options :: keyword()
@type resource_id :: String.t()
Expand Down Expand Up @@ -270,6 +271,15 @@ defmodule JSONAPI.View do
def show(model, conn, _params, meta \\ nil, options \\ []),
do: Serializer.serialize(__MODULE__, model, conn, meta, options)

def update(model, conn, _params, meta \\ nil, options \\ []),
do: Serializer.serialize(__MODULE__, model, conn, meta, options)

def delete(model, conn, _params, meta \\ nil, options \\ []),
do: Serializer.serialize(__MODULE__, model, conn, meta, options)

def create(model, conn, _params, meta \\ nil, options \\ []),
do: Serializer.serialize(__MODULE__, model, conn, meta, options)

if Code.ensure_loaded?(Phoenix) do
def render("show.json", %{data: data, conn: conn, meta: meta, options: options}),
do: Serializer.serialize(__MODULE__, data, conn, meta, options)
Expand All @@ -288,6 +298,33 @@ defmodule JSONAPI.View do

def render("index.json", %{data: data, conn: conn}),
do: Serializer.serialize(__MODULE__, data, conn)

def render("create.json", %{data: data, conn: conn, meta: meta, options: options}),
do: Serializer.serialize(__MODULE__, data, conn, meta, options)

def render("create.json", %{data: data, conn: conn, meta: meta}),
do: Serializer.serialize(__MODULE__, data, conn, meta)

def render("create.json", %{data: data, conn: conn}),
do: Serializer.serialize(__MODULE__, data, conn)

def render("update.json", %{data: data, conn: conn, meta: meta, options: options}),
do: Serializer.serialize(__MODULE__, data, conn, meta, options)

def render("update.json", %{data: data, conn: conn, meta: meta}),
do: Serializer.serialize(__MODULE__, data, conn, meta)

def render("update.json", %{data: data, conn: conn}),
do: Serializer.serialize(__MODULE__, data, conn)

def render("delete.json", %{data: data, conn: conn, meta: meta, options: options}),
do: Serializer.serialize(__MODULE__, data, conn, meta, options)

def render("delete.json", %{data: data, conn: conn, meta: meta}),
do: Serializer.serialize(__MODULE__, data, conn, meta)

def render("delete.json", %{data: data, conn: conn}),
do: Serializer.serialize(__MODULE__, data, conn)
else
raise ArgumentError,
"Attempted to call function that depends on Phoenix. " <>
Expand Down
11 changes: 11 additions & 0 deletions test/jsonapi_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ defmodule JSONAPITest do
def fields, do: [:username]
def type, do: "user"

def links(user, _conn) do
%{
profile: %{
href: "#{path()}/#{user.username}",
meta: %{
method: "get"
}
}
}
end

def relationships do
[company: JSONAPITest.CompanyView]
end
Expand Down

0 comments on commit def58a9

Please sign in to comment.