From d337d1d6ec8017a5fb61ad1f43f9544d8ae4e01f Mon Sep 17 00:00:00 2001 From: Maximilian Mayer Date: Thu, 16 Jan 2020 10:46:33 +0100 Subject: [PATCH] #164 make enrollment key optional (#194) * #164 make enrollment key optional * fix rubocop errors --- app/models/lecture.rb | 6 +++++- spec/models/lecture_spec.rb | 15 ++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/app/models/lecture.rb b/app/models/lecture.rb index c2b66d67..6ba44b93 100755 --- a/app/models/lecture.rb +++ b/app/models/lecture.rb @@ -11,7 +11,7 @@ class Lecture < ApplicationRecord validates :end_time, presence: true validates :name, presence: true, length: { in: 2..40 } - validates :enrollment_key, presence: true, length: { in: 3..20 } + validates :enrollment_key, length: { in: 3..20, if: :enrollment_key_present? } scope :active, -> { where status: "running" } @@ -61,4 +61,8 @@ def readonly? end false end + + def enrollment_key_present? + enrollment_key.present? + end end diff --git a/spec/models/lecture_spec.rb b/spec/models/lecture_spec.rb index 177f3678..a13c5a45 100644 --- a/spec/models/lecture_spec.rb +++ b/spec/models/lecture_spec.rb @@ -14,8 +14,21 @@ expect(@lecture).not_to be_valid end - it "is not valid without a enrollment key" do + it "is valid without an enrollment key" do @lecture.enrollment_key = "" + expect(@lecture).to be_valid + end + + it "is valid with an enrollment key with 3 characters" do + @lecture.enrollment_key = "123" + expect(@lecture).to be_valid + end + + it "is not valid with an enrollment key with 1 to 2 characters" do + @lecture.enrollment_key = "1" + expect(@lecture).not_to be_valid + + @lecture.enrollment_key = "12" expect(@lecture).not_to be_valid end