From 15a5b6b706a655a1636b7bda717b9dde8dd4e12f Mon Sep 17 00:00:00 2001 From: Mark Moser Date: Mon, 5 Jun 2017 11:27:48 -0500 Subject: [PATCH] limit candidate quiz dropdown to only show "active" quizzes completes #105 --- app/controllers/admin/candidate_controller.rb | 2 +- app/models/quiz.rb | 6 ++++++ test/fixtures/quizzes.yml | 5 +++++ test/models/quiz_test.rb | 9 ++++++--- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/app/controllers/admin/candidate_controller.rb b/app/controllers/admin/candidate_controller.rb index 079ecea..fe66db5 100644 --- a/app/controllers/admin/candidate_controller.rb +++ b/app/controllers/admin/candidate_controller.rb @@ -61,7 +61,7 @@ module Admin end def collect_quizzes - @quizzes ||= Quiz.order(:name) + @quizzes ||= Quiz.all_active.order(:name) end def send_notifications candidate diff --git a/app/models/quiz.rb b/app/models/quiz.rb index c1ed37b..fd57567 100644 --- a/app/models/quiz.rb +++ b/app/models/quiz.rb @@ -8,4 +8,10 @@ class Quiz < ApplicationRecord validates :name, presence: true, uniqueness: true validates :dept, presence: true validates :unit, presence: true + + class << self + def all_active + Quiz.joins(:questions).distinct + end + end end diff --git a/test/fixtures/quizzes.yml b/test/fixtures/quizzes.yml index 5f4aa1d..4aead31 100644 --- a/test/fixtures/quizzes.yml +++ b/test/fixtures/quizzes.yml @@ -14,3 +14,8 @@ studio: name: Studio Screening One unit: PD Studio dept: FED + +empty_quiz: + name: A quiz with no questions + unit: PD + dept: FED diff --git a/test/models/quiz_test.rb b/test/models/quiz_test.rb index 63a5253..7182d12 100644 --- a/test/models/quiz_test.rb +++ b/test/models/quiz_test.rb @@ -2,7 +2,10 @@ require 'test_helper' class QuizTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end + test "all_active only returns quizzes with questions" do + empty_quiz = quizzes(:empty_quiz) + quizzes = Quiz.all_active + + refute quizzes.any? { |q| q.id == empty_quiz.id } + end end