Skip to content

Commit

Permalink
avoid modifying item.cid or list.cid when updating #418
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Sep 10, 2023
1 parent da1cc04 commit 8acf8c0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
13 changes: 10 additions & 3 deletions lib/app/item.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,16 @@ defmodule App.Item do
item
|> cast(attrs, [:person_id, :status, :text])
|> validate_required([:person_id])
|> App.Cid.put_cid()
end

# Update an item without changing the cid ref: #418
def changeset_update(item, attrs) do
item
|> cast(attrs, [:cid, :person_id, :status, :text])
|> validate_required([:cid, :text, :person_id])
end


@doc """
`create_item/1` creates an `item`.
Expand Down Expand Up @@ -164,7 +171,7 @@ defmodule App.Item do
"""
def update_item(%Item{} = item, attrs) do
item
|> Item.changeset(attrs)
|> Item.changeset_update(attrs)
|> PaperTrail.update(originator: %{id: Map.get(attrs, :person_id, 0)})
end

Expand All @@ -186,7 +193,7 @@ defmodule App.Item do

def delete_item(id) do
get_item!(id)
|> Item.changeset(%{status: 6})
|> Item.changeset_update(%{status: 6})
|> Repo.update()
end

Expand Down
11 changes: 9 additions & 2 deletions lib/app/list.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ defmodule App.List do
|> App.Cid.put_cid()
end

# Update an list without changing the cid ref: #418
def changeset_update(list, attrs \\ %{}) do
list
|> cast(attrs, [:name, :person_id, :seq, :sort, :status])
|> validate_required([:cid, :name, :person_id])
end

@doc """
`create_list/1` creates an `list`.
Expand Down Expand Up @@ -68,7 +75,7 @@ defmodule App.List do
"""
def delete_list(id) do
get_list!(id)
|> changeset(%{status: 6})
|> changeset_update(%{status: 6})
|> Repo.update()
end

Expand Down Expand Up @@ -106,7 +113,7 @@ defmodule App.List do
"""
def update_list(%List{} = list, attrs) do
list
|> List.changeset(attrs)
|> List.changeset_update(attrs)
|> PaperTrail.update()
end

Expand Down

0 comments on commit 8acf8c0

Please sign in to comment.