Files
nairobi/app/controllers/admin/poll/polls_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

97 lines
2.2 KiB
Ruby

class Admin::Poll::PollsController < Admin::Poll::BaseController
include Translatable
include ImageAttributes
include ReportAttributes
load_and_authorize_resource
before_action :load_search, only: [:search_booths, :search_officers]
before_action :load_geozones, only: [:new, :create, :edit, :update]
def index
@polls = Poll.not_budget.created_by_admin.order(starts_at: :desc)
end
def show
@poll = Poll.find(params[:id])
end
def new
end
def create
@poll = Poll.new(poll_params.merge(author: current_user))
if @poll.save
notice = t("flash.actions.create.poll")
if @poll.budget.present?
redirect_to admin_poll_booth_assignments_path(@poll), notice: notice
else
redirect_to [:admin, @poll], notice: notice
end
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 booth_assignments
@polls = Poll.current.created_by_admin
end
def destroy
if ::Poll::Voter.where(poll: @poll).any?
redirect_to admin_poll_path(@poll), alert: t("admin.polls.destroy.unable_notice")
else
@poll.destroy!
redirect_to admin_polls_path, notice: t("admin.polls.destroy.success_notice")
end
end
private
def load_geozones
@geozones = Geozone.all.order(:name)
end
def poll_params
attributes = [:name, :starts_at, :ends_at, :geozone_restricted, :budget_id,
geozone_ids: [], image_attributes: image_attributes]
params.require(:poll).permit(*attributes, *report_attributes, translation_params(Poll))
end
def search_params
params.permit(:poll_id, :search)
end
def load_search
@search = search_params[:search]
end
def resource
@poll ||= Poll.find(params[:id])
end
end