Merge branch 'master' into site-customization

This commit is contained in:
Amaia Castro
2017-04-07 17:29:33 +02:00
279 changed files with 9351 additions and 464 deletions

View File

@@ -0,0 +1,66 @@
class Admin::Poll::BoothAssignmentsController < Admin::BaseController
before_action :load_poll, except: [:create, :destroy]
def index
@booth_assignments = @poll.booth_assignments.includes(:booth).order('poll_booths.name').page(params[:page]).per(50)
end
def search_booths
load_search
@booths = ::Poll::Booth.search(@search)
respond_to do |format|
format.js
end
end
def show
@booth_assignment = @poll.booth_assignments.includes(:recounts, :final_recounts, :voters, officer_assignments: [officer: [:user]]).find(params[:id])
@voters_by_date = @booth_assignment.voters.group_by {|v| v.created_at.to_date}
end
def create
@booth_assignment = ::Poll::BoothAssignment.new(poll_id: booth_assignment_params[:poll_id], booth_id: booth_assignment_params[:booth_id])
if @booth_assignment.save
notice = t("admin.poll_booth_assignments.flash.create")
else
notice = t("admin.poll_booth_assignments.flash.error_create")
end
redirect_to admin_poll_booth_assignments_path(@booth_assignment.poll_id), notice: notice
end
def destroy
@booth_assignment = ::Poll::BoothAssignment.find(params[:id])
if @booth_assignment.destroy
notice = t("admin.poll_booth_assignments.flash.destroy")
else
notice = t("admin.poll_booth_assignments.flash.error_destroy")
end
redirect_to admin_poll_booth_assignments_path(@booth_assignment.poll_id), notice: notice
end
private
def load_booth_assignment
@booth_assignment = ::Poll::BoothAssignment.find(params[:id])
end
def booth_assignment_params
params.permit(:booth_id, :poll_id)
end
def load_poll
@poll = ::Poll.find(params[:poll_id])
end
def search_params
params.permit(:poll_id, :search)
end
def load_search
@search = search_params[:search]
end
end

View File

@@ -0,0 +1,39 @@
class Admin::Poll::BoothsController < Admin::BaseController
load_and_authorize_resource class: 'Poll::Booth'
def index
@booths = @booths.order(name: :asc).page(params[:page])
end
def show
end
def new
end
def create
if @booth.save
redirect_to admin_booths_path, notice: t("flash.actions.create.poll_booth")
else
render :new
end
end
def edit
end
def update
if @booth.update(booth_params)
redirect_to admin_booth_path(@booth), notice: t("flash.actions.update.poll_booth")
else
render :edit
end
end
private
def booth_params
params.require(:poll_booth).permit(:name, :location)
end
end

View File

@@ -0,0 +1,92 @@
class Admin::Poll::OfficerAssignmentsController < Admin::BaseController
before_action :load_poll
before_action :redirect_if_blank_required_params, only: [:by_officer]
before_action :load_booth_assignment, only: [:create]
def index
@officers = ::Poll::Officer.
includes(:user).
order('users.username').
where(
id: @poll.officer_assignments.select(:officer_id).distinct.map(&:officer_id)
).page(params[:page]).per(50)
end
def by_officer
@poll = ::Poll.includes(:booths).find(params[:poll_id])
@officer = ::Poll::Officer.includes(:user).find(officer_assignment_params[:officer_id])
@officer_assignments = ::Poll::OfficerAssignment.
joins(:booth_assignment).
includes(:recount, :final_recounts, booth_assignment: :booth).
where("officer_id = ? AND poll_booth_assignments.poll_id = ?", @officer.id, @poll.id).
order(:date)
end
def search_officers
load_search
@officers = User.joins(:poll_officer).search(@search).order(username: :asc)
respond_to do |format|
format.js
end
end
def create
@officer_assignment = ::Poll::OfficerAssignment.new(booth_assignment: @booth_assignment,
officer_id: create_params[:officer_id],
date: create_params[:date])
@officer_assignment.final = true if @officer_assignment.date > @booth_assignment.poll.ends_at.to_date
if @officer_assignment.save
notice = t("admin.poll_officer_assignments.flash.create")
else
notice = t("admin.poll_officer_assignments.flash.error_create")
end
redirect_to by_officer_admin_poll_officer_assignments_path(poll_id: create_params[:poll_id], officer_id: create_params[:officer_id]), notice: notice
end
def destroy
@officer_assignment = ::Poll::OfficerAssignment.includes(:booth_assignment).find(params[:id])
if @officer_assignment.destroy
notice = t("admin.poll_officer_assignments.flash.destroy")
else
notice = t("admin.poll_officer_assignments.flash.error_destroy")
end
redirect_to by_officer_admin_poll_officer_assignments_path(poll_id: @officer_assignment.poll_id, officer_id: @officer_assignment.officer_id), notice: notice
end
private
def officer_assignment_params
params.permit(:officer_id)
end
def create_params
params.permit(:poll_id, :booth_id, :date, :officer_id)
end
def load_booth_assignment
@booth_assignment = ::Poll::BoothAssignment.includes(:poll).find_by(poll_id: create_params[:poll_id], booth_id: create_params[:booth_id])
end
def load_poll
@poll = ::Poll.find(params[:poll_id])
end
def redirect_if_blank_required_params
if officer_assignment_params[:officer_id].blank?
redirect_to admin_poll_path(@poll)
end
end
def search_params
params.permit(:poll_id, :search)
end
def load_search
@search = search_params[:search]
end
end

View File

@@ -0,0 +1,39 @@
class Admin::Poll::OfficersController < Admin::BaseController
load_and_authorize_resource :officer, class: "Poll::Officer", except: [:edit, :show]
def index
@officers = @officers.page(params[:page])
end
def search
@user = User.find_by(email: params[:email])
respond_to do |format|
if @user
@officer = Poll::Officer.find_or_initialize_by(user: @user)
format.js
else
format.js { render "user_not_found" }
end
end
end
def create
@officer.user_id = params[:user_id]
@officer.save
redirect_to admin_officers_path
end
def destroy
@officer.destroy
redirect_to admin_officers_path
end
def show
end
def edit
end
end

View File

@@ -0,0 +1,86 @@
class Admin::Poll::PollsController < Admin::BaseController
load_and_authorize_resource
before_action :load_search, only: [:search_booths, :search_questions, :search_officers]
before_action :load_geozones, only: [:new, :create, :edit, :update]
def index
end
def show
@poll = Poll.includes(:questions).
order('poll_questions.title').
find(params[:id])
end
def new
end
def create
if @poll.save
redirect_to [:admin, @poll], notice: t("flash.actions.create.poll")
else
render :new
end
end
def edit
end
def update
if @poll.update(poll_params)
redirect_to [:admin, @poll], notice: t("flash.actions.update.poll")
else
render :edit
end
end
def add_question
question = ::Poll::Question.find(params[:question_id])
if question.present?
@poll.questions << question
notice = t("admin.polls.flash.question_added")
else
notice = t("admin.polls.flash.error_on_question_added")
end
redirect_to admin_poll_path(@poll), notice: notice
end
def remove_question
question = ::Poll::Question.find(params[:question_id])
if @poll.questions.include? question
@poll.questions.delete(question)
notice = t("admin.polls.flash.question_removed")
else
notice = t("admin.polls.flash.error_on_question_removed")
end
redirect_to admin_poll_path(@poll), notice: notice
end
def search_questions
@questions = ::Poll::Question.where("poll_id IS ? OR poll_id != ?", nil, @poll.id).search({search: @search}).order(title: :asc)
respond_to do |format|
format.js
end
end
private
def load_geozones
@geozones = Geozone.all.order(:name)
end
def poll_params
params.require(:poll).permit(:name, :starts_at, :ends_at, :geozone_restricted, geozone_ids: [])
end
def search_params
params.permit(:poll_id, :search)
end
def load_search
@search = search_params[:search]
end
end

View File

@@ -0,0 +1,64 @@
class Admin::Poll::QuestionsController < Admin::BaseController
load_and_authorize_resource :poll
load_and_authorize_resource :question, class: 'Poll::Question'
def index
@polls = Poll.all
@search = search_params[:search]
@questions = @questions.search(search_params).page(params[:page]).order("created_at DESC")
@proposals = Proposal.successful.sort_by_confidence_score
end
def new
@polls = Poll.all
@question.valid_answers = I18n.t('poll_questions.default_valid_answers')
proposal = Proposal.find(params[:proposal_id]) if params[:proposal_id].present?
@question.copy_attributes_from_proposal(proposal)
end
def create
@question.author = @question.proposal.try(:author) || current_user
if @question.save
redirect_to admin_question_path(@question)
else
render :new
end
end
def show
end
def edit
end
def update
if @question.update(question_params)
redirect_to admin_question_path(@question), notice: t("flash.actions.save_changes.notice")
else
render :edit
end
end
def destroy
if @question.destroy
notice = "Question destroyed succesfully"
else
notice = t("flash.actions.destroy.error")
end
redirect_to admin_questions_path, notice: notice
end
private
def question_params
params.require(:poll_question).permit(:poll_id, :title, :question, :description, :proposal_id, :valid_answers)
end
def search_params
params.permit(:poll_id, :search)
end
end

View File

@@ -0,0 +1,16 @@
class Admin::Poll::RecountsController < Admin::BaseController
before_action :load_poll
def index
@booth_assignments = @poll.booth_assignments.
includes(:booth, :recounts, :final_recounts, :voters).
order("poll_booths.name").
page(params[:page]).per(50)
end
private
def load_poll
@poll = ::Poll.find(params[:poll_id])
end
end

View File

@@ -0,0 +1,13 @@
class Admin::Poll::ResultsController < Admin::BaseController
before_action :load_poll
def index
@partial_results = @poll.partial_results
end
private
def load_poll
@poll = ::Poll.includes(:questions).find(params[:poll_id])
end
end

View File

@@ -22,7 +22,7 @@ class DebatesController < ApplicationController
def index_customization
@featured_debates = @debates.featured
@proposal_successfull_exists = Proposal.successfull.exists?
@proposal_successfull_exists = Proposal.successful.exists?
end
def show

View File

@@ -0,0 +1,12 @@
class Officing::BaseController < ApplicationController
layout 'admin'
before_action :authenticate_user!
before_action :verify_officer
skip_authorization_check
def verify_officer
raise CanCan::AccessDenied unless current_user.try(:poll_officer?) || current_user.try(:administrator?)
end
end

View File

@@ -0,0 +1,6 @@
class Officing::DashboardController < Officing::BaseController
def index
end
end

View File

@@ -0,0 +1,47 @@
class Officing::FinalRecountsController < Officing::BaseController
before_action :load_poll
before_action :load_officer_assignment, only: :create
def new
@officer_assignments = ::Poll::OfficerAssignment.
includes(:final_recounts, 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)
@final_recounts = @officer_assignments.select {|oa| oa.final_recounts.any?}.map(&:final_recounts).flatten
end
def create
@final_recount = ::Poll::FinalRecount.find_or_initialize_by(booth_assignment_id: @officer_assignment.booth_assignment_id, date: final_recount_params[:date])
@final_recount.officer_assignment_id = @officer_assignment.id
@final_recount.count = final_recount_params[:count]
if @final_recount.save
msg = { notice: t("officing.final_recounts.flash.create") }
else
msg = { alert: t("officing.final_recounts.flash.error_create") }
end
redirect_to new_officing_poll_final_recount_path(@poll), msg
end
private
def load_poll
@poll = Poll.expired.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_final_recount_path(@poll), alert: t("officing.final_recounts.flash.error_create")
end
end
def final_recount_params
params.permit(:officer_assignment_id, :count, :date)
end
end

View File

@@ -0,0 +1,15 @@
class Officing::PollsController < Officing::BaseController
def index
@polls = current_user.poll_officer? ? current_user.poll_officer.voting_days_assigned_polls : []
@polls = @polls.select {|poll| poll.current?(Time.current) || poll.current?(1.day.ago)}
end
def final
@polls = current_user.poll_officer? ? current_user.poll_officer.final_days_assigned_polls : []
return unless current_user.poll_officer?
@polls = @polls.select {|poll| poll.ends_at > 1.week.ago && poll.expired?}
end
end

View File

@@ -0,0 +1,46 @@
class Officing::RecountsController < Officing::BaseController
before_action :load_poll
before_action :load_officer_assignment, only: :create
def new
@officer_assignments = ::Poll::OfficerAssignment.
includes(:recount, booth_assignment: :booth).
joins(:booth_assignment).
voting_days.
where(id: current_user.poll_officer.officer_assignment_ids).
where("poll_booth_assignments.poll_id = ?", @poll.id).
order(date: :asc)
@recounted = @officer_assignments.select {|oa| oa.recount.present?}.reverse
end
def create
@recount = ::Poll::Recount.find_or_initialize_by(booth_assignment_id: @officer_assignment.booth_assignment_id, date: @officer_assignment.date)
@recount.officer_assignment_id = @officer_assignment.id
@recount.count = recount_params[:count]
if @recount.save
msg = { notice: t("officing.recounts.flash.create") }
else
msg = { alert: t("officing.recounts.flash.error_create") }
end
redirect_to new_officing_poll_recount_path(@poll), msg
end
private
def load_poll
@poll = Poll.find(params[:poll_id])
end
def load_officer_assignment
@officer_assignment = current_user.poll_officer.
officer_assignments.find_by(id: recount_params[:officer_assignment_id])
if @officer_assignment.blank?
redirect_to new_officing_poll_recount_path(@poll), alert: t("officing.recounts.flash.error_create")
end
end
def recount_params
params.permit(:officer_assignment_id, :count)
end
end

View File

@@ -0,0 +1,37 @@
class Officing::ResidenceController < Officing::BaseController
before_action :load_officer_assignment
before_action :validate_officer_assignment, only: :create
def new
@residence = Officing::Residence.new
end
def create
@residence = Officing::Residence.new(residence_params.merge(officer: current_user.poll_officer))
if @residence.save
redirect_to new_officing_voter_path(id: @residence.user.id), notice: t("officing.residence.flash.create")
else
render :new
end
end
private
def residence_params
params.require(:residence).permit(:document_number, :document_type, :year_of_birth)
end
def load_officer_assignment
@officer_assignments = current_user.poll_officer.
officer_assignments.
voting_days.
where(date: Time.current.to_date)
end
def validate_officer_assignment
if @officer_assignments.blank?
redirect_to officing_root_path, notice: t("officing.residence.flash.not_allowed")
end
end
end

View File

@@ -0,0 +1,141 @@
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
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
def index
@booth_assignment = ::Poll::BoothAssignment.includes(:booth).find(index_params[:booth_assignment_id])
if current_user.poll_officer.officer_assignments.final.
where(booth_assignment_id: @booth_assignment.id).exists?
@partial_results = ::Poll::PartialResult.includes(:question).
where(booth_assignment_id: index_params[:booth_assignment_id]).
where(date: index_params[:date])
@whites = ::Poll::WhiteResult.where(booth_assignment_id: @booth_assignment.id, date: index_params[:date]).sum(:amount)
@nulls = ::Poll::NullResult.where(booth_assignment_id: @booth_assignment.id, date: index_params[:date]).sum(:amount)
end
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
build_white_results
build_null_results
end
def build_white_results
if results_params[:whites].present?
white_result = ::Poll::WhiteResult.find_or_initialize_by(booth_assignment_id: @officer_assignment.booth_assignment_id,
date: results_params[:date])
white_result.officer_assignment_id = @officer_assignment.id
white_result.amount = results_params[:whites].to_i
white_result.author = current_user
white_result.origin = 'booth'
@results << white_result
end
end
def build_null_results
if results_params[:nulls].present?
null_result = ::Poll::NullResult.find_or_initialize_by(booth_assignment_id: @officer_assignment.booth_assignment_id,
date: results_params[:date])
null_result.officer_assignment_id = @officer_assignment.id
null_result.amount = results_params[:nulls].to_i
null_result.author = current_user
null_result.origin = 'booth'
@results << null_result
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
load_partial_results
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 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 results_params
params.permit(:officer_assignment_id, :date, :questions, :whites, :nulls)
end
def index_params
params.permit(:booth_assignment_id, :date)
end
end

View File

@@ -0,0 +1,25 @@
class Officing::VotersController < Officing::BaseController
respond_to :html, :js
def new
@user = User.find(params[:id])
@polls = Poll.answerable_by(@user)
end
def create
@poll = Poll.find(voter_params[:poll_id])
@user = User.find(voter_params[:user_id])
@voter = Poll::Voter.new(document_type: @user.document_type,
document_number: @user.document_number,
user: @user,
poll: @poll)
@voter.save!
end
private
def voter_params
params.require(:voter).permit(:poll_id, :user_id)
end
end

View File

@@ -0,0 +1,27 @@
class Polls::QuestionsController < ApplicationController
load_and_authorize_resource :poll
load_and_authorize_resource :question, class: 'Poll::Question'
has_orders %w{most_voted newest oldest}, only: :show
def show
@commentable = @question.proposal.present? ? @question.proposal : @question
@comment_tree = CommentTree.new(@commentable, params[:page], @current_order)
set_comment_flags(@comment_tree.comments)
question_answer = @question.answers.where(author_id: current_user.try(:id)).first
@answers_by_question_id = {@question.id => question_answer.try(:answer)}
end
def answer
answer = @question.answers.find_or_initialize_by(author: current_user)
answer.answer = params[:answer]
answer.save!
answer.record_voter_participation
@answers_by_question_id = {@question.id => params[:answer]}
end
end

View File

@@ -0,0 +1,23 @@
class PollsController < ApplicationController
load_and_authorize_resource
has_filters %w{current expired incoming}
::Poll::Answer # trigger autoload
def index
@polls = @polls.send(@current_filter).includes(:geozones).sort_for_list.page(params[:page])
end
def show
@questions = @poll.questions.for_render.sort_for_list
@answers_by_question_id = {}
poll_answers = ::Poll::Answer.by_question(@poll.question_ids).by_author(current_user.try(:id))
poll_answers.each do |answer|
@answers_by_question_id[answer.question_id] = answer.answer
end
end
end

View File

@@ -1,8 +0,0 @@
class ProposalBallotsController < ApplicationController
skip_authorization_check
def index
@proposal_ballots = Proposal.successfull.sort_by_confidence_score
end
end

View File

@@ -28,8 +28,8 @@ class ProposalsController < ApplicationController
def index_customization
discard_archived
load_retired
load_proposal_ballots
load_featured unless @proposal_successfull_exists
load_successful_proposals
load_featured unless @proposal_successful_exists
end
def vote
@@ -103,8 +103,8 @@ class ProposalsController < ApplicationController
end
end
def load_proposal_ballots
@proposal_successfull_exists = Proposal.successfull.exists?
def load_successful_proposals
@proposal_successful_exists = Proposal.successful.exists?
end
end