diff --git a/app/controllers/admin/poll/officer_assignments_controller.rb b/app/controllers/admin/poll/officer_assignments_controller.rb
index e52108645..b14f99a6a 100644
--- a/app/controllers/admin/poll/officer_assignments_controller.rb
+++ b/app/controllers/admin/poll/officer_assignments_controller.rb
@@ -1,11 +1,16 @@
class Admin::Poll::OfficerAssignmentsController < Admin::BaseController
- before_action :redirect_if_blank_required_params, only: [:index]
+ before_action :load_poll
+ before_action :redirect_if_blank_required_params, only: [:by_officer]
before_action :load_booth_assignment, only: [:create]
def index
- @poll = ::Poll.includes(:booths).find(officer_assignment_params[:poll])
- @officer = ::Poll::Officer.includes(:user).find(officer_assignment_params[:officer])
+ @officers = @poll.officer_assignments.includes(officer: :user).select(:officer_id).distinct.map(&:officer)
+ 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).
@@ -13,6 +18,15 @@ class Admin::Poll::OfficerAssignmentsController < Admin::BaseController
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],
@@ -24,7 +38,7 @@ class Admin::Poll::OfficerAssignmentsController < Admin::BaseController
else
notice = t("admin.poll_officer_assignments.flash.error_create")
end
- redirect_to admin_officer_assignments_path(officer: create_params[:officer_id], poll: create_params[:poll_id]), notice: notice
+ 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
@@ -35,13 +49,13 @@ class Admin::Poll::OfficerAssignmentsController < Admin::BaseController
else
notice = t("admin.poll_officer_assignments.flash.error_destroy")
end
- redirect_to admin_officer_assignments_path(officer: @officer_assignment.officer_id, poll: @officer_assignment.poll_id), notice: notice
+ 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, :poll)
+ params.permit(:officer_id)
end
def create_params
@@ -52,14 +66,22 @@ class Admin::Poll::OfficerAssignmentsController < Admin::BaseController
@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].blank?
- if officer_assignment_params[:poll].blank?
- redirect_to admin_polls_path
- else
- redirect_to admin_poll_path(officer_assignment_params[:poll])
- end
+ 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
\ No newline at end of file
diff --git a/app/controllers/admin/poll/polls_controller.rb b/app/controllers/admin/poll/polls_controller.rb
index 340cbec71..9f5319f9d 100644
--- a/app/controllers/admin/poll/polls_controller.rb
+++ b/app/controllers/admin/poll/polls_controller.rb
@@ -9,11 +9,9 @@ class Admin::Poll::PollsController < Admin::BaseController
def show
@poll = Poll.includes(:questions,
- booth_assignments: [:booth,
- :final_recounts,
- :recounts],
- officers: [:user]).
- order('poll_questions.title', 'poll_booths.name', 'users.username').
+ booth_assignments: [:final_recounts,
+ :recounts]).
+ order('poll_questions.title').
find(params[:id])
end
@@ -70,14 +68,6 @@ class Admin::Poll::PollsController < Admin::BaseController
end
end
- def search_officers
- @officers = User.joins(:poll_officer).search(@search).order(username: :asc)
-
- respond_to do |format|
- format.js
- end
- end
-
private
def load_geozones
@geozones = Geozone.all.order(:name)
diff --git a/app/views/admin/poll/polls/_search_officers.html.erb b/app/views/admin/poll/officer_assignments/_search_officers.html.erb
similarity index 81%
rename from app/views/admin/poll/polls/_search_officers.html.erb
rename to app/views/admin/poll/officer_assignments/_search_officers.html.erb
index b26c98d0c..7ed39f10d 100644
--- a/app/views/admin/poll/polls/_search_officers.html.erb
+++ b/app/views/admin/poll/officer_assignments/_search_officers.html.erb
@@ -1,6 +1,6 @@
- <%= form_tag(search_officers_admin_poll_path(@poll), method: :get, remote: true) do |f| %>
+ <%= form_tag(search_officers_admin_poll_officer_assignments_path(@poll), method: :get, remote: true) do |f| %>