70 lines
2.0 KiB
Ruby
70 lines
2.0 KiB
Ruby
module Admin
|
|
class QuestionController < AdminController
|
|
def index
|
|
@questions = Question.includes(:quiz).order("quizzes.name", { active: :desc }, :sort)
|
|
end
|
|
|
|
def new
|
|
@question = Question.new(active: true)
|
|
@quizzes = Quiz.all
|
|
end
|
|
|
|
def create
|
|
@quizzes = Quiz.all
|
|
@question = Question.create(process_question_params)
|
|
|
|
if @question.persisted?
|
|
redirect_to admin_questions_path, flash: { notice: "Sucessfully created question" }
|
|
else
|
|
flash[:error] = "Failed to save question."
|
|
render :new
|
|
end
|
|
end
|
|
|
|
def view
|
|
@question = Question.includes(:quiz).find(params[:question_id])
|
|
end
|
|
|
|
def edit
|
|
@quizzes = Quiz.all
|
|
@question = Question.includes(:quiz).find(params[:question_id])
|
|
end
|
|
|
|
def update
|
|
@quizzes = Quiz.all
|
|
@question = Question.find(params[:question_id])
|
|
|
|
if @question.update_attributes(process_question_params)
|
|
redirect_to admin_question_path(@question.to_i),
|
|
flash: { notice: "Sucessfully updated question" }
|
|
else
|
|
flash[:error] = "Failed to update question."
|
|
render :edit
|
|
end
|
|
end
|
|
|
|
def options
|
|
@question = params[:question_id].present? ? Question.find(params[:question_id]) : Question.new
|
|
render layout: false
|
|
end
|
|
|
|
private
|
|
|
|
def question_params
|
|
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?
|
|
question[:input_options] = question_params[:live_coder] unless question_params[:live_coder].nil?
|
|
question.delete(:multi_choice)
|
|
question.delete(:live_coder)
|
|
question
|
|
end
|
|
end
|
|
end
|