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

108 lines
3.9 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
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: [:new, :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) | @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
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
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
if @question.global
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
if @question.update(question_params)
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
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
authorize! :update, Question.new(conference_id: @conference.id)
if @conference.update(conference_params)
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
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
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|
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
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
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
params.require(:question).permit(:title, :global, :answer_ids, :question_type_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