~afontaine/home

936773f69efe5cafe8c237c9534ed69d677b26ec — Andrew Fontaine 8 months ago e54bf56
Remove Templates and Routes for New/Edit/Delete

As we will be creating our posts via email, we have no need for the new
or create actions, nor their templates.

I'm still working out how we'll actually edit posts, but we can figure
that out later.

As for deletes... Why bother?
8 files changed, 7 insertions(+), 153 deletions(-)

M apps/home_web/lib/home_web/controllers/post_controller.ex
M apps/home_web/lib/home_web/router.ex
D apps/home_web/lib/home_web/templates/post/edit.html.eex
D apps/home_web/lib/home_web/templates/post/form.html.eex
M apps/home_web/lib/home_web/templates/post/index.html.eex
D apps/home_web/lib/home_web/templates/post/new.html.eex
M apps/home_web/lib/home_web/templates/post/show.html.eex
M apps/home_web/test/home_web/controllers/post_controller_test.exs
M apps/home_web/lib/home_web/controllers/post_controller.ex => apps/home_web/lib/home_web/controllers/post_controller.ex +0 -47
@@ 2,61 2,14 @@ defmodule HomeWeb.PostController do
  use HomeWeb, :controller

  alias Blog.Blog
  alias Blog.Blog.Post

  def index(conn, _params) do
    posts = Blog.list_posts()
    render(conn, "index.html", posts: posts)
  end

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

  def create(conn, %{"post" => post_params}) do
    case Blog.create_post(post_params) do
      {:ok, post} ->
        conn
        |> put_flash(:info, "Post created successfully.")
        |> redirect(to: Routes.post_path(conn, :show, post))

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

  def show(conn, %{"id" => id}) do
    post = Blog.get_post!(id)
    render(conn, "show.html", post: post)
  end

  def edit(conn, %{"id" => id}) do
    post = Blog.get_post!(id)
    changeset = Blog.change_post(post)
    render(conn, "edit.html", post: post, changeset: changeset)
  end

  def update(conn, %{"id" => id, "post" => post_params}) do
    post = Blog.get_post!(id)

    case Blog.update_post(post, post_params) do
      {:ok, post} ->
        conn
        |> put_flash(:info, "Post updated successfully.")
        |> redirect(to: Routes.post_path(conn, :show, post))

      {:error, %Ecto.Changeset{} = changeset} ->
        render(conn, "edit.html", post: post, changeset: changeset)
    end
  end

  def delete(conn, %{"id" => id}) do
    post = Blog.get_post!(id)
    {:ok, _post} = Blog.delete_post(post)

    conn
    |> put_flash(:info, "Post deleted successfully.")
    |> redirect(to: Routes.post_path(conn, :index))
  end
end

M apps/home_web/lib/home_web/router.ex => apps/home_web/lib/home_web/router.ex +2 -0
@@ 17,6 17,8 @@ defmodule HomeWeb.Router do
    pipe_through :browser

    get "/", PageController, :index

    resources "/blog", PostController, only: [:index, :show]
  end

  # Other scopes may use custom stacks.

D apps/home_web/lib/home_web/templates/post/edit.html.eex => apps/home_web/lib/home_web/templates/post/edit.html.eex +0 -5
@@ 1,5 0,0 @@
<h1>Edit Post</h1>

<%= render "form.html", Map.put(assigns, :action, Routes.post_path(@conn, :update, @post)) %>

<span><%= link "Back", to: Routes.post_path(@conn, :index) %></span>

D apps/home_web/lib/home_web/templates/post/form.html.eex => apps/home_web/lib/home_web/templates/post/form.html.eex +0 -23
@@ 1,23 0,0 @@
<%= form_for @changeset, @action, fn f -> %>
  <%= if @changeset.action do %>
    <div class="alert alert-danger">
      <p>Oops, something went wrong! Please check the errors below.</p>
    </div>
  <% end %>

  <%= label f, :subject %>
  <%= text_input f, :subject %>
  <%= error_tag f, :subject %>

  <%= label f, :text %>
  <%= text_input f, :text %>
  <%= error_tag f, :text %>

  <%= label f, :message_id %>
  <%= text_input f, :message_id %>
  <%= error_tag f, :message_id %>

  <div>
    <%= submit "Save" %>
  </div>
<% end %>

M apps/home_web/lib/home_web/templates/post/index.html.eex => apps/home_web/lib/home_web/templates/post/index.html.eex +0 -4
@@ 19,12 19,8 @@

      <td>
        <span><%= link "Show", to: Routes.post_path(@conn, :show, post) %></span>
        <span><%= link "Edit", to: Routes.post_path(@conn, :edit, post) %></span>
        <span><%= link "Delete", to: Routes.post_path(@conn, :delete, post), method: :delete, data: [confirm: "Are you sure?"] %></span>
      </td>
    </tr>
<% end %>
  </tbody>
</table>

<span><%= link "New Post", to: Routes.post_path(@conn, :new) %></span>

D apps/home_web/lib/home_web/templates/post/new.html.eex => apps/home_web/lib/home_web/templates/post/new.html.eex +0 -5
@@ 1,5 0,0 @@
<h1>New Post</h1>

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

<span><%= link "Back", to: Routes.post_path(@conn, :index) %></span>

M apps/home_web/lib/home_web/templates/post/show.html.eex => apps/home_web/lib/home_web/templates/post/show.html.eex +0 -1
@@ 19,5 19,4 @@

</ul>

<span><%= link "Edit", to: Routes.post_path(@conn, :edit, @post) %></span>
<span><%= link "Back", to: Routes.post_path(@conn, :index) %></span>

M apps/home_web/test/home_web/controllers/post_controller_test.exs => apps/home_web/test/home_web/controllers/post_controller_test.exs +5 -68
@@ 4,7 4,11 @@ defmodule HomeWeb.PostControllerTest do
  alias Blog.Blog

  @create_attrs %{message_id: "some message_id", subject: "some subject", text: "some text"}
  @update_attrs %{message_id: "some updated message_id", subject: "some updated subject", text: "some updated text"}
  @update_attrs %{
    message_id: "some updated message_id",
    subject: "some updated subject",
    text: "some updated text"
  }
  @invalid_attrs %{message_id: nil, subject: nil, text: nil}

  def fixture(:post) do


@@ 18,71 22,4 @@ defmodule HomeWeb.PostControllerTest do
      assert html_response(conn, 200) =~ "Listing Posts"
    end
  end

  describe "new post" do
    test "renders form", %{conn: conn} do
      conn = get(conn, Routes.post_path(conn, :new))
      assert html_response(conn, 200) =~ "New Post"
    end
  end

  describe "create post" do
    test "redirects to show when data is valid", %{conn: conn} do
      conn = post(conn, Routes.post_path(conn, :create), post: @create_attrs)

      assert %{id: id} = redirected_params(conn)
      assert redirected_to(conn) == Routes.post_path(conn, :show, id)

      conn = get(conn, Routes.post_path(conn, :show, id))
      assert html_response(conn, 200) =~ "Show Post"
    end

    test "renders errors when data is invalid", %{conn: conn} do
      conn = post(conn, Routes.post_path(conn, :create), post: @invalid_attrs)
      assert html_response(conn, 200) =~ "New Post"
    end
  end

  describe "edit post" do
    setup [:create_post]

    test "renders form for editing chosen post", %{conn: conn, post: post} do
      conn = get(conn, Routes.post_path(conn, :edit, post))
      assert html_response(conn, 200) =~ "Edit Post"
    end
  end

  describe "update post" do
    setup [:create_post]

    test "redirects when data is valid", %{conn: conn, post: post} do
      conn = put(conn, Routes.post_path(conn, :update, post), post: @update_attrs)
      assert redirected_to(conn) == Routes.post_path(conn, :show, post)

      conn = get(conn, Routes.post_path(conn, :show, post))
      assert html_response(conn, 200) =~ "some updated message_id"
    end

    test "renders errors when data is invalid", %{conn: conn, post: post} do
      conn = put(conn, Routes.post_path(conn, :update, post), post: @invalid_attrs)
      assert html_response(conn, 200) =~ "Edit Post"
    end
  end

  describe "delete post" do
    setup [:create_post]

    test "deletes chosen post", %{conn: conn, post: post} do
      conn = delete(conn, Routes.post_path(conn, :delete, post))
      assert redirected_to(conn) == Routes.post_path(conn, :index)
      assert_error_sent 404, fn ->
        get(conn, Routes.post_path(conn, :show, post))
      end
    end
  end

  defp create_post(_) do
    post = fixture(:post)
    %{post: post}
  end
end