From 16329908388d6b265d82b575a1a1c3edcaa7f12e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Wed, 2 Apr 2025 18:21:21 +0200 Subject: [PATCH] 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. --- .../officing/results/form_component.html.erb | 49 +++++++++++++++++ .../officing/results/form_component.rb | 19 +++++++ app/helpers/officing_helper.rb | 8 --- app/views/officing/results/new.html.erb | 52 +------------------ 4 files changed, 69 insertions(+), 59 deletions(-) create mode 100644 app/components/officing/results/form_component.html.erb create mode 100644 app/components/officing/results/form_component.rb diff --git a/app/components/officing/results/form_component.html.erb b/app/components/officing/results/form_component.html.erb new file mode 100644 index 000000000..cbe4b2b92 --- /dev/null +++ b/app/components/officing/results/form_component.html.erb @@ -0,0 +1,49 @@ +<%= form_tag(officing_poll_results_path(poll), { id: "officer_assignment_form" }) do %> +
+
+ <%= 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") } %> +
+
+ + <% poll.questions.each do |question| %> +
+
+

<%= question.title %>

+
+ <% question.question_options.each_with_index do |option, i| %> +
+ + <%= text_field_tag "questions[#{question.id}][#{i}]", answer_result_value(question.id, i), placeholder: "0" %> +
+ <% end %> +
+
+ <% end %> + +
+
+

<%= t("officing.results.new.ballots_white") %>

+ <%= text_field_tag :whites, params[:whites].presence, placeholder: "0" %> +
+ +
+

<%= t("officing.results.new.ballots_null") %>

+ <%= text_field_tag :nulls, params[:nulls].presence, placeholder: "0" %> +
+ +
+

<%= t("officing.results.new.ballots_total") %>

+ <%= text_field_tag :total, params[:total].presence, placeholder: "0" %> +
+
+
+ +
+
+ <%= submit_tag t("officing.results.new.submit"), class: "button expanded" %> +
+
+<% end %> diff --git a/app/components/officing/results/form_component.rb b/app/components/officing/results/form_component.rb new file mode 100644 index 000000000..dc5ecaffc --- /dev/null +++ b/app/components/officing/results/form_component.rb @@ -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 diff --git a/app/helpers/officing_helper.rb b/app/helpers/officing_helper.rb index 3078f3c84..15c2e7788 100644 --- a/app/helpers/officing_helper.rb +++ b/app/helpers/officing_helper.rb @@ -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 diff --git a/app/views/officing/results/new.html.erb b/app/views/officing/results/new.html.erb index c53ff92d5..c1b6c43b3 100644 --- a/app/views/officing/results/new.html.erb +++ b/app/views/officing/results/new.html.erb @@ -1,56 +1,6 @@ <% if @officer_assignments.any? %>

<%= t("officing.results.new.title", poll: @poll.name) %>

- - <%= form_tag(officing_poll_results_path(@poll), { id: "officer_assignment_form" }) do %> -
-
- <%= 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") } %> -
-
- - <% @poll.questions.each do |question| %> -
-
-

<%= question.title %>

-
- <% question.question_options.each_with_index do |option, i| %> -
- - <%= text_field_tag "questions[#{question.id}][#{i}]", answer_result_value(question.id, i), placeholder: "0" %> -
- <% end %> -
-
- <% end %> - -
-
-

<%= t("officing.results.new.ballots_white") %>

- <%= text_field_tag :whites, params[:whites].presence, placeholder: "0" %> -
- -
-

<%= t("officing.results.new.ballots_null") %>

- <%= text_field_tag :nulls, params[:nulls].presence, placeholder: "0" %> -
- -
-

<%= t("officing.results.new.ballots_total") %>

- <%= text_field_tag :total, params[:total].presence, placeholder: "0" %> -
-
-
- -
-
- <%= submit_tag t("officing.results.new.submit"), class: "button expanded" %> -
-
- <% end %> - + <%= render Officing::Results::FormComponent.new(@poll, @officer_assignments) %> <% else %>

<%= @poll.name %>