Skip to content

create an insert function that is not a macro to compare to macro alt… #26

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
13 changes: 11 additions & 2 deletions lib/append/address.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ defmodule Append.Address do
@doc false
def changeset(address, attrs) do
address
# I think that this should be removed
# see comments above this functions definition for more on this
|> insert_entry_id()
|> cast(attrs, [
:name,
Expand All @@ -37,11 +39,18 @@ defmodule Append.Address do
:address_line_2,
:city,
:postcode,
:tel,
:entry_id
:tel
# alog doesn't have entry_id as a required_field, it just ensures that an
# entry_id is added to the changeset.
# see https://github.com/dwyl/alog/blob/master/lib/alog.ex#L112
# :entry_id
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need make a couple of data transformation tables for this example to explain what the primary key of each record is so that:
a) each revision of the address record is unique
b) the address for a given person can be looked up by ID and we can see the history of the record.

])
end

# Ideally, we wouldn't require the user to create a function that they have to
# call in their changeset function.
# This is moved in alog, and can be updated for the non-macro approach so that
# users will not need to do this.
def insert_entry_id(address) do
case Map.fetch(address, :entry_id) do
{:ok, nil} -> %{address | entry_id: Ecto.UUID.generate()}
Expand Down
58 changes: 58 additions & 0 deletions lib/append/aol.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
defmodule Append.AOL do
import Ecto.Changeset

# macro way
# user needs to...
# 1 - call use MACRO_MODULE name in calling module
# 2 - Call CallingModuleName.insert(params)
# (not much effort on the users part)
# def insert(attrs) do
# %__MODULE__{}
# |> __MODULE__.changeset(attrs)
# |> Repo.insert()
# end

# non macro way
# user needs to...
# 1 - Call ThisModuleName.insert(Module, params)
# (again, not much effort on the users part)
# Also, if the user is following new practices and has a context which handles
# calling insert, they could (and I think should) have a function defined
# which in turn calls this function. e.g.
# defmodule MyApp.MyContext do
# def insert_user(attrs) do
# Append.AOL.insert(User, attrs)
# end
# end
# This would mean that they would just be calling...
# MyApp.MyContext.insert_user(attrs)
# so really no overhead at all
# shrug emoji
def insert(module, attrs) do
module
|> struct()
|> module.changeset(attrs)
# |> insert_entry_id()
|> Append.Repo.insert()
end

# This function would handle adding the entry_id to the changeset. It's not
# really needed for this example but it would be needed in a real app. See
# comments in Append.Address for more info on this
def insert_entry_id(%Ecto.Changeset{valid?: true} = changeset) do
case get_field(changeset, :entry_id) do
nil ->
put_change(changeset, :entry_id, Ecto.UUID.generate())

_ ->
# may need to put an error here saying something like...
# OI!!!!!! Don't try to generate your own entry_id...
# But haven't really thought that far ahead. Also it isn't done in this
# example anyway so didn't think it was needed.
changeset
end

end

def insert_entry_id(changeset), do: changeset
end
34 changes: 30 additions & 4 deletions test/append/address_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,29 @@ defmodule Append.AddressTest do

describe "get items from database" do
test "get/1" do
{:ok, item} = insert_address()
# {:ok, item} = insert_address()
{:ok, item} = insert_address2()

assert Address.get(item.entry_id) == item
end

test "all/0" do
{:ok, _} = insert_address()
{:ok, _} = insert_address("Loki")

insert_address()
{:ok, loki} = insert_address2("Loki")
assert length(Address.all()) == 2

{:ok, loki2} = insert_address("Loki")

# remove things that will always be different
[m1, m2] =
[loki, loki2]
|> Enum.map(&Map.drop(&1, ~w(entry_id id inserted_at updated_at)a))

# This test shows that insert_address and insert_address2 both return the
# same value
# check out where insert_address amd insert_address2 are defined below to
# see differences between the two functions. (There isn't many)
assert Map.equal?(m1, m2)
end
end

Expand Down Expand Up @@ -67,6 +80,7 @@ defmodule Append.AddressTest do
assert Map.fetch(h2, :city) == {:ok, "Oslo"}
end

# macro way
def insert_address(name \\ "Thor") do
Address.insert(%{
name: name,
Expand All @@ -78,6 +92,18 @@ defmodule Append.AddressTest do
})
end

# function way
def insert_address2(name \\ "Thor") do
Append.AOL.insert(Address, %{
name: name,
address_line_1: "The Hall",
address_line_2: "Valhalla",
city: "Asgard",
postcode: "AS1 3DG",
tel: "0800123123"
})
end

describe "delete:" do
test "deleted items are not retrieved with 'get'" do
{:ok, item} = insert_address()
Expand Down