move quiz faker to new dev utilities location
This commit is contained in:
@ -1,10 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
namespace :faker do
|
||||
desc "Generates 10 fake candidates with results."
|
||||
task results: :environment do
|
||||
puts "Not for production use" and return if Rails.env == "production"
|
||||
|
||||
faker = FakeQuiz.new
|
||||
faker.create_completed_quizzes 10
|
||||
end
|
||||
end
|
69
lib/utils/fake_quiz.rb
Normal file
69
lib/utils/fake_quiz.rb
Normal file
@ -0,0 +1,69 @@
|
||||
# frozen_string_literal: true
|
||||
class FakeQuiz
|
||||
def create_completed_quizzes num = 10
|
||||
num.times do
|
||||
candidate = create_candidate Faker::Name.name
|
||||
answer_questions candidate
|
||||
candidate.update_attributes(completed: true, completed_at: Time.zone.now - rand(0..112).days)
|
||||
candidate.build_reviews
|
||||
end
|
||||
end
|
||||
|
||||
def create_candidate name
|
||||
Candidate.create(name: name,
|
||||
email: "#{Faker::Internet.user_name(name)}@mailinator.com",
|
||||
experience: rando_experience,
|
||||
project: Faker::Company.name,
|
||||
recruiter_id: recruiter_id,
|
||||
quiz_id: fed_quiz_id)
|
||||
end
|
||||
|
||||
def answer_questions candidate
|
||||
candidate.quiz.questions.each do |question|
|
||||
candidate.answers.create(question_id: question.id,
|
||||
answer: generate_answer(question),
|
||||
submitted: true)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def fed_quiz_id
|
||||
Quiz.find_by(dept: 'fed').id
|
||||
end
|
||||
|
||||
def recruiter_id
|
||||
User.find_by(name: 'Sam Recruiter').id
|
||||
end
|
||||
|
||||
def rando_experience
|
||||
%w(0-3 4-6 7-9 10-14 15+)[rand(0..4)]
|
||||
end
|
||||
|
||||
def generate_answer question # rubocop:disable Metrics/MethodLength
|
||||
case question.input_type
|
||||
when "checkbox"
|
||||
question.input_options
|
||||
when "checkbox_other"
|
||||
{
|
||||
other: Faker::TwinPeaks.quote,
|
||||
options: question.input_options
|
||||
}
|
||||
when "radio"
|
||||
question.input_options.sample
|
||||
when "radio_other"
|
||||
{
|
||||
other: Faker::TwinPeaks.quote,
|
||||
options: question.input_options.sample
|
||||
}
|
||||
when "live_code"
|
||||
{
|
||||
html: "<p>#{Faker::TwinPeaks.quote}</p>",
|
||||
css: "body {color: #{Faker::Color.hex_color}}",
|
||||
text: Faker::TwinPeaks.quote
|
||||
}
|
||||
else
|
||||
Faker::TwinPeaks.quote
|
||||
end
|
||||
end # rubocop:enable Metrics/MethodLength
|
||||
end
|
Reference in New Issue
Block a user