Extract component to render officing results form

In a couple of commits we're going to add some styles for this form, and
it's easier to know where to add these styles when there's a component.
This commit is contained in:
Javi Martín
2025-04-02 18:21:21 +02:00
parent 962aa388df
commit 1632990838
4 changed files with 69 additions and 59 deletions

View File

@@ -0,0 +1,49 @@
<%= form_tag(officing_poll_results_path(poll), { id: "officer_assignment_form" }) do %>
<div class="row">
<div class="small-12 medium-6 column">
<%= label_tag :officer_assignment_id, t("officing.results.new.booth") %>
<%= select_tag :officer_assignment_id,
booths_for_officer_select_options(officer_assignments),
{ prompt: t("officing.results.new.select_booth") } %>
</div>
</div>
<% poll.questions.each do |question| %>
<div class="row">
<div class="small-12 column">
<h3><%= question.title %></h3>
</div>
<% question.question_options.each_with_index do |option, i| %>
<div class="small-12 medium-6 large-3 column end">
<label><%= option.title %></label>
<%= text_field_tag "questions[#{question.id}][#{i}]", answer_result_value(question.id, i), placeholder: "0" %>
</div>
<% end %>
</div>
<hr>
<% end %>
<div class="row">
<div class="small-12 medium-6 large-3 column">
<h3><%= t("officing.results.new.ballots_white") %></h3>
<%= text_field_tag :whites, params[:whites].presence, placeholder: "0" %>
</div>
<div class="small-12 medium-6 large-3 column end">
<h3><%= t("officing.results.new.ballots_null") %></h3>
<%= text_field_tag :nulls, params[:nulls].presence, placeholder: "0" %>
</div>
<div class="small-12 medium-6 large-3 column end">
<h3><%= t("officing.results.new.ballots_total") %></h3>
<%= text_field_tag :total, params[:total].presence, placeholder: "0" %>
</div>
</div>
<hr>
<div class="row">
<div class="small-12 medium-6 large-3 column">
<%= submit_tag t("officing.results.new.submit"), class: "button expanded" %>
</div>
</div>
<% end %>

View File

@@ -0,0 +1,19 @@
class Officing::Results::FormComponent < ApplicationComponent
attr_reader :poll, :officer_assignments
use_helpers :booths_for_officer_select_options
def initialize(poll, officer_assignments)
@poll = poll
@officer_assignments = officer_assignments
end
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]
end
end

View File

@@ -6,12 +6,4 @@ module OfficingHelper
options.sort_by! { |x| x[0] }
options_for_select(options, params[:oa])
end
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]
end
end

View File

@@ -1,56 +1,6 @@
<% if @officer_assignments.any? %>
<h2><%= t("officing.results.new.title", poll: @poll.name) %></h2>
<%= form_tag(officing_poll_results_path(@poll), { id: "officer_assignment_form" }) do %>
<div class="row">
<div class="small-12 medium-6 column">
<%= label_tag :officer_assignment_id, t("officing.results.new.booth") %>
<%= select_tag :officer_assignment_id,
booths_for_officer_select_options(@officer_assignments),
{ prompt: t("officing.results.new.select_booth") } %>
</div>
</div>
<% @poll.questions.each do |question| %>
<div class="row">
<div class="small-12 column">
<h3><%= question.title %></h3>
</div>
<% question.question_options.each_with_index do |option, i| %>
<div class="small-12 medium-6 large-3 column end">
<label><%= option.title %></label>
<%= text_field_tag "questions[#{question.id}][#{i}]", answer_result_value(question.id, i), placeholder: "0" %>
</div>
<% end %>
</div>
<hr>
<% end %>
<div class="row">
<div class="small-12 medium-6 large-3 column">
<h3><%= t("officing.results.new.ballots_white") %></h3>
<%= text_field_tag :whites, params[:whites].presence, placeholder: "0" %>
</div>
<div class="small-12 medium-6 large-3 column end">
<h3><%= t("officing.results.new.ballots_null") %></h3>
<%= text_field_tag :nulls, params[:nulls].presence, placeholder: "0" %>
</div>
<div class="small-12 medium-6 large-3 column end">
<h3><%= t("officing.results.new.ballots_total") %></h3>
<%= text_field_tag :total, params[:total].presence, placeholder: "0" %>
</div>
</div>
<hr>
<div class="row">
<div class="small-12 medium-6 large-3 column">
<%= submit_tag t("officing.results.new.submit"), class: "button expanded" %>
</div>
</div>
<% end %>
<%= render Officing::Results::FormComponent.new(@poll, @officer_assignments) %>
<% else %>
<h2><%= @poll.name %></h2>
<div class="callout alert">