Skip to content

Commit c1f5481

Browse files
Change creator_id to only need to be unique for schools where rejecte… (#742)
## Status - [Closes](RaspberryPiFoundation/digital-editor-issues#1233) ## What's changed? - Update school creation validation so that `creator_id` can be reused if `rejected_at` is not nil
1 parent a2eb1f6 commit c1f5481

4 files changed

Lines changed: 36 additions & 4 deletions

File tree

app/models/school.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ class School < ApplicationRecord
3131
format: { with: /\A[0-9]+[A-Z]+\z/, allow_nil: true, message: I18n.t('validations.school.school_roll_number') },
3232
presence: true,
3333
if: :ireland?
34-
validates :creator_id, presence: true, uniqueness: true
34+
validates :creator_id,
35+
presence: true,
36+
uniqueness: {
37+
conditions: -> { where(rejected_at: nil) }
38+
}
3539
validates :creator_agree_authority, presence: true, acceptance: true
3640
validates :creator_agree_terms_and_conditions, presence: true, acceptance: true
3741
validates :creator_agree_responsible_safeguarding, presence: true, acceptance: true
@@ -52,7 +56,7 @@ class School < ApplicationRecord
5256
after_create :generate_code!
5357

5458
def self.find_for_user!(user)
55-
school = Role.find_by(user_id: user.id)&.school || find_by(creator_id: user.id)
59+
school = Role.find_by(user_id: user.id)&.school || find_by(creator_id: user.id, rejected_at: nil)
5660
raise ActiveRecord::RecordNotFound unless school
5761

5862
school
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class UpdateCreatorIdIndexOnSchools < ActiveRecord::Migration[7.2]
2+
def up
3+
remove_index :schools, name: "index_schools_on_creator_id"
4+
5+
add_index :schools,
6+
:creator_id,
7+
unique: true,
8+
where: "rejected_at IS NULL",
9+
name: "index_schools_on_creator_id_active_only"
10+
end
11+
12+
def down
13+
remove_index :schools, name: "index_schools_on_creator_id_active_only"
14+
15+
add_index :schools,
16+
:creator_id,
17+
unique: true,
18+
name: "index_schools_on_creator_id"
19+
end
20+
end

db/schema.rb

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/models/school_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,14 @@
483483
user = build(:user)
484484
expect { described_class.find_for_user!(user) }.to raise_error(ActiveRecord::RecordNotFound)
485485
end
486+
487+
it('raises ActiveRecord::RecordNotFound if the user is the creator of a rejected school') do
488+
creator = create(:user)
489+
school.update!(creator_id: creator.id)
490+
school.reject
491+
492+
expect { described_class.find_for_user!(creator) }.to raise_error(ActiveRecord::RecordNotFound)
493+
end
486494
end
487495

488496
describe '#verified?' do

0 commit comments

Comments
 (0)