Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle nil in find_by_email #1876

Merged
merged 1 commit into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app/models/concerns/emailable.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Emailable
extend ActiveSupport::Concern

Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions spec/models/staff_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
2 changes: 2 additions & 0 deletions spec/models/teacher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
9 changes: 9 additions & 0 deletions spec/support/shared_examples/emailable.rb
Original file line number Diff line number Diff line change
@@ -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
Loading