philomena/lib/philomena_web/views/topic/poll_view.ex

40 lines
1.1 KiB
Elixir
Raw Normal View History

2019-11-18 19:09:59 +01:00
defmodule PhilomenaWeb.Topic.PollView do
use PhilomenaWeb, :view
def ranked_options(poll) do
poll.options
|> Enum.sort_by(&{-&1.vote_count, &1.id})
end
def winning_option(poll) do
poll
|> ranked_options()
2023-04-10 01:30:18 +02:00
|> Enum.at(0)
2019-11-18 19:09:59 +01:00
end
def active?(poll) do
DateTime.diff(poll.active_until, DateTime.utc_now()) > 0
2019-11-18 19:09:59 +01:00
end
2019-12-20 04:41:19 +01:00
def require_answer?(%{vote_method: vote_method}), do: vote_method == "single"
def input_type(%{vote_method: "single"}), do: "radio"
def input_type(_poll), do: "checkbox"
def input_name(_poll), do: "poll[option_ids][]"
2019-11-19 04:38:22 +01:00
def percent_of_total(_option, %{total_votes: 0}), do: "0%"
2020-01-11 05:20:19 +01:00
2019-11-18 19:09:59 +01:00
def percent_of_total(%{vote_count: vote_count}, %{total_votes: total_votes}) do
2020-01-11 05:20:19 +01:00
:io_lib.format("~.2f%", [vote_count / total_votes * 100])
2019-11-18 19:09:59 +01:00
end
def option_class(%{id: option_id}, %{id: option_id}, true), do: "poll-option-top"
def option_class(_option, _top_option, _winners?), do: nil
2020-01-11 05:20:19 +01:00
def poll_bar_class(%{id: option_id}, %{id: option_id}, true),
do: "poll-bar__fill poll-bar__fill--top"
2019-11-18 19:09:59 +01:00
def poll_bar_class(_option, _top_option, _winners?), do: "poll-bar__fill"
end