Skip to content

Commit 5c4bbbd

Browse files
authored
Prepare for generating school codes (#348)
2 parents 71ed6df + 2ce285f commit 5c4bbbd

File tree

22 files changed

+354
-62
lines changed

22 files changed

+354
-62
lines changed

app/controllers/admin/schools_controller.rb

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,28 @@
22

33
module Admin
44
class SchoolsController < Admin::ApplicationController
5-
def authorized_action?(resource, action)
6-
case action
7-
when :verify_school
8-
resource&.rejected_at.present? || resource&.verified_at.nil?
9-
when :reject_school
10-
resource&.verified_at.present? || resource&.rejected_at.nil?
11-
else
12-
super
13-
end
14-
end
15-
16-
def verify_school
17-
school_id = params[:school_id]
18-
service = SchoolVerificationService.new(school_id)
5+
def verify
6+
service = SchoolVerificationService.new(requested_resource)
197

208
if service.verify
219
flash[:notice] = t('administrate.controller.verify_school.success')
2210
else
2311
flash[:error] = t('administrate.controller.verify_school.error')
2412
end
2513

26-
redirect_to admin_school_path(id: school_id)
14+
redirect_to admin_school_path(requested_resource)
2715
end
2816

29-
def reject_school
30-
school_id = params[:school_id]
31-
service = SchoolVerificationService.new(school_id)
17+
def reject
18+
service = SchoolVerificationService.new(requested_resource)
3219

3320
if service.reject
3421
flash[:notice] = t('administrate.controller.reject_school.success')
3522
else
3623
flash[:error] = t('administrate.controller.reject_school.error')
3724
end
3825

39-
redirect_to admin_school_path(id: school_id)
26+
redirect_to admin_school_path(requested_resource)
4027
end
4128

4229
def default_sorting_attribute

app/models/school.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ class School < ApplicationRecord
1717
validates :creator_id, presence: true, uniqueness: true
1818
validates :creator_agree_authority, presence: true, acceptance: true
1919
validates :creator_agree_terms_and_conditions, presence: true, acceptance: true
20+
validates :rejected_at, absence: { if: proc { |school| school.verified? } }
21+
validates :verified_at, absence: { if: proc { |school| school.rejected? } }
22+
validate :verified_at_cannot_be_changed
23+
validate :rejected_at_cannot_be_changed
2024

2125
before_validation :normalize_reference
2226

@@ -31,10 +35,34 @@ def creator
3135
User.from_userinfo(ids: creator_id).first
3236
end
3337

38+
def verified?
39+
verified_at.present?
40+
end
41+
42+
def rejected?
43+
rejected_at.present?
44+
end
45+
46+
def verify!
47+
update!(verified_at: Time.zone.now)
48+
end
49+
50+
def reject
51+
update(rejected_at: Time.zone.now)
52+
end
53+
3454
private
3555

3656
# Ensure the reference is nil, not an empty string
3757
def normalize_reference
3858
self.reference = nil if reference.blank?
3959
end
60+
61+
def verified_at_cannot_be_changed
62+
errors.add(:verified_at, 'cannot be changed after verification') if verified_at_was.present? && verified_at_changed?
63+
end
64+
65+
def rejected_at_cannot_be_changed
66+
errors.add(:rejected_at, 'cannot be changed after rejection') if rejected_at_was.present? && rejected_at_changed?
67+
end
4068
end

app/models/teacher_invitation.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class TeacherInvitation < ApplicationRecord
1616
private
1717

1818
def school_is_verified
19-
return if school.verified_at
19+
return if school.verified?
2020

2121
errors.add(:school, 'is not verified')
2222
end
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
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)
11-
school.update(verified_at: Time.zone.now, rejected_at: nil)
13+
school.verify!
1214
Role.owner.create(user_id: school.creator_id, school:)
1315
Role.teacher.create(user_id: school.creator_id, school:)
1416
end
1517
rescue StandardError => e
1618
Sentry.capture_exception(e)
17-
Rails.logger.error { "Failed to verify school #{school_id}: #{e.message}" }
19+
Rails.logger.error { "Failed to verify school #{@school_id}: #{e.message}" }
1820
false
1921
else
2022
true
2123
end
24+
# rubocop:enable Metrics/AbcSize
2225

23-
def reject
24-
school = School.find(@school_id)
25-
school.update(verified_at: nil, rejected_at: Time.zone.now)
26-
end
26+
delegate :reject, to: :school
2727
end

app/views/admin/schools/show.html.erb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,21 @@ as well as a link to its edit page.
3232

3333
<%= link_to(
3434
t("administrate.actions.verify_school"),
35-
admin_school_verify_school_path(page.resource),
35+
verify_admin_school_path(page.resource),
3636
class: "button button--verify",
3737
method: :post,
3838
style: "background-color: green; margin: 5px;",
3939
data: { confirm: t("administrate.actions.confirm_verify_school") }
40-
) if accessible_action?(page.resource, :verify_school) %>
40+
) unless page.resource.verified? || page.resource.rejected? %>
4141

4242
<%= link_to(
4343
t("administrate.actions.reject_school"),
44-
admin_school_reject_school_path(page.resource),
44+
reject_admin_school_path(page.resource),
4545
class: "button button--danger",
4646
method: :patch,
4747
style: "background-color: hsla(0, 88%, 33%, 1); margin: 5px 5px 5px 0;",
4848
data: { confirm: t("administrate.actions.confirm_reject_school") }
49-
) if accessible_action?(page.resource, :reject_school) %>
49+
) unless page.resource.verified? || page.resource.rejected? %>
5050
</div>
5151
</header>
5252

config/routes.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
end
1212

1313
resources :schools, only: %i[index show edit update] do
14-
post :verify_school, action: :verify_school
15-
patch :reject_school, action: :reject_school
14+
member do
15+
post :verify
16+
patch :reject
17+
end
1618
end
1719

1820
resources :school_classes, only: %i[show]

lib/concepts/school_owner/invite.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def call(school:, school_owner_params:, token:)
1818
def invite_owner(school, school_owner_params, token)
1919
email_address = school_owner_params.fetch(:email_address)
2020

21-
raise ArgumentError, 'school is not verified' unless school.verified_at
21+
raise ArgumentError, 'school is not verified' unless school.verified?
2222
raise ArgumentError, "email address '#{email_address}' is invalid" unless EmailValidator.valid?(email_address)
2323

2424
ProfileApiClient.invite_school_owner(token:, email_address:, organisation_id: school.id)

lib/concepts/school_student/create.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def create_student(school, school_student_params, token)
2727
end
2828

2929
def validate(school:, username:, password:, name:)
30-
raise ArgumentError, 'school is not verified' unless school.verified_at
30+
raise ArgumentError, 'school is not verified' unless school.verified?
3131
raise ArgumentError, "username '#{username}' is invalid" if username.blank?
3232
raise ArgumentError, "password '#{password}' is invalid" if password.size < 8
3333
raise ArgumentError, "name '#{name}' is invalid" if name.blank?

lib/concepts/school_student/create_batch.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def create_batch(school, uploaded_file, token)
3030
def validate(school:, sheet:)
3131
expected_header = ['Student Name', 'Username', 'Password']
3232

33-
raise ArgumentError, 'school is not verified' unless school.verified_at
33+
raise ArgumentError, 'school is not verified' unless school.verified?
3434
raise ArgumentError, 'the spreadsheet header row is invalid' unless sheet.row(1) == expected_header
3535

3636
@errors = []

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/concepts/school_owner/invite_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
RSpec.describe SchoolOwner::Invite, type: :unit do
66
let(:token) { UserProfileMock::TOKEN }
7-
let(:school) { create(:school, verified_at: Time.zone.now) }
7+
let(:school) { create(:verified_school) }
88
let(:owner_id) { SecureRandom.uuid }
99

1010
let(:school_owner_params) do

spec/concepts/school_student/create_batch_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
RSpec.describe SchoolStudent::CreateBatch, type: :unit do
66
let(:token) { UserProfileMock::TOKEN }
7-
let(:school) { create(:school, verified_at: Time.zone.now) }
7+
let(:school) { create(:verified_school) }
88
let(:file) { fixture_file_upload('students.csv') }
99

1010
before do

spec/concepts/school_student/create_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
RSpec.describe SchoolStudent::Create, type: :unit do
66
let(:token) { UserProfileMock::TOKEN }
7-
let(:school) { create(:school, verified_at: Time.zone.now) }
7+
let(:school) { create(:verified_school) }
88

99
let(:school_student_params) do
1010
{

spec/concepts/school_teacher/invite_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
RSpec.describe SchoolTeacher::Invite, type: :unit do
66
let(:token) { UserProfileMock::TOKEN }
7-
let(:school) { create(:school, verified_at: Time.zone.now) }
7+
let(:school) { create(:verified_school) }
88
let(:teacher_id) { SecureRandom.uuid }
99

1010
let(:school_teacher_params) do

0 commit comments

Comments
 (0)