2018-05-07 16:47:29 -07:00
# frozen_string_literal: true
2014-08-06 21:14:00 +03:00
module Admin
2014-08-13 14:37:05 +03:00
class QuestionsController < Admin :: BaseController
2014-08-12 11:51:59 +03:00
load_and_authorize_resource :conference , find_by : :short_title
load_and_authorize_resource through : :conference , except : [ :new , :create ]
2014-02-09 18:05:57 +02:00
2014-08-06 21:14:00 +03:00
def index
2014-08-13 22:46:41 +03:00
authorize! :index , Question . new ( conference_id : @conference . id )
2016-05-25 14:40:45 +02:00
@questions = Question . where ( global : true ) | @conference . questions
2014-08-06 21:14:00 +03:00
@questions_conference = @conference . questions
@new_question = @conference . questions . new
end
2014-02-09 18:05:57 +02:00
2015-04-22 18:55:51 +03:00
def show
@registrations = @conference . registrations . joins ( :qanswers ) . uniq
end
2014-08-06 21:14:00 +03:00
def new
2014-08-12 11:51:59 +03:00
@question = Question . new ( conference_id : @conference . id )
authorize! :create , @question
2014-08-06 21:14:00 +03:00
end
2014-02-09 18:05:57 +02:00
2014-08-06 21:14:00 +03:00
def create
2015-10-28 18:05:15 +02:00
@question = @conference . questions . new ( question_params )
2014-08-06 21:14:00 +03:00
@question . conference_id = @conference . id
2014-08-12 11:51:59 +03:00
authorize! :create , @question
2014-02-09 18:05:57 +02:00
2014-11-09 14:20:36 +02:00
if @question . question_type_id == QuestionType . find_by ( title : 'Yes/No' ) . id
@question . answers = [ Answer . find_by ( title : 'Yes' ) , Answer . find_by ( title : 'No' ) ]
end
2014-08-06 21:14:00 +03:00
respond_to do | format |
if @conference . save
format . html { redirect_to admin_conference_questions_path , notice : 'Question was successfully created.' }
else
2016-03-10 14:25:38 +05:30
format . html { redirect_to admin_conference_questions_path , error : " Oops, couldn't save Question. #{ @question . errors . full_messages . join ( '. ' ) } " }
2014-08-06 21:14:00 +03:00
end
2014-02-09 18:05:57 +02:00
end
2014-08-06 21:14:00 +03:00
end
2014-02-09 18:05:57 +02:00
2014-08-06 21:14:00 +03:00
# GET questions/1/edit
def edit
2014-08-13 22:46:41 +03:00
if @question . global
2016-03-16 11:20:56 +05:30
redirect_to admin_conference_questions_path ( conference_id : @conference . short_title ) , error : 'Sorry, you cannot edit global questions. Create a new one.'
2014-08-06 21:14:00 +03:00
end
2014-02-09 18:05:57 +02:00
end
2014-08-06 21:14:00 +03:00
# PUT questions/1
def update
2022-02-14 17:53:58 +01:00
if @question . update ( question_params )
2016-03-11 02:08:00 +05:30
redirect_to admin_conference_questions_path ( conference_id : @conference . short_title ) , notice : " Question ' #{ @question . title } ' for #{ @conference . short_title } successfully updated. "
2014-08-06 21:14:00 +03:00
else
2016-03-11 02:08:00 +05:30
redirect_to admin_conference_questions_path ( conference_id : @conference . short_title ) , notice : " Update of questions for #{ @conference . short_title } failed. #{ @question . errors . full_messages . join ( '. ' ) } "
2014-08-06 21:14:00 +03:00
end
2014-02-09 18:05:57 +02:00
end
2014-08-06 21:14:00 +03:00
# Update questions used for the conference
def update_conference
2014-08-13 22:46:41 +03:00
authorize! :update , Question . new ( conference_id : @conference . id )
2022-02-14 17:53:58 +01:00
if @conference . update ( conference_params )
2016-03-11 02:08:00 +05:30
redirect_to admin_conference_questions_path ( conference_id : @conference . short_title ) , notice : " Questions for #{ @conference . short_title } successfully updated. "
2014-08-06 21:14:00 +03:00
else
2016-03-11 02:08:00 +05:30
redirect_to admin_conference_questions_path ( conference_id : @conference . short_title ) , notice : " Update of questions for #{ @conference . short_title } failed. "
2014-08-06 21:14:00 +03:00
end
2014-02-09 18:05:57 +02:00
end
2014-08-06 21:14:00 +03:00
# DELETE questions/1
def destroy
2014-08-12 11:51:59 +03:00
if can? :destroy , @question
2014-08-06 21:14:00 +03:00
# Do not delete global questions
2016-08-08 23:57:03 +02:00
if @question . global
flash [ :error ] = 'You cannot delete global questions.'
else
2014-08-06 21:14:00 +03:00
# Delete question and its answers
begin
Question . transaction do
2014-02-09 18:05:57 +02:00
2014-08-12 11:51:59 +03:00
@question . destroy
2014-08-06 21:14:00 +03:00
@question . answers . each do | a |
2014-08-13 22:46:41 +03:00
a . destroy
2014-08-06 21:14:00 +03:00
end
flash [ :notice ] = " Deleted question: #{ @question . title } and its answers: #{ @question . answers . map { | a | a . title } . join ',' } "
2014-08-12 20:23:51 +03:00
end
2014-08-06 21:14:00 +03:00
rescue ActiveRecord :: RecordInvalid
2014-08-18 21:54:43 +03:00
flash [ :error ] = 'Could not delete question.'
2014-07-21 14:27:32 +02:00
end
2014-02-09 18:05:57 +02:00
end
else
2014-08-18 21:54:43 +03:00
flash [ :error ] = 'You must be an admin to delete a question.'
2014-02-09 18:05:57 +02:00
end
2014-08-12 15:48:29 +03:00
@questions = Question . where ( global : true ) . all | Question . where ( conference_id : @conference . id )
2014-08-06 21:14:00 +03:00
@questions_conference = @conference . questions
end
2015-10-28 18:05:15 +02:00
private
def question_params
2016-12-15 11:50:23 +01:00
params . require ( :question ) . permit ( :title , :global , :answer_ids , :question_type_id , :conference_id , answers_attributes : [ :id , :title ] )
2015-10-28 18:05:15 +02:00
end
def conference_params
params . require ( :conference ) . permit ( question_ids : [ ] )
end
2014-02-09 18:05:57 +02:00
end
2014-06-30 17:07:53 +02:00
end