skill-assessment-app/app/controllers/admin/question_controller.rb

70 lines
2.0 KiB
Ruby
Raw Normal View History

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?
redirect_to admin_questions_path, flash: { notice: "Sucessfully created question" }
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),
flash: { notice: "Sucessfully updated question" }
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
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?
question[:input_options] = question_params[:live_coder] unless question_params[:live_coder].nil?
question.delete(:multi_choice)
question.delete(:live_coder)
question
2016-08-19 16:02:18 -05:00
end
2016-08-17 17:49:09 -05:00
end
end