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.
This commit is contained in:
Javi Martín
2025-04-02 19:20:21 +02:00
parent ff3ada780d
commit 9308189a00
3 changed files with 24 additions and 9 deletions

View File

@@ -14,7 +14,7 @@
<% question.question_options.each_with_index do |option, i| %> <% question.question_options.each_with_index do |option, i| %>
<div class="small-12 medium-6 large-3 column end"> <div class="small-12 medium-6 large-3 column end">
<%= label_tag "questions_#{question.id}_#{i}", option.title %> <%= 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 %>
</div> </div>
<% end %> <% end %>
</fieldset> </fieldset>
@@ -24,17 +24,17 @@
<div class="row"> <div class="row">
<div class="small-12 medium-6 large-3 column"> <div class="small-12 medium-6 large-3 column">
<%= label_tag :whites, t("officing.results.new.ballots_white") %> <%= 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 %>
</div> </div>
<div class="small-12 medium-6 large-3 column end"> <div class="small-12 medium-6 large-3 column end">
<%= label_tag :nulls, t("officing.results.new.ballots_null") %> <%= 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 %>
</div> </div>
<div class="small-12 medium-6 large-3 column end"> <div class="small-12 medium-6 large-3 column end">
<%= label_tag :total, t("officing.results.new.ballots_total") %> <%= 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 %>
</div> </div>
</div> </div>
<hr> <hr>

View File

@@ -10,10 +10,6 @@ class Officing::Results::FormComponent < ApplicationComponent
private private
def answer_result_value(question_id, option_index) def answer_result_value(question_id, option_index)
return nil if params.blank? params.dig(:questions, question_id.to_s, option_index.to_s).to_i
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]
end end
end end

View File

@@ -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