Skip to content
This repository has been archived by the owner on Mar 20, 2018. It is now read-only.

Commit

Permalink
Email professors on email change
Browse files Browse the repository at this point in the history
  • Loading branch information
keith committed Apr 15, 2014
1 parent bf6e234 commit 536f24c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
8 changes: 7 additions & 1 deletion app/models/professor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Professor < ActiveRecord::Base
EMAIL_REGEX = /\A\w+\z/

before_create { self.identifier = new_professor_identifier }
before_save { email.downcase! }
before_save { email.downcase!; email_changed }
has_many :positions
validate :static_identifier, on: :update
validates_presence_of :name
Expand All @@ -20,4 +20,10 @@ def to_param
def static_identifier
errors[:identifier] = "can't be changed" if self.identifier_changed?
end

def email_changed
return unless self.persisted?
return if self.email.downcase == self.email_was.downcase
ProfessorMailer.pending_recommendation(self).deliver
end
end
3 changes: 3 additions & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
# The :test delivery method accumulates sent emails in the
# ActionMailer::Base.deliveries array.
config.action_mailer.delivery_method = :test
config.action_mailer.default_url_options = {
host: 'localhost:3000'
}

# Print deprecation notices to the stderr.
config.active_support.deprecation = :stderr
Expand Down
35 changes: 35 additions & 0 deletions spec/models/professor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
describe Professor do
before { @prof = FactoryGirl.create(:professor) }
subject { @prof }
after(:each) do
ActionMailer::Base.deliveries.clear
end

it { should respond_to(:name) }
it { should respond_to(:email) }
Expand Down Expand Up @@ -32,4 +35,36 @@
before { @prof.identifier = 'abc' }
it { should_not be_valid }
end

describe 'changing professor emails' do
context 'email changed' do
before do
@prof.email = @prof.email + 'a'
@prof.save!
end

it 'should send an email' do
ActionMailer::Base.deliveries.count.should == 1
end
end

context 'unchanged email' do
before { @prof.save! }

it 'should not send an email' do
ActionMailer::Base.deliveries.count.should == 0
end
end

context 'email case changed' do
before do
@prof.email = @prof.email.upcase
@prof.save!
end

it 'should not send an email' do
ActionMailer::Base.deliveries.count.should == 0
end
end
end
end

0 comments on commit 536f24c

Please sign in to comment.