osem-fcy/app/controllers/admin/questions_controller.rb

125 lines
4.6 KiB
Ruby
Raw Normal View History

2014-08-06 21:14:00 +03:00
module Admin
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 except: [:create]
2014-02-09 18:05:57 +02:00
2014-08-06 21:14:00 +03:00
def index
authorize! :index, Question.new(conference_id: @conference.id)
@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
@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
# 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
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
2014-08-06 21:14:00 +03:00
respond_to do |format|
# 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
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
def edit; end
2014-02-09 18:05:57 +02:00
2014-08-06 21:14:00 +03:00
# PUT questions/1
def update
@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
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
def toggle_question
authorize! :update, Question.new(conference_id: @conference.id)
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
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
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|
a.destroy unless a.questions.any?
2014-08-06 21:14:00 +03:00
end
@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
flash[:error] = 'Could not delete question.'
2014-07-21 14:27:32 +02:00
end
2014-08-06 21:14:00 +03:00
else
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
flash[:error] = 'You must be an admin to delete a question.'
2014-02-09 18:05:57 +02:00
end
@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