2014-07-11 18:54:51 +03:00
module Admin
class QuestionsController < ApplicationController
load_and_authorize_resource :conference , find_by : :short_title
authorize_resource :question , through : :conference
2014-02-09 18:05:57 +02:00
2014-07-11 18:54:51 +03:00
def index
@questions = Question . where ( global : true ) . all | Question . where ( conference_id : @conference . id )
@questions_conference = @conference . questions
@new_question = @conference . questions . new
end
2014-02-09 18:05:57 +02:00
2014-07-11 18:54:51 +03:00
def new
@new_question = @conference . questions . new
end
2014-02-09 18:05:57 +02:00
2014-07-11 18:54:51 +03:00
def create
@question = @conference . questions . new ( params [ :question ] )
@question . conference_id = @conference . id
2014-02-09 18:05:57 +02:00
2014-07-11 18:54:51 +03:00
respond_to do | format |
if @conference . save
format . html { redirect_to admin_conference_questions_path , notice : 'Question was successfully created.' }
else
flash [ :error ] = " Oops, couldn't save. Question and answer(s) have titles? "
format . html { redirect_to admin_conference_questions_path }
end
2014-02-09 18:05:57 +02:00
end
end
2014-07-11 18:54:51 +03:00
# GET questions/1/edit
def edit
@question = Question . find ( params [ :id ] )
2014-02-09 18:05:57 +02:00
2014-07-11 18:54:51 +03:00
if @question . global == true && ! ( current_user . has_role? :organizer , @conference )
redirect_to ( admin_conference_questions_path ( conference_id : @conference . short_title ) , alert : " Sorry, you cannot edit global questions. Create a new one. " )
end
2014-02-09 18:05:57 +02:00
end
2014-07-11 18:54:51 +03:00
# PUT questions/1
def update
@question = Question . find ( params [ :id ] )
2014-02-09 18:05:57 +02:00
2014-07-11 18:54:51 +03:00
if @conference . update_attributes ( params [ :conference ] )
redirect_to ( admin_conference_questions_path ( conference_id : @conference . short_title ) , notice : " Questions for #{ @conference . short_title } successfully updated. " )
else
redirect_to ( admin_conference_questions_path ( conference_id : @conference . short_title ) , notice : " Update of questions for #{ @conference . short_title } failed. " )
end
2014-02-09 18:05:57 +02:00
end
2014-07-11 18:54:51 +03:00
# Update questions used for the conference
def update_conference
if @conference . update_attributes ( params [ :conference ] )
redirect_to ( admin_conference_questions_path ( :conference_id = > @conference . short_title ) , :notice = > " Questions for #{ @conference . short_title } successfully updated. " )
else
redirect_to ( admin_conference_questions_path ( :conference_id = > @conference . short_title ) , :notice = > " Update of questions for #{ @conference . short_title } failed. " )
end
end
2014-02-09 18:05:57 +02:00
2014-07-11 18:54:51 +03:00
# DELETE questions/1
def destroy
if can? :destroy , @question
2014-02-09 18:05:57 +02:00
2014-07-11 18:54:51 +03:00
# Do not delete global questions
if @question . global == false
2014-02-09 18:05:57 +02:00
2014-07-11 18:54:51 +03:00
# Delete question and its answers
begin
Question . transaction do
@question . delete
@question . answers . each do | a |
a . delete
end
flash [ :notice ] = " Deleted question: #{ @question . title } and its answers: #{ @question . answers . map { | a | a . title } . join ',' } "
end
rescue ActiveRecord :: RecordInvalid
flash [ :error ] = " Could not delete question. "
2014-07-21 14:27:32 +02:00
end
2014-07-11 18:54:51 +03:00
else
flash [ :error ] = " You cannot delete global questions. "
2014-02-09 18:05:57 +02:00
end
else
2014-07-11 18:54:51 +03:00
flash [ :error ] = " You must be an admin to delete a question. "
2014-02-09 18:05:57 +02:00
end
2014-07-21 14:49:05 +02:00
@questions = Question . where ( global : true ) . all | Question . where ( conference_id : @conference . id )
2014-07-21 14:27:32 +02:00
@questions_conference = @conference . questions
2014-02-09 18:05:57 +02:00
end
2014-06-30 17:07:53 +02:00
end