Skip to content

Commit 17840bf

Browse files
committed
Pass School instance to SchoolVerificationService
This feels more idiomatic. I don't understand why RuboCop is now complaining about the Metrics/AbcSize of `SchoolVerificationService#verify`.
1 parent 4d83db1 commit 17840bf

File tree

5 files changed

+16
-27
lines changed

5 files changed

+16
-27
lines changed

app/controllers/admin/schools_controller.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,29 @@
33
module Admin
44
class SchoolsController < Admin::ApplicationController
55
def verify
6-
school_id = params[:id]
7-
service = SchoolVerificationService.new(school_id)
6+
school = School.find(params[:id])
7+
service = SchoolVerificationService.new(school)
88

99
if service.verify
1010
flash[:notice] = t('administrate.controller.verify_school.success')
1111
else
1212
flash[:error] = t('administrate.controller.verify_school.error')
1313
end
1414

15-
redirect_to admin_school_path(id: school_id)
15+
redirect_to admin_school_path(school)
1616
end
1717

1818
def reject
19-
school_id = params[:id]
20-
service = SchoolVerificationService.new(school_id)
19+
school = School.find(params[:id])
20+
service = SchoolVerificationService.new(school)
2121

2222
if service.reject
2323
flash[:notice] = t('administrate.controller.reject_school.success')
2424
else
2525
flash[:error] = t('administrate.controller.reject_school.error')
2626
end
2727

28-
redirect_to admin_school_path(id: school_id)
28+
redirect_to admin_school_path(school)
2929
end
3030

3131
def default_sorting_attribute
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
# frozen_string_literal: true
22

33
class SchoolVerificationService
4-
def initialize(school_id)
5-
@school_id = school_id
4+
attr_reader :school
5+
6+
def initialize(school)
7+
@school = school
68
end
79

10+
# rubocop:disable Metrics/AbcSize
811
def verify
912
School.transaction do
10-
school = School.find(@school_id)
1113
school.update!(verified_at: Time.zone.now, rejected_at: nil)
12-
1314
Role.owner.create(user_id: school.creator_id, school:)
1415
Role.teacher.create(user_id: school.creator_id, school:)
1516
end
@@ -20,9 +21,9 @@ def verify
2021
else
2122
true
2223
end
24+
# rubocop:enable Metrics/AbcSize
2325

2426
def reject
25-
school = School.find(@school_id)
2627
school.update(verified_at: nil, rejected_at: Time.zone.now)
2728
end
2829
end

lib/tasks/classroom_management_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def create_school(creator_id, school_id = nil)
2525

2626
def verify_school(school)
2727
Rails.logger.info 'Verifying the school...'
28-
SchoolVerificationService.new(school.id).verify
28+
SchoolVerificationService.new(school).verify
2929
end
3030

3131
def create_school_class(teacher_id, school)

spec/features/admin/schools_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373

7474
before do
7575
stub_user_info_api_for(creator)
76-
allow(SchoolVerificationService).to receive(:new).with(school.id).and_return(verification_service)
76+
allow(SchoolVerificationService).to receive(:new).with(school).and_return(verification_service)
7777

7878
post verify_admin_school_path(school)
7979
end
@@ -115,7 +115,7 @@
115115

116116
before do
117117
stub_user_info_api_for(creator)
118-
allow(SchoolVerificationService).to receive(:new).with(school.id).and_return(verification_service)
118+
allow(SchoolVerificationService).to receive(:new).with(school).and_return(verification_service)
119119

120120
patch reject_admin_school_path(school)
121121
end

spec/services/school_verification_service_spec.rb

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
let(:school) { create(:school, creator_id: school_creator.id) }
77
let(:user) { create(:user) }
88
let(:school_creator) { create(:user) }
9-
let(:service) { described_class.new(school.id) }
9+
let(:service) { described_class.new(school) }
1010
let(:organisation_id) { SecureRandom.uuid }
1111

1212
describe '#verify' do
@@ -55,18 +55,6 @@
5555
expect(service.verify).to be(false)
5656
end
5757
end
58-
59-
describe 'when the school cannot be found' do
60-
before do
61-
allow(Sentry).to receive(:capture_exception)
62-
allow(School).to receive(:find).with(school.id).and_raise(ActiveRecord::RecordNotFound)
63-
service.verify
64-
end
65-
66-
it 'reports the error in Sentry' do
67-
expect(Sentry).to have_received(:capture_exception)
68-
end
69-
end
7058
end
7159

7260
describe '#reject' do

0 commit comments

Comments
 (0)