adds results creation by poll officers
This commit is contained in:
@@ -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
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user