Skip to content

Commit

Permalink
Add Wizards::Steps::MainEmail model
Browse files Browse the repository at this point in the history
  • Loading branch information
rnestler committed Jun 18, 2024
1 parent f60bdf8 commit 3cd2bb1
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
30 changes: 30 additions & 0 deletions app/models/wizards/steps/main_email.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

# Copyright (c) 2024, Schweizer Alpen-Club. This file is part of
# hitobito_sac_cas and licensed under the Affero General Public License version 3
# or later. See the COPYING file at the top-level directory or at
# https://github.com/hitobito/hitobito_sac_cas.

class Wizards::Steps::MainEmail < Wizards::Step
include ValidatedEmail

attribute :email, :string

validates :email, presence: true
validate :ensure_email_available

# #email_changed? is used in `ValidatedEmail` to determine if the email
# should be validated. Here it should only be validated if the email is
# present.
def email_changed?
email.present?
end

def ensure_email_available
return if email.blank?

if Person.exists?(email: email)
errors.add(:email, I18n.t('activerecord.errors.models.person.attributes.email.taken'))
end
end
end
49 changes: 49 additions & 0 deletions spec/models/wizards/steps/main_email_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true

#
# Copyright (c) 2023, Schweizer Alpen-Club. This file is part of
# hitobito_sac_cas and licensed under the Affero General Public License version 3
# or later. See the COPYING file at the top-level directory or at
# https://github.com/hitobito/hitobito_sac_cas.

require 'spec_helper'

describe Wizards::Steps::MainEmail do
let(:params) { {} }
let(:wizard) { nil } # we don't need a wizard for the model specs
let(:subject) { described_class.new(wizard, **params) }

describe 'validations' do
context 'without email' do
it do
is_expected.not_to be_valid
expect(subject.errors[:email].count).to eq 1
end
end

context 'with invalid email' do
before { allow(Truemail).to receive(:valid?).and_return(false) }

it do
is_expected.not_to be_valid
expect(subject.errors[:email].count).to eq 1
end
end

context 'with valid email' do
let(:params) { { email: 'foo@bar.ch' } }

it { is_expected.to be_valid }

context 'when email is already taken' do
before { Fabricate(:person, email: params[:email]) }

it do
is_expected.not_to be_valid
expect(subject.errors[:email].count).to eq 1
end

end
end
end
end

0 comments on commit 3cd2bb1

Please sign in to comment.