Skip to content

Commit

Permalink
Strip associations from user struct
Browse files Browse the repository at this point in the history
When preparing the user for serialization the associations
are now stripped given that the associations preloading might
not be required.
  • Loading branch information
trenpixster committed Apr 21, 2016
1 parent 0059a41 commit 97d5e9d
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/addict/interactors/create_session.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ defmodule Addict.Interactors.CreateSession do
Returns `{:ok, conn}`
"""
def call(conn, user) do
def call(conn, user, schema \\ Addict.Configs.user_schema) do
conn = conn
|> fetch_session
|> put_session(:current_user, Addict.Presenter.strip_all(user))
|> put_session(:current_user, Addict.Presenter.strip_all(user, schema))
{:ok, conn}
end
end
13 changes: 10 additions & 3 deletions lib/addict/presenter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@ Normalized structure presentation
"""

@doc """
Strips `:__struct__`, `:__meta__` and `:encrypted_password` from the structure
Strips all associations, `:__struct__`, `:__meta__` and `:encrypted_password` from the structure
Returns the stripped structure
"""
def strip_all(model) do
model |> Map.drop([:__struct__, :__meta__, :encrypted_password])
def strip_all(model, schema \\ Addict.Configs.user_schema) do
model |> drop_keys(schema)
end

defp drop_keys(model, schema) do
associations = schema.__schema__(:associations)
Map.drop model, associations ++ [:__struct__, :__meta__, :encrypted_password]
end

end
2 changes: 1 addition & 1 deletion test/interactors/create_session_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ defmodule CreateSessionTest do

assert Plug.Conn.get_session(conn, :current_user) == nil

{:ok, conn} = CreateSession.call(conn, fake_user)
{:ok, conn} = CreateSession.call(conn, fake_user, TestAddictSchema)

assert Plug.Conn.get_session(conn, :current_user) == fake_user
end
Expand Down
2 changes: 1 addition & 1 deletion test/interactors/destroy_session_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ defmodule DestroySessionTest do
conn = conn(:get, "/")
|> Plug.Session.call(@session_opts)
|> fetch_session
{:ok, conn} = CreateSession.call(conn, fake_user)
{:ok, conn} = CreateSession.call(conn, fake_user, TestAddictSchema)

{:ok, conn} = DestroySession.call(conn)

Expand Down
38 changes: 38 additions & 0 deletions test/presenter_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
defmodule PresenterTest do
alias Addict.Presenter
use ExUnit.Case, async: true

test "it strips associations" do
user = struct(
TestAddictUserAssociationsSchema,
%{
name: "Joe Doe",
email: "joe.doe@example.com",
encrypted_password: "what a hash!"
})

model = Presenter.strip_all(user, TestAddictUserAssociationsSchema)

assert Map.has_key?(model, :__struct__) == false
assert Map.has_key?(model, :__meta__) == false
assert Map.has_key?(model, :drugs) == false
end
end

defmodule TestAddictDrugsSchema do
use Ecto.Schema
schema "drugs" do
field :name, :string
end
end

defmodule TestAddictUserAssociationsSchema do
use Ecto.Schema

schema "users" do
field :name, :string
field :email, :string
field :encrypted_password, :string
has_many :drugs, TestAddictDrugsSchema
end
end

0 comments on commit 97d5e9d

Please sign in to comment.