Skip to content

Commit

Permalink
Remove separate PhoenixView
Browse files Browse the repository at this point in the history
  • Loading branch information
scrogson committed May 5, 2016
1 parent 991a3c0 commit c089625
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 28 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ proper functions to your view like so.

```elixir
defmodule MyApp.PostView do
use JSONAPI.PhoenixView

def type, do: "post"
use JSONAPI.View, type: "posts"

def fields do
[:text, :body]
Expand Down
23 changes: 0 additions & 23 deletions lib/jsonapi/phoenix.ex

This file was deleted.

21 changes: 20 additions & 1 deletion lib/jsonapi/view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,26 @@ defmodule JSONAPI.View do
"#{url_for(data, conn)}/relationships/#{rel_type}"
end

defoverridable [attributes: 2, relationships: 0, id: 1, type: 0, fields: 0, url_for: 2, url_for_rel: 3]

if Code.ensure_loaded?(Phoenix) do
def render("show.json", %{data: data, conn: conn}),
do: show(data, conn, conn.params)
def render("show.json", %{data: data, conn: conn, params: params}),
do: show(data, conn, params)

def render("index.json", %{data: data, conn: conn}),
do: index(data, conn, conn.params)
def render("index.json", %{data: data, conn: conn, params: params}),
do: show(data, conn, params)
end

defoverridable attributes: 2,
fields: 0,
id: 1,
relationships: 0,
type: 0,
url_for: 2,
url_for_rel: 3
end
end
end
25 changes: 24 additions & 1 deletion test/jsonapi/view_test.exs
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
defmodule JSONAPI.ViewTest do
use ExUnit.Case, async: true
use ExUnit.Case

setup tags do
if tags[:compile_phoenix] do
Module.create(Phoenix, [], __ENV__)

defmodule CommentView do
use JSONAPI.View, type: "comments"
end
end

:ok
end

defmodule PostView do
use JSONAPI.View, type: "posts", namespace: "/api"
end

alias JSONAPI.ViewTest.CommentView

test "type/0 when specified via using macro" do
assert PostView.type == "posts"
end
Expand All @@ -18,4 +32,13 @@ defmodule JSONAPI.ViewTest do
assert PostView.url_for_rel([], "comments", %Plug.Conn{}) == "http://www.example.com/api/posts/relationships/comments"
assert PostView.url_for_rel(%{id: 1}, "comments", %Plug.Conn{}) == "http://www.example.com/api/posts/1/relationships/comments"
end

@tag :compile_phoenix
test "render/2 is defined when 'Phoenix' is loaded" do
assert {:render, 2} in CommentView.__info__(:functions)
end

test "render/2 is not defined when 'Phoenix' is not loaded" do
refute {:render, 2} in PostView.__info__(:functions)
end
end

0 comments on commit c089625

Please sign in to comment.