Skip to content

Commit b34f10b

Browse files
tuzzfloehopper
authored andcommitted
Validate that the user association with a project matches the lesson (if present)
With regards to students creating projects relating to a lesson, I think they should just remix the project created by the teacher and not associate it with the lesson directly to make it easier to distinguish which is the actual project for a lesson, rather than a version created by a student.
1 parent 67576db commit b34f10b

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

app/models/project.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class Project < ApplicationRecord
1717
validate :identifier_cannot_be_taken_by_another_user
1818
validates :locale, presence: true, unless: :user_id
1919
validate :user_has_a_role_within_the_school
20+
validate :user_is_the_owner_of_the_lesson
2021

2122
scope :internal_projects, -> { where(user_id: nil) }
2223

@@ -57,9 +58,15 @@ def user_has_a_role_within_the_school
5758
_, user = with_user
5859

5960
return if user.blank?
60-
return if user.org_roles(organisation_id: school.id).any?
61+
return if user.org_roles(organisation_id: school_id).any?
6162

62-
msg = "'#{user_id}' does not have any roles for for organisation '#{school.id}'"
63+
msg = "'#{user_id}' does not have any roles for for organisation '#{school_id}'"
6364
errors.add(:user, msg)
6465
end
66+
67+
def user_is_the_owner_of_the_lesson
68+
return if !lesson || user_id == lesson.user_id
69+
70+
errors.add(:user, "'#{user_id}' is not the owner for lesson '#{lesson_id}'")
71+
end
6572
end

spec/models/lesson_spec.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@
3131
end
3232

3333
it 'has many projects' do
34-
lesson = create(:lesson, projects: [build(:project), build(:project)])
35-
expect(lesson.projects.size).to eq(2)
34+
user_id = SecureRandom.uuid
35+
lesson = create(:lesson, user_id:, projects: [build(:project, user_id:)])
36+
expect(lesson.projects.size).to eq(1)
3637
end
3738
end
3839

spec/models/project_spec.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,19 @@
7676
project.update!(school: create(:school))
7777
end
7878

79-
it 'requires a user that has a role within the school' do
79+
it 'requires that the user that has a role within the school' do
80+
project.user_id = SecureRandom.uuid
81+
expect(project).to be_invalid
82+
end
83+
end
84+
85+
context 'when the project has a lesson' do
86+
before do
87+
lesson = create(:lesson)
88+
project.update!(lesson:, user_id: lesson.user_id, identifier: 'something')
89+
end
90+
91+
it 'requires that the user be the owner of the lesson' do
8092
project.user_id = SecureRandom.uuid
8193
expect(project).to be_invalid
8294
end

0 commit comments

Comments
 (0)