Clean up poll validation

This commit is contained in:
byte[] 2023-04-09 19:30:18 -04:00
parent 3ff083f157
commit 3366182557
3 changed files with 10 additions and 2 deletions

View file

@ -39,7 +39,7 @@ defmodule Philomena.Polls.Poll do
|> validate_required([:title, :active_until, :vote_method])
|> validate_length(:title, max: 140, count: :bytes)
|> validate_inclusion(:vote_method, ["single", "multiple"])
|> cast_assoc(:options, with: &PollOption.creation_changeset/2)
|> cast_assoc(:options, required: true, with: &PollOption.creation_changeset/2)
|> validate_length(:options, min: 2, max: 20)
|> ignore_if_blank()
end

View file

@ -51,6 +51,14 @@ defmodule PhilomenaWeb.Topic.Poll.VoteController do
end
end
def create(conn, _params) do
topic = conn.assigns.topic
conn
|> put_flash(:error, "Your vote was not recorded.")
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic))
end
def delete(conn, %{"id" => poll_vote_id}) do
topic = conn.assigns.topic
poll_vote = PollVotes.get_poll_vote!(poll_vote_id)

View file

@ -9,7 +9,7 @@ defmodule PhilomenaWeb.Topic.PollView do
def winning_option(poll) do
poll
|> ranked_options()
|> hd()
|> Enum.at(0)
end
def active?(poll) do