diff --git a/app/models/concerns/emailable.rb b/app/models/concerns/emailable.rb index f682185d3d..91722e1712 100644 --- a/app/models/concerns/emailable.rb +++ b/app/models/concerns/emailable.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Emailable extend ActiveSupport::Concern @@ -14,7 +16,7 @@ module Emailable class_methods do def find_by_email(email) - find_by("LOWER(email) = ?", email.downcase) + find_by("LOWER(email) = ?", email&.downcase) end def find_or_initialize_by_email(email) diff --git a/spec/models/staff_spec.rb b/spec/models/staff_spec.rb index f0a7d12619..2f43d6cc8d 100644 --- a/spec/models/staff_spec.rb +++ b/spec/models/staff_spec.rb @@ -55,6 +55,8 @@ RSpec.describe Staff, type: :model do subject(:staff) { build(:staff) } + it_behaves_like "an emailable" + describe "validations" do it { is_expected.to be_valid } diff --git a/spec/models/teacher_spec.rb b/spec/models/teacher_spec.rb index c7989e72fd..414caea627 100644 --- a/spec/models/teacher_spec.rb +++ b/spec/models/teacher_spec.rb @@ -27,6 +27,8 @@ RSpec.describe Teacher, type: :model do subject(:teacher) { create(:teacher) } + it_behaves_like "an emailable" + describe "validations" do it { is_expected.to be_valid } diff --git a/spec/support/shared_examples/emailable.rb b/spec/support/shared_examples/emailable.rb new file mode 100644 index 0000000000..16dc5b40db --- /dev/null +++ b/spec/support/shared_examples/emailable.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +RSpec.shared_examples "an emailable" do + describe "#find_by_email" do + it "accepts nil" do + expect { described_class.find_by_email(nil) }.to_not raise_error + end + end +end