Skip to content

Commit

Permalink
split nodes changeset in different use cases
Browse files Browse the repository at this point in the history
  • Loading branch information
electronicbites committed Feb 17, 2024
1 parent 2294c2a commit ec82624
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 44 deletions.
32 changes: 13 additions & 19 deletions lib/radiator/outline.ex
Original file line number Diff line number Diff line change
Expand Up @@ -92,26 +92,33 @@ defmodule Radiator.Outline do
"""
def create_node(attrs \\ %{}) do
%Node{}
|> Node.changeset(attrs)
|> Node.insert_changeset(attrs)
|> Repo.insert()
|> broadcast_node_action(:insert)
end

def create_node(attrs, %{id: id}) do
%Node{creator_id: id}
|> Node.insert_changeset(attrs)
|> Repo.insert()
|> broadcast_node_action(:insert)
end

@doc """
Updates a node.
Updates a nodes content.
## Examples
iex> update_node(node, %{field: new_value})
iex> update_node_content(node, %{content: new_value})
{:ok, %Node{}}
iex> update_node(node, %{field: bad_value})
iex> update_node_content(node, %{content: nil})
{:error, %Ecto.Changeset{}}
"""
def update_node(%Node{} = node, attrs) do
def update_node_content(%Node{} = node, attrs) do
node
|> Node.changeset(attrs)
|> Node.update_content_changeset(attrs)
|> Repo.update()
|> broadcast_node_action(:update)
end
Expand All @@ -134,19 +141,6 @@ defmodule Radiator.Outline do
|> broadcast_node_action(:delete)
end

@doc """
Returns an `%Ecto.Changeset{}` for tracking node changes.
## Examples
iex> change_node(node)
%Ecto.Changeset{data: %Node{}}
"""
def change_node(%Node{} = node, attrs \\ %{}) do
Node.changeset(node, attrs)
end

defp broadcast_node_action({:ok, node}, action) do
PubSub.broadcast(Radiator.PubSub, @topic, {action, node})
{:ok, node}
Expand Down
44 changes: 29 additions & 15 deletions lib/radiator/outline/node.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule Radiator.Outline.Node do
@derive {Jason.Encoder, only: [:uuid, :content, :creator_id, :parent_id, :prev_id]}

@primary_key {:uuid, :binary_id, autogenerate: true}

schema "outline_nodes" do
field :content, :string
field :creator_id, :integer
Expand All @@ -21,25 +22,38 @@ defmodule Radiator.Outline.Node do
timestamps(type: :utc_datetime)
end

@required_fields [
:episode_id
]

@optional_fields [
:content,
:creator_id,
:parent_id,
:prev_id
]
@doc """
A changeset for inserting a new node
Work in progress. Since we currently ignore the tree structure, there is
no concept for a root node.
Also questionable wether a node really needs a content from beginning. So probably a root
doesnt have a content
Another issue might be we need to create the uuid upfront and pass it here
"""
def insert_changeset(node, attributes) do
node
|> cast(attributes, [:content, :episode_id, :creator_id, :parent_id, :prev_id])
|> update_change(:content, &trim/1)
|> validate_required([:content, :episode_id])
end

@all_fields @optional_fields ++ @required_fields
@doc """
Changeset for moving a node
Only the parent_id is allowed and expected to be changed
"""
def move_changeset(node, attrs) do
node
|> cast(attrs, [:parent_id])
end

@doc false
def changeset(node, attrs) do
@doc """
Changeset for updating the content of a node
"""
def update_content_changeset(node, attrs) do
node
|> cast(attrs, @all_fields)
|> cast(attrs, [:content])
|> update_change(:content, &trim/1)
|> validate_required(@required_fields)
|> validate_required([:content])
end

defp trim(content) when is_binary(content), do: String.trim(content)
Expand Down
2 changes: 1 addition & 1 deletion lib/radiator_web/live/episode_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ defmodule RadiatorWeb.EpisodeLive.Index do

case Outline.get_node(uuid) do
nil -> nil
node -> Outline.update_node(node, attrs)
node -> Outline.update_node_content(node, attrs)
end

socket
Expand Down
13 changes: 4 additions & 9 deletions test/radiator/outline_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,17 @@ defmodule Radiator.OutlineTest do
assert {:error, %Ecto.Changeset{}} = Outline.create_node(@invalid_attrs)
end

test "update_node/2 with valid data updates the node" do
test "update_node_content/2 with valid data updates the node" do
node = node_fixture()
update_attrs = %{content: "some updated content"}

assert {:ok, %Node{} = node} = Outline.update_node(node, update_attrs)
assert {:ok, %Node{} = node} = Outline.update_node_content(node, update_attrs)
assert node.content == "some updated content"
end

test "update_node/2 with invalid data returns error changeset" do
test "update_node_content/2 with invalid data returns error changeset" do
node = node_fixture()
assert {:error, %Ecto.Changeset{}} = Outline.update_node(node, @invalid_attrs)
assert {:error, %Ecto.Changeset{}} = Outline.update_node_content(node, @invalid_attrs)
assert node == Outline.get_node!(node.uuid)
end

Expand All @@ -70,10 +70,5 @@ defmodule Radiator.OutlineTest do
assert {:ok, %Node{}} = Outline.delete_node(node)
assert_raise Ecto.NoResultsError, fn -> Outline.get_node!(node.uuid) end
end

test "change_node/1 returns a node changeset" do
node = node_fixture()
assert %Ecto.Changeset{} = Outline.change_node(node)
end
end
end

0 comments on commit ec82624

Please sign in to comment.