diff --git a/app/controllers/officing/results_controller.rb b/app/controllers/officing/results_controller.rb index be6f312bf..cffd3e0d0 100644 --- a/app/controllers/officing/results_controller.rb +++ b/app/controllers/officing/results_controller.rb @@ -1,41 +1,96 @@ class Officing::ResultsController < Officing::BaseController before_action :load_poll + + before_action :load_officer_assignments, only: :new + before_action :load_partial_results, only: :new + before_action :load_officer_assignment, only: :create + before_action :check_booth_and_date, only: :create + before_action :build_results, only: :create def new - @officer_assignments = ::Poll::OfficerAssignment. + end + + def create + @results.each { |result| result.save! } + + notice = t("officing.results.flash.create") + redirect_to new_officing_poll_result_path(@poll), notice: notice + end + + private + + def check_booth_and_date + if @officer_assignment.blank? + go_back_to_new(t("officing.results.flash.error_wrong_booth")) + elsif results_params[:date].blank? || + Date.parse(results_params[:date]) < @poll.starts_at.to_date || + Date.parse(results_params[:date]) > @poll.ends_at.to_date + go_back_to_new(t("officing.results.flash.error_wrong_date")) + end + end + + def build_results + @results = [] + + params[:questions].each_pair do |question_id, results| + question = @poll.questions.find(question_id) + go_back_to_new if question.blank? + + results.each_pair do |answer_index, count| + if count.present? + answer = question.valid_answers[answer_index.to_i] + go_back_to_new if question.blank? + + partial_result = ::Poll::PartialResult.find_or_initialize_by(booth_assignment_id: @officer_assignment.booth_assignment_id, + date: results_params[:date], + question_id: question_id, + answer: answer) + partial_result.officer_assignment_id = @officer_assignment.id + partial_result.amount = count.to_i + partial_result.author = current_user + partial_result.origin = 'booth' + @results << partial_result + end + end + end + end + + def go_back_to_new(alert = nil) + params[:d] = results_params[:date] + params[:oa] = results_params[:officer_assignment_id] + flash.now[:alert] = (alert || t("officing.results.flash.error_create")) + load_officer_assignments + render :new + end + + def load_poll + @poll = ::Poll.expired.includes(:questions).find(params[:poll_id]) + end + + def load_officer_assignment + @officer_assignment = current_user.poll_officer. + officer_assignments.final.find_by(id: results_params[:officer_assignment_id]) + end + + def load_officer_assignments + @officer_assignments = ::Poll::OfficerAssignment. includes(booth_assignment: [:booth]). joins(:booth_assignment). final. where(id: current_user.poll_officer.officer_assignment_ids). where("poll_booth_assignments.poll_id = ?", @poll.id). order(date: :asc) - end - - def create - if false - notice = t("officing.results.flash.create") - else - notice = t("officing.results.flash.error_create") - end - redirect_to new_officing_poll_result_path(@poll), notice: notice - end - - private - def load_poll - @poll = Poll.expired.includes(:questions).find(params[:poll_id]) end - def load_officer_assignment - @officer_assignment = current_user.poll_officer. - officer_assignments.final.find_by(id: final_recount_params[:officer_assignment_id]) - if @officer_assignment.blank? - redirect_to new_officing_poll_result_path(@poll), notice: t("officing.results.flash.error_create") + def load_partial_results + if @officer_assignments.present? + @partial_results = ::Poll::PartialResult.where(officer_assignment_id: @officer_assignments.map(&:id)).order(:booth_assignment_id, :date) end end - def final_recount_params - params.permit(:officer_assignment_id, :count, :date) + def results_params + params.permit(:officer_assignment_id, :date, :questions) end -end \ No newline at end of file +end diff --git a/app/helpers/officing_helper.rb b/app/helpers/officing_helper.rb index 43916fc24..eb7513e87 100644 --- a/app/helpers/officing_helper.rb +++ b/app/helpers/officing_helper.rb @@ -25,4 +25,11 @@ module OfficingHelper final_recount.booth_assignment.voters.select {|v| v.created_at.to_date == final_recount.date}.size end + def answer_result_value(question_id, answer_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][answer_index.to_s] + end + end \ No newline at end of file diff --git a/app/models/poll/booth_assignment.rb b/app/models/poll/booth_assignment.rb index 057a58d27..bb507d0b8 100644 --- a/app/models/poll/booth_assignment.rb +++ b/app/models/poll/booth_assignment.rb @@ -8,5 +8,6 @@ class Poll has_many :final_recounts, class_name: "Poll::FinalRecount", dependent: :destroy has_many :officers, through: :officer_assignments has_many :voters + has_many :partial_results end end diff --git a/app/models/poll/officer_assignment.rb b/app/models/poll/officer_assignment.rb index 736deb54b..ed1902ab8 100644 --- a/app/models/poll/officer_assignment.rb +++ b/app/models/poll/officer_assignment.rb @@ -4,6 +4,7 @@ class Poll belongs_to :booth_assignment has_one :recount has_many :final_recounts + has_many :partial_results has_many :voters validates :officer_id, presence: true diff --git a/app/models/poll/partial_result.rb b/app/models/poll/partial_result.rb index 3dc432f1c..037395277 100644 --- a/app/models/poll/partial_result.rb +++ b/app/models/poll/partial_result.rb @@ -1,9 +1,11 @@ class Poll::PartialResult < ActiveRecord::Base - VALID_ORIGINS = %w{ web } + VALID_ORIGINS = %w{ web booth } belongs_to :question, -> { with_hidden } belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id' + belongs_to :booth_assignment + belongs_to :officer_assignment validates :question, presence: true validates :author, presence: true diff --git a/app/views/officing/results/new.html.erb b/app/views/officing/results/new.html.erb index 7d55537f6..146a99699 100644 --- a/app/views/officing/results/new.html.erb +++ b/app/views/officing/results/new.html.erb @@ -27,10 +27,10 @@
| <%= t("officing.results.new.date") %> | +<%= t("officing.results.new.booth") %> | ++ + + <% results_by_booth = @partial_results.group_by(&:booth_assignment_id) %> + <% results_by_booth.keys.each do |booth_assignment| %> + <% results_by_booth[booth_assignment].group_by(&:date).keys.each do |date| %> + |
|---|---|---|
| + <%= l(date.to_date, format: :long) %> + | ++ <%= results_by_booth[booth_assignment].first.booth_assignment.booth.name %> + | ++ + | +