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
|
2015-09-10 12:39:52 +03:00
|
|
|
load_and_authorize_resource except: [: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)
|
2015-09-10 12:39:52 +03:00
|
|
|
@questions = Question.where(global: true).all | Question.where(conference_id: @conference.id) | @conference.questions
|
|
|
|
|
@question = @conference.questions.new
|
2014-08-06 21:14:00 +03:00
|
|
|
end
|
2014-02-09 18:05:57 +02:00
|
|
|
|
2015-04-22 18:55:51 +03:00
|
|
|
def show
|
2015-09-10 12:39:52 +03:00
|
|
|
@registrations = @conference.registrations.joins(:qanswers).where(qanswers: { question: @question })
|
2015-04-22 18:55:51 +03:00
|
|
|
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
|
|
|
|
|
@question = @conference.questions.new(params[:question])
|
|
|
|
|
@question.conference_id = @conference.id
|
2015-09-10 12:39:52 +03:00
|
|
|
|
|
|
|
|
# We need to authorize the @question after a conference_id has been associated with the question,
|
|
|
|
|
# because authorization in ability.rb is based on existence of conference_id attribute
|
|
|
|
|
# (and the controller does not authorize through conference)
|
2014-08-12 11:51:59 +03:00
|
|
|
authorize! :create, @question
|
2014-02-09 18:05:57 +02:00
|
|
|
|
2015-09-10 12:39:52 +03:00
|
|
|
if @question.question_type == QuestionType.find_by(title: 'Yes/No')
|
|
|
|
|
@question.answers = [ Answer.find_or_create_by(title: 'Yes'), Answer.find_or_create_by(title: 'No') ]
|
2014-11-09 14:20:36 +02:00
|
|
|
end
|
|
|
|
|
|
2014-08-06 21:14:00 +03:00
|
|
|
respond_to do |format|
|
2015-09-10 12:39:52 +03:00
|
|
|
# Do not automatically associate newly created question with the conference. The new question shall be enabled for the conference manually.
|
|
|
|
|
if @question.save
|
2014-08-06 21:14:00 +03:00
|
|
|
format.html { redirect_to admin_conference_questions_path, notice: 'Question was successfully created.' }
|
|
|
|
|
else
|
2014-08-19 13:01:39 +02:00
|
|
|
flash[:error] = "Oops, couldn't save Question. #{@question.errors.full_messages.join('. ')}"
|
2014-08-06 21:14:00 +03:00
|
|
|
format.html { redirect_to admin_conference_questions_path }
|
|
|
|
|
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
|
2015-09-10 12:39:52 +03:00
|
|
|
def edit; end
|
2014-02-09 18:05:57 +02:00
|
|
|
|
2014-08-06 21:14:00 +03:00
|
|
|
# PUT questions/1
|
|
|
|
|
def update
|
2015-09-10 12:39:52 +03:00
|
|
|
@question.assign_attributes(params[:question])
|
|
|
|
|
|
|
|
|
|
if @question.question_type == QuestionType.find_by(title: 'Yes/No')
|
|
|
|
|
@question.answers = [ Answer.find_or_create_by(title: 'Yes'), Answer.find_or_create_by(title: 'No') ]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if @question.save
|
|
|
|
|
if @question.answers.blank?
|
|
|
|
|
# A question without answers cannot be enabled for a conference
|
|
|
|
|
@conference.questions.delete(@question)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
redirect_to(admin_conference_questions_path(conference_id: @conference.short_title), notice: "Question '#{@question.title}' for #{@conference.short_title} updated successfully.")
|
2014-08-06 21:14:00 +03:00
|
|
|
else
|
2015-09-10 12:39:52 +03:00
|
|
|
flash[:error] = "Update of questions for #{@conference.short_title} failed. #{@question.errors.full_messages.join('. ')}"
|
|
|
|
|
redirect_to admin_conference_questions_path(conference_id: @conference.short_title)
|
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
|
2015-09-10 12:39:52 +03:00
|
|
|
def toggle_question
|
2014-08-13 22:46:41 +03:00
|
|
|
authorize! :update, Question.new(conference_id: @conference.id)
|
2015-09-10 12:39:52 +03:00
|
|
|
|
|
|
|
|
ids = @conference.question_ids
|
|
|
|
|
if params[:enable] == 'true'
|
|
|
|
|
ids = ids.push(@question.id)
|
|
|
|
|
elsif params[:enable] == 'false'
|
|
|
|
|
ids.delete(@question.id)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if @conference.update_attributes(question_ids: ids)
|
|
|
|
|
flash[:notice] = "Questions for #{@conference.short_title} successfully updated. Note: Only questions with answers can be enabled for a conference."
|
2014-08-06 21:14:00 +03:00
|
|
|
else
|
2015-09-10 12:39:52 +03:00
|
|
|
flash[:error] = "Update of questions for #{@conference.short_title} failed."
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if request.xhr?
|
|
|
|
|
render js: 'index'
|
|
|
|
|
else
|
|
|
|
|
redirect_to admin_conference_questions_path(conference_id: @conference.short_title)
|
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
|
2015-09-10 12:39:52 +03:00
|
|
|
if !@question.global || @question.conferences.blank?
|
2014-02-09 18:05:57 +02:00
|
|
|
|
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-06 21:14:00 +03:00
|
|
|
@question.answers.each do |a|
|
2015-09-10 12:39:52 +03:00
|
|
|
a.destroy unless a.questions.any?
|
2014-08-06 21:14:00 +03:00
|
|
|
end
|
2015-09-10 12:39:52 +03:00
|
|
|
|
|
|
|
|
@question.destroy
|
|
|
|
|
flash[:notice] = "Deleted question: #{@question.title}"
|
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-08-06 21:14:00 +03:00
|
|
|
else
|
2015-09-10 12:39:52 +03:00
|
|
|
flash[:error] = 'You cannot delete global questions that are currently being used for a conference.'
|
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
|
|
|
|
|
|
2015-09-10 12:39:52 +03:00
|
|
|
@questions = Question.where(global: true).all | Question.where(conference_id: @conference.id) | @conference.questions
|
|
|
|
|
redirect_to admin_conference_questions_path(@conference.short_title)
|
2014-08-06 21:14:00 +03:00
|
|
|
end
|
2014-02-09 18:05:57 +02:00
|
|
|
end
|
2014-06-30 17:07:53 +02:00
|
|
|
end
|