Skip to content

Commit

Permalink
Improve seed data, adding draft and closed ballots.
Browse files Browse the repository at this point in the history
  • Loading branch information
zorn committed Jan 2, 2025
1 parent 7f493db commit 8586e45
Showing 1 changed file with 67 additions and 25 deletions.
92 changes: 67 additions & 25 deletions priv/repo/seeds.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,79 @@
# We recommend using the bang functions (`insert!`, `update!`
# and so on) as they will fail if something goes wrong.

{:ok, ballot} =
defmodule SeedScripts do
alias Flick.RankedVoting.Ballot

@doc """
Given a published `Flick.RankedVoting.Ballot` will populate said ballot with 25 votes.
It is expected that the ballot has three possible answers.
"""
def populate_ballot_with_votes(%Ballot{published_at: published_at} = ballot)
when not is_nil(published_at) do
# This assumes a ballot with three possible answers.

for _ <- 1..25 do
available_answers = Ballot.possible_answers_as_list(ballot.possible_answers)

if length(available_answers) != 3 do
raise """
The ballot with the question title '#{ballot.question_title}' must have at least three possible answers. We saw '#{available_answers}'.
"""
end

first_answer = Enum.random(available_answers)
available_answers = Enum.reject(available_answers, &(&1 == first_answer))
second_answer = Enum.random(available_answers)
available_answers = Enum.reject(available_answers, &(&1 == second_answer))
third_answer = Enum.random(available_answers)

# For a little variance, we'll sometime use an empty quote value for the third
# answer, since a user need not always provide a full ranked list answer.
third_answer = Enum.random([third_answer, ""])

full_name = if Enum.random(1..5) > 1, do: Faker.Person.name()

{:ok, _vote} =
Flick.RankedVoting.create_vote(ballot, %{
"full_name" => full_name,
"ranked_answers" => [
%{"value" => first_answer},
%{"value" => second_answer},
%{"value" => third_answer}
]
})
end
end
end

# Create a published ballot with some votes.
{:ok, sandwich_ballot} =
Flick.RankedVoting.create_ballot(%{
question_title: "What is your sandwich preference?",
possible_answers: "Turkey, Ham, Roast Beef",
url_slug: "sandwich-preference"
})

{:ok, published_ballot} = Flick.RankedVoting.publish_ballot(ballot)

for _ <- 1..25 do
available_answers = ["Turkey", "Ham", "Roast Beef"]
{:ok, sandwich_ballot_published} = Flick.RankedVoting.publish_ballot(sandwich_ballot)
SeedScripts.populate_ballot_with_votes(sandwich_ballot_published)

first_answer = Enum.random(available_answers)
available_answers = Enum.reject(available_answers, &(&1 == first_answer))
second_answer = Enum.random(available_answers)
available_answers = Enum.reject(available_answers, &(&1 == second_answer))
third_answer = Enum.random(available_answers)

# For a little variance, we'll sometime use an empty quote value for the third
# answer, since a user need not always provide a full ranked list answer.
third_answer = Enum.random([third_answer, ""])
# Create a draft ballot.
{:ok, color_ballot} =
Flick.RankedVoting.create_ballot(%{
question_title: "What is your favorite color?",
possible_answers: "Red, Green, Blue",
url_slug: "favorite-color"
})

full_name = if Enum.random(1..5) > 1, do: Faker.Person.name()
# Create a closed ballot.
{:ok, fruit_ballot} =
Flick.RankedVoting.create_ballot(%{
question_title: "What is your favorite fruit?",
possible_answers: "Apple, Banana, Orange",
url_slug: "favorite-fruit"
})

{:ok, _vote} =
Flick.RankedVoting.create_vote(published_ballot, %{
"full_name" => full_name,
"ranked_answers" => [
%{"value" => first_answer},
%{"value" => second_answer},
%{"value" => third_answer}
]
})
end
{:ok, fruit_ballot_published} = Flick.RankedVoting.publish_ballot(fruit_ballot)
SeedScripts.populate_ballot_with_votes(fruit_ballot_published)
{:ok, fruit_ballot_closed} = Flick.RankedVoting.close_ballot(fruit_ballot_published)

0 comments on commit 8586e45

Please sign in to comment.