Skip to content

Commit 2ce285f

Browse files
committed
Extract School#reject
To complement `School#verify!`. Note that this doesn't need to be a bang method because the return value of calling `update` is passed through `SchoolVerificationService#reject` and back to `Admin::SchoolsController#reject`. The `School#verify!` method needs to raise on error so that it triggers a transaction rollback in `SchoolVerificationService#verify`.
1 parent 54610fe commit 2ce285f

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

app/models/school.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ def verify!
4747
update!(verified_at: Time.zone.now)
4848
end
4949

50+
def reject
51+
update(rejected_at: Time.zone.now)
52+
end
53+
5054
private
5155

5256
# Ensure the reference is nil, not an empty string

app/services/school_verification_service.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,5 @@ def verify
2323
end
2424
# rubocop:enable Metrics/AbcSize
2525

26-
def reject
27-
school.update(verified_at: nil, rejected_at: Time.zone.now)
28-
end
26+
delegate :reject, to: :school
2927
end

spec/models/school_spec.rb

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,12 @@
180180

181181
it 'cannot have #rejected_at set when #verified_at is present' do
182182
school.verify!
183-
school.update(rejected_at: Time.zone.now)
183+
school.reject
184184
expect(school.errors[:rejected_at]).to include('must be blank')
185185
end
186186

187187
it 'cannot have #verified_at set when #rejected_at is present' do
188-
school.update!(rejected_at: Time.zone.now)
188+
school.reject
189189
school.update(verified_at: Time.zone.now)
190190
expect(school.errors[:verified_at]).to include('must be blank')
191191
end
@@ -197,7 +197,7 @@
197197
end
198198

199199
it "cannot change #rejected_at once it's been set" do
200-
school.update!(rejected_at: Time.zone.now)
200+
school.reject
201201
school.update(rejected_at: nil)
202202
expect(school.errors[:rejected_at]).to include('cannot be changed after rejection')
203203
end
@@ -281,4 +281,20 @@
281281
expect { school.verify! }.to raise_error(ActiveRecord::RecordInvalid)
282282
end
283283
end
284+
285+
describe '#reject' do
286+
it 'sets rejected_at to the current time' do
287+
school.reject
288+
expect(school.rejected_at).to be_within(1.second).of(Time.zone.now)
289+
end
290+
291+
it 'returns true on successful rejection' do
292+
expect(school.reject).to be(true)
293+
end
294+
295+
it 'returns false on unsuccessful rejection' do
296+
school.verified_at = Time.zone.now
297+
expect(school.reject).to be(false)
298+
end
299+
end
284300
end

0 commit comments

Comments
 (0)