Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: link objects and controller actions #325

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
@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 @@ -167,7 +168,7 @@
{path, opts} = Keyword.pop(opts, :path)
{paginator, _opts} = Keyword.pop(opts, :paginator)

quote do

Check warning on line 171 in lib/jsonapi/view.ex

View workflow job for this annotation

GitHub Actions / Credo

Avoid long quote blocks.
alias JSONAPI.{Serializer, View}

@behaviour View
Expand Down Expand Up @@ -270,6 +271,15 @@
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 @@

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
Loading