skill-assessment-app/app/controllers/admin/question_controller.rb
Mark Moser 4bbd93ded1 rubocop noise: fixes FrozenStringLiteralComment
Adding the .ruby-verison file triggered previously un-run cops, specifically:

  This cop is designed to help upgrade to Ruby 3.0. It will add the
  comment `# frozen_string_literal: true` to the top of files to enable
  frozen string literals. Frozen string literals will be default in Ruby
  3.0. The comment will be added below a shebang and encoding comment. The
  frozen string literal comment is only valid in Ruby 2.3+.

More info on rubocop [Automatic-Corrections](https://github.com/bbatsov/rubocop/wiki/Automatic-Corrections)
2016-09-08 10:30:13 -05:00

71 lines
2.0 KiB
Ruby

# frozen_string_literal: true
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: { success: "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: { success: "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_code] unless question_params[:live_code].nil?
question.delete(:multi_choice)
question.delete(:live_code)
question
end
end
end