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

80 lines
2.2 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2016-08-17 17:49:09 -05:00
module Admin
class QuestionController < AdminController
def index
2016-09-20 18:17:27 -05:00
@questions = policy_scope Question.includes(:quiz).order("quizzes.name", { active: :desc }, :sort)
2016-08-17 17:49:09 -05:00
end
def new
2016-09-20 18:17:27 -05:00
authorize Question
2016-08-19 16:02:18 -05:00
@question = Question.new(active: true)
2016-09-20 18:17:27 -05:00
@quizzes = policy_scope Quiz.all
2016-08-17 17:49:09 -05:00
end
def create
2016-09-20 18:17:27 -05:00
authorize Quiz
@quizzes = policy_scope 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-09-20 18:17:27 -05:00
authorize @question
2016-08-17 17:49:09 -05:00
end
def edit
2016-09-20 18:17:27 -05:00
@quizzes = policy_scope Quiz.all
2016-08-19 16:02:18 -05:00
@question = Question.includes(:quiz).find(params[:question_id])
2016-09-20 18:17:27 -05:00
authorize @question
2016-08-17 17:49:09 -05:00
end
def update
2016-09-20 18:17:27 -05:00
@quizzes = policy_scope Quiz.all
2016-08-19 16:02:18 -05:00
@question = Question.find(params[:question_id])
2016-09-20 18:17:27 -05:00
authorize @question
2016-08-19 16:02:18 -05:00
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
def options
@question = params[:question_id].present? ? Question.find(params[:question_id]) : Question.new
2016-09-20 18:17:27 -05:00
authorize @question
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(
2016-11-16 18:44:18 -06:00
:quiz_id, :question, :category, :attachment, :input_type, :sort, :active, :input_options,
2016-08-19 16:41:44 -05:00
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