From 9308189a009af34687ec0c1088f1868d47034afe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Wed, 2 Apr 2025 19:20:21 +0200 Subject: [PATCH] Use number fields to enter number of votes This way the fields are easier to use, and we can get rid of the placeholders. Note we're simplifying the `answer_result_value` in order to make it easier to return `0` instead of `nil` when the field is empty. Also note there's a small change of behavior here; previously, these fields were empty by default, and now their value is `0` by default, so blindly clicking the "Save" button would send `0` instead of an empty value. I don't think it's a big deal, though, but we need to keep that in mind. --- .../officing/results/form_component.html.erb | 8 ++++---- .../officing/results/form_component.rb | 6 +----- .../officing/results/form_component_spec.rb | 19 +++++++++++++++++++ 3 files changed, 24 insertions(+), 9 deletions(-) create mode 100644 spec/components/officing/results/form_component_spec.rb diff --git a/app/components/officing/results/form_component.html.erb b/app/components/officing/results/form_component.html.erb index 7d32d0439..d1c7870ec 100644 --- a/app/components/officing/results/form_component.html.erb +++ b/app/components/officing/results/form_component.html.erb @@ -14,7 +14,7 @@ <% question.question_options.each_with_index do |option, i| %>
<%= label_tag "questions_#{question.id}_#{i}", option.title %> - <%= text_field_tag "questions[#{question.id}][#{i}]", answer_result_value(question.id, i), placeholder: "0" %> + <%= number_field_tag "questions[#{question.id}][#{i}]", answer_result_value(question.id, i), min: 0 %>
<% end %> @@ -24,17 +24,17 @@
<%= label_tag :whites, t("officing.results.new.ballots_white") %> - <%= text_field_tag :whites, params[:whites].presence, placeholder: "0" %> + <%= number_field_tag :whites, params[:whites].to_i, min: 0 %>
<%= label_tag :nulls, t("officing.results.new.ballots_null") %> - <%= text_field_tag :nulls, params[:nulls].presence, placeholder: "0" %> + <%= number_field_tag :nulls, params[:nulls].to_i, min: 0 %>
<%= label_tag :total, t("officing.results.new.ballots_total") %> - <%= text_field_tag :total, params[:total].presence, placeholder: "0" %> + <%= number_field_tag :total, params[:total].to_i, min: 0 %>

diff --git a/app/components/officing/results/form_component.rb b/app/components/officing/results/form_component.rb index dc5ecaffc..a21979a1c 100644 --- a/app/components/officing/results/form_component.rb +++ b/app/components/officing/results/form_component.rb @@ -10,10 +10,6 @@ class Officing::Results::FormComponent < ApplicationComponent private def answer_result_value(question_id, option_index) - return nil if params.blank? - return nil if params[:questions].blank? - return nil if params[:questions][question_id.to_s].blank? - - params[:questions][question_id.to_s][option_index.to_s] + params.dig(:questions, question_id.to_s, option_index.to_s).to_i end end diff --git a/spec/components/officing/results/form_component_spec.rb b/spec/components/officing/results/form_component_spec.rb new file mode 100644 index 000000000..81f19e38e --- /dev/null +++ b/spec/components/officing/results/form_component_spec.rb @@ -0,0 +1,19 @@ +require "rails_helper" + +describe Officing::Results::FormComponent do + let(:poll) { create(:poll, ends_at: 1.day.ago) } + before { create(:poll_question, :yes_no, poll: poll, title: "Agreed?") } + + it "uses number fields with 0 as a default value" do + render_inline Officing::Results::FormComponent.new(poll, Poll::OfficerAssignment.none) + + page.find(:fieldset, "Agreed?") do |fieldset| + expect(fieldset).to have_field "Yes", with: 0, type: :number + expect(fieldset).to have_field "No", with: 0, type: :number + end + + expect(page).to have_field "Totally blank ballots", with: 0, type: :number + expect(page).to have_field "Invalid ballots", with: 0, type: :number + expect(page).to have_field "Valid ballots", with: 0, type: :number + end +end