~afontaine/home

d8f5aa634f667434cc6f093465a46af6e845a419 — Andrew Fontaine 8 months ago 91a2c85
Add Author to Blog Post

Pulls it automatically out of the From header of the email.

This supports multi-author blogs!
M apps/blog/lib/blog/content/post.ex => apps/blog/lib/blog/content/post.ex +1 -0
@@ 8,6 8,7 @@ defmodule Blog.Content.Post do
    field :subject, :string
    field :text, :string
    field :slug, :string
    field :author, :string

    timestamps()
  end

M apps/blog/lib/blog/mail/post.ex => apps/blog/lib/blog/mail/post.ex +10 -0
@@ 7,6 7,7 @@ defmodule Blog.Mail.Post do
    field :subject, :string
    field :envelope, Blog.Types.Message
    field :text, :string
    field :author, :string
  end

  @doc false


@@ 15,6 16,7 @@ defmodule Blog.Mail.Post do
    |> cast(attrs, [:message_id, :subject, :envelope])
    |> validate_required([:message_id, :subject, :envelope])
    |> put_body()
    |> put_author()
  end

  defp put_body(%Ecto.Changeset{valid?: true, changes: %{envelope: envelope}} = changeset) do


@@ 33,4 35,12 @@ defmodule Blog.Mail.Post do
  end

  defp put_body(changeset), do: changeset

  defp put_author(%Ecto.Changeset{valid?: true, changes: %{envelope: envelope}} = changeset) do
    author = Mail.get_from(envelope)

    put_change(changeset, :author, author)
  end

  defp put_author(changeset), do: changeset
end

A apps/blog/priv/repo/migrations/20210327220223_add_author_to_posts.exs => apps/blog/priv/repo/migrations/20210327220223_add_author_to_posts.exs +9 -0
@@ 0,0 1,9 @@
defmodule Blog.Repo.Migrations.AddAuthorToPosts do
  use Ecto.Migration

  def change do
    alter table(:posts) do
      add :author, :string
    end
  end
end