ensure name_at_post_time is set correctly

This commit is contained in:
byte[] 2019-11-17 20:53:08 -05:00
parent 0a62a3d5ce
commit 57a37971b1
3 changed files with 11 additions and 4 deletions

View file

@ -39,10 +39,10 @@ defmodule Philomena.Comments do
{:error, %Ecto.Changeset{}} {:error, %Ecto.Changeset{}}
""" """
def create_comment(image, attributes, params \\ %{}) do def create_comment(image, user, attributes, params \\ %{}) do
comment = comment =
struct(Comment, [image_id: image.id] ++ attributes) struct(Comment, [image_id: image.id] ++ attributes)
|> Comment.creation_changeset(params) |> Comment.creation_changeset(user, params)
image_query = image_query =
Image Image

View file

@ -31,9 +31,10 @@ defmodule Philomena.Comments.Comment do
end end
@doc false @doc false
def creation_changeset(comment, attrs) do def creation_changeset(comment, user, attrs) do
comment comment
|> cast(attrs, [:body, :anonymous]) |> cast(attrs, [:body, :anonymous])
|> set_name_at_post_time(user)
|> 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)
end end
@ -45,4 +46,9 @@ defmodule Philomena.Comments.Comment do
|> validate_length(:body, min: 1, max: 300_000, count: :bytes) |> validate_length(:body, min: 1, max: 300_000, count: :bytes)
|> validate_length(:edit_reason, max: 70, count: :bytes) |> validate_length(:edit_reason, max: 70, count: :bytes)
end end
def set_name_at_post_time(changeset, nil), do: changeset
def set_name_at_post_time(changeset, %{name: name}) do
change(changeset, name_at_post_time: name)
end
end end

View file

@ -63,8 +63,9 @@ defmodule PhilomenaWeb.Image.CommentController do
def create(conn, %{"comment" => comment_params}) do def create(conn, %{"comment" => comment_params}) do
attributes = conn.assigns.attributes attributes = conn.assigns.attributes
image = conn.assigns.image image = conn.assigns.image
user = conn.assigns.current_user
case Comments.create_comment(image, attributes, comment_params) do case Comments.create_comment(image, user, attributes, comment_params) do
{:ok, %{comment: comment}} -> {:ok, %{comment: comment}} ->
Comments.notify_comment(comment) Comments.notify_comment(comment)
Comments.reindex_comment(comment) Comments.reindex_comment(comment)