Files
nairobi/app/controllers/admin/poll/shifts_controller.rb
Javi Martín 7ca55c44e0 Apply Rails/SaveBang rubocop rule
Having exceptions is better than having silent bugs.

There are a few methods I've kept the same way they were.

The `RelatedContentScore#score_with_opposite` method is a bit peculiar:
it creates scores for both itself and the opposite related content,
which means the opposite related content will try to create the same
scores as well.

We've already got a test to check `Budget::Ballot#add_investment` when
creating a line fails ("Edge case voting a non-elegible investment").

Finally, the method `User#send_oauth_confirmation_instructions` doesn't
update the record when the email address isn't already present, leading
to the test "Try to register with the email of an already existing user,
when an unconfirmed email was provided by oauth" fo fail if we raise an
exception for an invalid user. That's because updating a user's email
doesn't update the database automatically, but instead a confirmation
email is sent.

There are also a few false positives for classes which don't have bang
methods (like the GraphQL classes) or destroying attachments.

For these reasons, I'm adding the rule with a "Refactor" severity,
meaning it's a rule we can break if necessary.
2019-10-23 14:39:31 +02:00

66 lines
1.7 KiB
Ruby

class Admin::Poll::ShiftsController < Admin::Poll::BaseController
before_action :load_booth
before_action :load_officer
def new
load_shifts
@shift = ::Poll::Shift.new
@voting_polls = @booth.polls.current
@recount_polls = @booth.polls.current_or_recounting
end
def create
@shift = ::Poll::Shift.new(shift_params)
@officer = @shift.officer
if @shift.save
notice = t("admin.poll_shifts.flash.create")
redirect_to new_admin_booth_shift_path(@shift.booth), notice: notice
else
load_shifts
flash[:error] = t("admin.poll_shifts.flash.date_missing")
render :new
end
end
def destroy
@shift = Poll::Shift.find(params[:id])
if @shift.unable_to_destroy?
alert = t("admin.poll_shifts.flash.unable_to_destroy")
redirect_to new_admin_booth_shift_path(@booth), alert: alert
else
@shift.destroy!
notice = t("admin.poll_shifts.flash.destroy")
redirect_to new_admin_booth_shift_path(@booth), notice: notice
end
end
def search_officers
@officers = User.search(params[:search]).order(username: :asc).select { |o| o.poll_officer? }
end
private
def load_booth
@booth = ::Poll::Booth.find(params[:booth_id])
end
def load_shifts
@shifts = @booth.shifts
end
def load_officer
if params[:officer_id].present?
@officer = ::Poll::Officer.find(params[:officer_id])
end
end
def shift_params
date_attributes = [:vote_collection_date, :recount_scrutiny_date]
attributes = [:booth_id, :officer_id, :task, date: date_attributes]
shift_params = params.require(:shift).permit(*attributes)
shift_params.merge(date: shift_params[:date]["#{shift_params[:task]}_date".to_sym])
end
end