set name_at_post_time more explicitly

This commit is contained in:
byte[] 2019-11-18 22:58:21 -05:00
parent 623b2b486f
commit 92d53f6450
3 changed files with 9 additions and 11 deletions

View file

@ -2,6 +2,8 @@
Next generation imageboard. Next generation imageboard.
![](https://derpicdn.net/img/2019/8/23/2125268/full.png)
To start your Phoenix server: To start your Phoenix server:
* Install dependencies with `mix deps.get` * Install dependencies with `mix deps.get`

View file

@ -37,7 +37,7 @@ defmodule Philomena.Comments.Comment do
|> validate_required([:body]) |> validate_required([:body])
|> validate_length(:body, min: 1, max: 300_000, count: :bytes) |> validate_length(:body, min: 1, max: 300_000, count: :bytes)
|> change(attribution) |> change(attribution)
|> put_name_at_post_time() |> put_name_at_post_time(attribution[:user])
end end
def changeset(comment, attrs) do def changeset(comment, attrs) do
@ -48,8 +48,6 @@ defmodule Philomena.Comments.Comment do
|> validate_length(:edit_reason, max: 70, count: :bytes) |> validate_length(:edit_reason, max: 70, count: :bytes)
end end
defp put_name_at_post_time(%{changes: %{user: %{data: %{name: name}}}} = changeset), defp put_name_at_post_time(changeset, nil), do: changeset
do: change(changeset, name_at_post_time: name) defp put_name_at_post_time(changeset, user), do: change(changeset, name_at_post_time: user.name)
defp put_name_at_post_time(changeset),
do: changeset
end end

View file

@ -46,7 +46,7 @@ defmodule Philomena.Posts.Post do
|> validate_required([:body]) |> validate_required([:body])
|> validate_length(:body, min: 1, max: 300_000, count: :bytes) |> validate_length(:body, min: 1, max: 300_000, count: :bytes)
|> change(attribution) |> change(attribution)
|> put_name_at_post_time() |> put_name_at_post_time(attribution[:user])
end end
@doc false @doc false
@ -58,11 +58,9 @@ defmodule Philomena.Posts.Post do
|> validate_length(:body, min: 1, max: 300_000, count: :bytes) |> validate_length(:body, min: 1, max: 300_000, count: :bytes)
|> change(attribution) |> change(attribution)
|> change(topic_position: 0) |> change(topic_position: 0)
|> put_name_at_post_time() |> put_name_at_post_time(attribution[:user])
end end
defp put_name_at_post_time(%{changes: %{user: %{data: %{name: name}}}} = changeset), defp put_name_at_post_time(changeset, nil), do: changeset
do: change(changeset, name_at_post_time: name) defp put_name_at_post_time(changeset, user), do: change(changeset, name_at_post_time: user.name)
defp put_name_at_post_time(changeset),
do: changeset
end end