-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add public changes to create and vote Poll:Questions with votation type
- Loading branch information
1 parent
23d3683
commit c6e4b24
Showing
22 changed files
with
713 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
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.count + 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].present? ? params[:page] : 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.