Skip to content

Commit

Permalink
Merge pull request #208 from dwyl/create-tags
Browse files Browse the repository at this point in the history
PR: Allow creating new `tag`
  • Loading branch information
nelsonic authored Nov 22, 2022
2 parents 8809718 + 8d1fbe1 commit daaa369
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 3 deletions.
20 changes: 20 additions & 0 deletions lib/app_web/controllers/tag_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,26 @@ defmodule AppWeb.TagController do
render(conn, "index.html", tags: tags)
end

def new(conn, _params) do
changeset = Tag.changeset(%Tag{})
render(conn, "new.html", changeset: changeset)
end

def create(conn, %{"tag" => tag_params}) do
person_id = conn.assigns[:person][:id] || 0
tag_params = Map.put(tag_params, "person_id", person_id)

case Tag.create_tag(tag_params) do
{:ok, _tag} ->
conn
|> put_flash(:info, "Tag created successfully.")
|> redirect(to: Routes.tag_path(conn, :index))

{:error, changeset} ->
render(conn, "new.html", changeset: changeset)
end
end

def edit(conn, %{"id" => id}) do
tag = Tag.get_tag!(id)
changeset = Tag.changeset(tag)
Expand Down
4 changes: 3 additions & 1 deletion lib/app_web/templates/tag/edit.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

<%= render(
"form.html",
Map.put(assigns, :action, Routes.tag_path(@conn, :update, @tag))
assigns
|> Map.put(:action, Routes.tag_path(@conn, :update, @tag))
|> Map.put(:method, "patch")
) %>

<.a to={Routes.tag_path(@conn, :index)} class="" label="Back to tags" />
Expand Down
2 changes: 1 addition & 1 deletion lib/app_web/templates/tag/form.html.heex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<.form :let={f} for={@changeset} action={@action} method="patch" class="py-3">
<.form :let={f} for={@changeset} action={@action} method={@method} class="py-3">
<%= if @changeset.action do %>
<div class="alert alert-danger">
<p>Oops, something went wrong! Please check the errors below.</p>
Expand Down
9 changes: 8 additions & 1 deletion lib/app_web/templates/tag/index.html.heex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<.h2 class="text-center mt-3">Listing Tags</.h2>

<.container>
<.table>
<.table class="my-4">
<.tr>
<.th>Name</.th>

Expand Down Expand Up @@ -31,4 +32,10 @@
</.tr>
<% end %>
</.table>
<.button
link_type="a"
to={Routes.tag_path(@conn, :new)}
label="Create Tag"
class="my-2"
/>
</.container>
12 changes: 12 additions & 0 deletions lib/app_web/templates/tag/new.html.heex
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<.container>
<.h2 class="text-center mt-3">New Tag</.h2>

<%= render(
"form.html",
assigns
|> Map.put(:action, Routes.tag_path(@conn, :create))
|> Map.put(:method, "post")
) %>

<.a to={Routes.tag_path(@conn, :index)} class="" label="Back to tags" />
</.container>
33 changes: 33 additions & 0 deletions test/app_web/controllers/tag_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,39 @@ defmodule AppWeb.TagControllerTest do
end
end

describe "new tag" do
test "renders form for creating a tag", %{conn: conn} do
conn =
conn
|> assign(:person, %{id: 1})
|> get(Routes.tag_path(conn, :new))

assert html_response(conn, 200) =~ "New Tag"
end
end

describe "create tag" do
test "redirects to show when data is valid", %{conn: conn} do
conn =
conn
|> assign(:person, %{id: 1})
|> post(Routes.tag_path(conn, :create),
tag: %{text: "new tag", color: "#FCA5A5"}
)

assert redirected_to(conn) == Routes.tag_path(conn, :index)
end

test "renders errors when data is invalid", %{conn: conn} do
conn =
conn
|> assign(:person, %{id: 1})
|> post(Routes.tag_path(conn, :create), tag: @invalid_attrs)

assert html_response(conn, 200) =~ "New Tag"
end
end

describe "edit tag" do
setup [:create_tag]

Expand Down

0 comments on commit daaa369

Please sign in to comment.