From 80703c754678036f51f1e3d9a442e033c5a2b812 Mon Sep 17 00:00:00 2001 From: amoffat Date: Fri, 25 Feb 2022 14:20:14 -0500 Subject: [PATCH 1/2] link object & additional View actions --- lib/jsonapi/view.ex | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/lib/jsonapi/view.ex b/lib/jsonapi/view.ex index 24d38dd0..223d5096 100644 --- a/lib/jsonapi/view.ex +++ b/lib/jsonapi/view.ex @@ -118,7 +118,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() @@ -244,6 +245,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) @@ -262,6 +272,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. " <> From 947a19431f477d6c20b62fec3d61ffd95607fe23 Mon Sep 17 00:00:00 2001 From: amoffat Date: Fri, 25 Feb 2022 14:46:58 -0500 Subject: [PATCH 2/2] add link object to test --- test/jsonapi_test.exs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/jsonapi_test.exs b/test/jsonapi_test.exs index 83b9d104..5e7bac02 100644 --- a/test/jsonapi_test.exs +++ b/test/jsonapi_test.exs @@ -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