skill-assessment-app/app/controllers/admin/question_controller.rb
2016-08-22 16:26:21 -05:00

65 lines
1.8 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
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