Skip to content

Commit 6238e08

Browse files
committed
Prevent School#verified_at being changed once set
In preparation for creating a school in Profile API once it's verified. Reducing the number of states a school can be in simplifies our interactions with Profile API (we don't have to update/delete schools in Profile API that were verified and then rejected, for example).
1 parent 47ce330 commit 6238e08

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

app/models/school.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class School < ApplicationRecord
1919
validates :creator_agree_terms_and_conditions, presence: true, acceptance: true
2020
validates :rejected_at, absence: { if: proc { |school| school.verified? } }
2121
validates :verified_at, absence: { if: proc { |school| school.rejected? } }
22+
validate :verified_at_cannot_be_changed
2223

2324
before_validation :normalize_reference
2425

@@ -47,4 +48,8 @@ def rejected?
4748
def normalize_reference
4849
self.reference = nil if reference.blank?
4950
end
51+
52+
def verified_at_cannot_be_changed
53+
errors.add(:verified_at, 'cannot be changed after verification') if verified_at_was.present? && verified_at_changed?
54+
end
5055
end

spec/models/school_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,12 @@
189189
school.update(verified_at: Time.zone.now)
190190
expect(school.errors[:verified_at]).to include('must be blank')
191191
end
192+
193+
it "cannot change #verified_at once it's been set" do
194+
school.update!(verified_at: Time.zone.now)
195+
school.update(verified_at: nil)
196+
expect(school.errors[:verified_at]).to include('cannot be changed after verification')
197+
end
192198
end
193199

194200
describe '#creator' do

spec/services/school_verification_service_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,12 @@
8383
school.reload
8484
end
8585

86-
it 'sets verified_at to nil' do
87-
expect(school.verified_at).to be_nil
86+
it 'does not reset verified_at' do
87+
expect(school.verified_at).to be_present
8888
end
8989

90-
it 'sets rejected_at to a date' do
91-
expect(school.rejected_at).to be_a(ActiveSupport::TimeWithZone)
90+
it 'does not set rejected_at' do
91+
expect(school.rejected_at).to be_nil
9292
end
9393
end
9494
end

0 commit comments

Comments
 (0)