Files
nairobi/app/controllers/polls/answers_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

71 lines
2.3 KiB
Ruby

class Polls::AnswersController < ApplicationController
load_and_authorize_resource :poll
load_and_authorize_resource :question, class: "Poll::Question"
authorize_resource :answer, class: "Poll::Answer"
def create
@question = Poll::Question.find_by(id: params[:id])
if @question.votation_type.open? && !check_question_answer_exist
@question.question_answers.create!(
title: params[:answer],
given_order: @question.question_answers.maximum(:given_order).to_i + 1,
hidden: false
)
flash.now[:notice] = t("dashboard.polls.index.succesfull")
else
flash.now[:alert] = "Unfortunately failed to sent"
end
load_for_answers
if @question.enum_type&.include?("answer_couples")
last_pair ||= generate_and_store_new_pair(@question)
@last_pair_question_answers = { @question.id => last_pair }
end
render "polls/questions/answer", format: :js
end
def delete
@question = Poll::Question.find_by(id: params[:id])
!@question.answers.find_by(author: current_user, answer: params[:answer]).destroy
@question.question_answers.each do |question_answer|
question_answer.set_most_voted
end
question_answers
load_for_answers
if @question.enum_type&.include?("answer_couples")
last_pair ||= generate_and_store_new_pair(@question)
@last_pair_question_answers = { @question.id => last_pair }
end
render "polls/questions/answer", format: :js
end
private
def check_question_answer_exist
exist = false
@question.question_answers.each do |question_answer|
break if exist
exist = true if question_answer.title == params[:answer]
end
exist
end
def load_for_answers
@page = params[:page].presence || 1
question_answers
@answers_by_question_id = { @question.id => @question.answers
.by_author(current_user)
.order(:order)
.pluck(:answer) }
end
def question_answers
if @question.is_positive_negative?
@answers = @question.question_answers.visibles.page(params[:page])
else
@answers = @question.question_answers.visibles
end
end
end