2016-09-08 10:25:33 -05:00
|
|
|
# frozen_string_literal: true
|
2016-08-17 17:49:09 -05:00
|
|
|
module Admin
|
|
|
|
class QuestionController < AdminController
|
|
|
|
def index
|
2016-08-19 16:02:18 -05:00
|
|
|
@questions = Question.includes(:quiz).order("quizzes.name", { active: :desc }, :sort)
|
2016-08-17 17:49:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
2016-08-19 16:02:18 -05:00
|
|
|
@question = Question.new(active: true)
|
|
|
|
@quizzes = Quiz.all
|
2016-08-17 17:49:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2016-08-19 16:02:18 -05:00
|
|
|
@quizzes = Quiz.all
|
2016-08-19 16:41:44 -05:00
|
|
|
@question = Question.create(process_question_params)
|
2016-08-19 16:02:18 -05:00
|
|
|
|
|
|
|
if @question.persisted?
|
2016-08-24 12:15:12 -05:00
|
|
|
redirect_to admin_questions_path, flash: { success: "Sucessfully created question" }
|
2016-08-19 16:02:18 -05:00
|
|
|
else
|
|
|
|
flash[:error] = "Failed to save question."
|
|
|
|
render :new
|
|
|
|
end
|
2016-08-17 17:49:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def view
|
2016-08-19 16:02:18 -05:00
|
|
|
@question = Question.includes(:quiz).find(params[:question_id])
|
2016-08-17 17:49:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
2016-08-19 16:02:18 -05:00
|
|
|
@quizzes = Quiz.all
|
|
|
|
@question = Question.includes(:quiz).find(params[:question_id])
|
2016-08-17 17:49:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2016-08-19 16:02:18 -05:00
|
|
|
@quizzes = Quiz.all
|
|
|
|
@question = Question.find(params[:question_id])
|
|
|
|
|
2016-08-19 16:41:44 -05:00
|
|
|
if @question.update_attributes(process_question_params)
|
2016-08-19 16:02:18 -05:00
|
|
|
redirect_to admin_question_path(@question.to_i),
|
2016-08-24 12:15:12 -05:00
|
|
|
flash: { success: "Sucessfully updated question" }
|
2016-08-19 16:02:18 -05:00
|
|
|
else
|
|
|
|
flash[:error] = "Failed to update question."
|
|
|
|
render :edit
|
|
|
|
end
|
2016-08-17 17:49:09 -05:00
|
|
|
end
|
2016-08-19 16:02:18 -05:00
|
|
|
|
2016-08-22 16:24:00 -05:00
|
|
|
def options
|
|
|
|
@question = params[:question_id].present? ? Question.find(params[:question_id]) : Question.new
|
|
|
|
render layout: false
|
|
|
|
end
|
|
|
|
|
2016-08-19 16:02:18 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def question_params
|
2016-08-19 16:41:44 -05:00
|
|
|
params.require(:question).permit(
|
|
|
|
:quiz_id, :question, :category, :input_type, :sort, :active, :input_options,
|
|
|
|
multi_choice: [], live_code: [:later, :html, :css, :js, :text]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_question_params
|
|
|
|
question = question_params
|
|
|
|
question[:input_options] = question_params[:multi_choice] unless question_params[:multi_choice].nil?
|
2016-08-23 21:54:53 -05:00
|
|
|
question[:input_options] = question_params[:live_code] unless question_params[:live_code].nil?
|
2016-08-19 16:41:44 -05:00
|
|
|
question.delete(:multi_choice)
|
2016-08-23 21:54:53 -05:00
|
|
|
question.delete(:live_code)
|
2016-08-19 16:41:44 -05:00
|
|
|
question
|
2016-08-19 16:02:18 -05:00
|
|
|
end
|
2016-08-17 17:49:09 -05:00
|
|
|
end
|
|
|
|
end
|