Files
grecia/app/components/polls/questions/question_component.rb
Javi Martín a7e1b42b6c Use checkboxes and radio buttons on poll forms
Our original interface to vote in a poll had a few issues:

* Since there was no button to send the form, it wasn't clear that
  selecting an option would automatically store it in the database.
* The interface was almost identical for single-choice questions and
  multiple-choice questions, which made it hard to know which type of
  question we were answering.
* Adding other type of questions, like open answers, was hard since we
  would have to add a different submit button for each answer.

So we're now using radio buttons for single-choice questions and
checkboxes for multiple-choice questions, which are the native controls
designed for these purposes, and a button to send the whole form.

Since we don't have a database table for poll ballots like we have for
budget ballots, we're adding a new `Poll::WebVote` model to manage poll
ballots. We're using WebVote instead of Ballot or Vote because they
could be mistaken with other vote classes.

Note that browsers don't allow removing answers with radio buttons, so
once somebody has voted in a single-choice question, they can't remove
the vote unless they manually edit their HTML. This is the same behavior
we had before commit 7df0e9a96.

As mentioned in c2010f975, we're now adding the `ChangeByZero` rubocop
rule, since we've removed the test that used `and change`.
2025-08-14 13:06:37 +02:00

66 lines
1.7 KiB
Ruby

class Polls::Questions::QuestionComponent < ApplicationComponent
attr_reader :question, :disabled
alias_method :disabled?, :disabled
use_helpers :current_user
def initialize(question, disabled: false)
@question = question
@disabled = disabled
end
private
def fieldset_attributes
tag.attributes(
id: dom_id(question),
disabled: ("disabled" if disabled?),
data: { max_votes: question.max_votes }
)
end
def options_read_more_links
safe_join(question.options_with_read_more.map do |option|
link_to option.title, "#option_#{option.id}"
end, ", ")
end
def multiple_choice?
question.multiple?
end
def multiple_choice_help_text
tag.span(
t("poll_questions.description.multiple", maximum: question.max_votes),
class: "help-text"
)
end
def multiple_choice_field(option)
choice_field(option) do
check_box_tag "web_vote[#{question.id}][option_id][]",
option.id,
checked?(option),
id: "web_vote_option_#{option.id}"
end
end
def single_choice_field(option)
choice_field(option) do
radio_button_tag "web_vote[#{question.id}][option_id]",
option.id,
checked?(option),
id: "web_vote_option_#{option.id}"
end
end
def choice_field(option, &block)
label_tag("web_vote_option_#{option.id}") do
block.call + option.title
end
end
def checked?(option)
question.answers.where(author: current_user, option: option).any?
end
end