Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions app/controllers/courses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def show
authorize @course

# query for all projects_instances, owner_type and owner_id for participants table
@course.projects.includes(project_instances: { enrolment: :user }).load
@course.projects.includes(project_instances: { supervisor_enrolment: :user }).load
@projects_by_owner = @course.projects.index_by { |p| [p.owner_type, p.owner_id] }

@student_list = @course.students
Expand Down Expand Up @@ -43,10 +43,10 @@ def show
if @current_user_enrolment&.coordinator?
supervisor_enrolment = @lecturer_enrolment || @current_user_enrolment
@my_student_projects = @course.projects.supervised_by(supervisor_enrolment).approved
@incoming_proposals = @course.projects.where(enrolment: supervisor_enrolment).proposals
@incoming_proposals = @course.projects.where(supervisor_enrolment: supervisor_enrolment).proposals
elsif @current_user_enrolment&.lecturer?
@my_student_projects = @course.projects.supervised_by(@current_user_enrolment).approved
@incoming_proposals = @course.projects.where(enrolment: @current_user_enrolment).proposals
@incoming_proposals = @course.projects.where(supervisor_enrolment: @current_user_enrolment).proposals
end

# view instances for participants_table
Expand Down Expand Up @@ -262,7 +262,7 @@ def profile
@course = Course.find(params[:id])
@grouped = @course.grouped

@course.projects.includes(project_instances: { enrolment: :user }).load
@course.projects.includes(project_instances: { supervisor_enrolment: :user }).load
@projects_by_owner = @course.projects.index_by { |p| [p.owner_type, p.owner_id] }

if @participant_type == 'group'
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def create
@project = Project.create!(
course: @course,
owner: @course.grouped? ? group : current_user,
enrolment: supervisor_enrolment
supervisor_enrolment: supervisor_enrolment
)

# Get title
Expand All @@ -176,7 +176,7 @@ def create
version: 1,
title: title_value,
created_by: current_user,
enrolment: supervisor_enrolment,
supervisor_enrolment: supervisor_enrolment,
source_topic: topic || nil,
last_edit_time: Time.current,
last_edit_by: current_user.id
Expand Down
11 changes: 4 additions & 7 deletions app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Project < ApplicationRecord
enum :ownership_type, { student: 0, project_group: 1, lecturer: 2 }
default_scope { where(ownership_type: %i[student project_group]) }

belongs_to :enrolment
belongs_to :supervisor_enrolment, class_name: 'Enrolment', foreign_key: 'enrolment_id'
belongs_to :course
belongs_to :owner, polymorphic: true

Expand All @@ -23,7 +23,7 @@ class Project < ApplicationRecord
scope :proposals, -> { where(status: %i[pending redo rejected]) }

# Enrolment (supervisor) filters
scope :supervised_by, ->(enrolment) { where(enrolment: enrolment) }
scope :supervised_by, ->(supervisor_enrolment) { where(supervisor_enrolment: supervisor_enrolment) }

scope :owned_by_user_or_groups, lambda { |user, groups|
where(owner: [user] + groups.to_a)
Expand All @@ -36,10 +36,7 @@ class Project < ApplicationRecord
STATUS_SORT_ORDER = { 'rejected' => 0, 'redo' => 1, 'pending' => 2, 'not_submitted' => 3, 'approved' => 4 }.freeze

def supervisor
return nil unless enrolment_id.present?

enrolment = Enrolment.find_by(id: enrolment_id)
enrolment&.user
self.supervisor_enrolment.user
end

def member
Expand Down Expand Up @@ -75,7 +72,7 @@ def instance_to_edit(created_by:, has_supervisor_comment:)
project_instances.build(
version: (project_instances.maximum(:version) || 0) + 1,
created_by: created_by,
enrolment: enrolment,
supervisor_enrolment: supervisor_enrolment,
title: current_title,
status: (status if approved?)
)
Expand Down
6 changes: 3 additions & 3 deletions app/models/project_instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class ProjectInstance < ApplicationRecord
default_scope { where(project_instance_type: :project) }

belongs_to :project
belongs_to :enrolment
belongs_to :supervisor_enrolment, class_name: 'Enrolment', foreign_key: 'enrolment_id'

has_many :comments, as: :location, dependent: :destroy

Expand All @@ -23,7 +23,7 @@ class ProjectInstance < ApplicationRecord
validates :title, presence: true

def supervisor
enrolment&.user
self.supervisor_enrolment.user
end

private
Expand All @@ -33,7 +33,7 @@ def update_parent_project

project.update(
status: status,
enrolment: enrolment
supervisor_enrolment: supervisor_enrolment
)
end

Expand Down
44 changes: 22 additions & 22 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -604,56 +604,56 @@

group_1_project = Project.create!(
course: course_with_groups,
enrolment: lecturer_1_lecturer_enrolment,
supervisor_enrolment: lecturer_1_lecturer_enrolment,
owner: group_1,
ownership_type: :project_group
)

group_2_project = Project.create!(
course: course_with_groups,
enrolment: lecturer_1_lecturer_enrolment,
supervisor_enrolment: lecturer_1_lecturer_enrolment,
owner: group_2,
ownership_type: :project_group
)

group_3_project = Project.create!(
course: course_with_groups,
enrolment: lecturer_1_lecturer_enrolment,
supervisor_enrolment: lecturer_1_lecturer_enrolment,
owner: group_3,
ownership_type: :project_group
)

group_4_project = Project.create!(
course: course_with_groups,
enrolment: lecturer_1_lecturer_enrolment,
supervisor_enrolment: lecturer_1_lecturer_enrolment,
owner: group_4,
ownership_type: :project_group
)

student_1_project = Project.create!(
course: course_no_groups,
enrolment: lecturer_1_lecturer_enrolment_no_groups,
supervisor_enrolment: lecturer_1_lecturer_enrolment_no_groups,
owner: student1,
ownership_type: :student
)

student_2_project = Project.create!(
course: course_no_groups,
enrolment: lecturer_1_lecturer_enrolment_no_groups,
supervisor_enrolment: lecturer_1_lecturer_enrolment_no_groups,
owner: student2,
ownership_type: :student
)

student_3_project = Project.create!(
course: course_no_groups,
enrolment: lecturer_1_lecturer_enrolment_no_groups,
supervisor_enrolment: lecturer_1_lecturer_enrolment_no_groups,
owner: student3,
ownership_type: :student
)

student_4_project = Project.create!(
course: course_no_groups,
enrolment: lecturer_1_lecturer_enrolment_no_groups,
supervisor_enrolment: lecturer_1_lecturer_enrolment_no_groups,
owner: student4,
ownership_type: :student
)
Expand Down Expand Up @@ -809,7 +809,7 @@
created_by: student1,
title: 'Difficult Group Project 1',
status: :pending,
enrolment: lecturer_2_lecturer_enrolment
supervisor_enrolment: lecturer_2_lecturer_enrolment
)

group_1_instance_2 = ProjectInstance.create!(
Expand All @@ -818,7 +818,7 @@
created_by: student1,
title: 'Difficult Group Project 2',
status: :redo,
enrolment: lecturer_2_lecturer_enrolment
supervisor_enrolment: lecturer_2_lecturer_enrolment
)

group_1_instance_3 = ProjectInstance.create!(
Expand All @@ -827,7 +827,7 @@
created_by: student1,
title: 'Difficult Group Project 3',
status: :rejected,
enrolment: lecturer_3_lecturer_enrolment
supervisor_enrolment: lecturer_3_lecturer_enrolment
)

group_1_instance_4 = ProjectInstance.create!(
Expand All @@ -836,7 +836,7 @@
created_by: student1,
title: 'Difficult Group Project 4',
status: :approved,
enrolment: lecturer_1_lecturer_enrolment
supervisor_enrolment: lecturer_1_lecturer_enrolment
)

group_2_instance_1 = ProjectInstance.create!(
Expand All @@ -845,7 +845,7 @@
created_by: student4,
title: 'Difficult Group Project Group 2',
status: :rejected,
enrolment: lecturer_1_lecturer_enrolment
supervisor_enrolment: lecturer_1_lecturer_enrolment
)

group_3_instance_1 = ProjectInstance.create!(
Expand All @@ -854,7 +854,7 @@
created_by: student7,
title: 'Difficult Group Project Group 3',
status: :redo,
enrolment: lecturer_1_lecturer_enrolment
supervisor_enrolment: lecturer_1_lecturer_enrolment
)

group_4_instance_1 = ProjectInstance.create!(
Expand All @@ -863,7 +863,7 @@
created_by: student10,
title: 'Difficult Group Project Group 4',
status: :pending,
enrolment: lecturer_1_lecturer_enrolment
supervisor_enrolment: lecturer_1_lecturer_enrolment
)

student_1_project_instance_1 = ProjectInstance.create!(
Expand All @@ -872,7 +872,7 @@
created_by: student1,
title: 'Student 1 Project',
status: :pending,
enrolment: lecturer_3_lecturer_enrolment_no_groups
supervisor_enrolment: lecturer_3_lecturer_enrolment_no_groups
)

student_1_project_instance_2 = ProjectInstance.create!(
Expand All @@ -881,7 +881,7 @@
created_by: student1,
title: 'Student 1 Project 2',
status: :rejected,
enrolment: lecturer_3_lecturer_enrolment_no_groups
supervisor_enrolment: lecturer_3_lecturer_enrolment_no_groups
)

student_1_project_instance_3 = ProjectInstance.create!(
Expand All @@ -890,7 +890,7 @@
created_by: student1,
title: 'Student 1 Project 3',
status: :redo,
enrolment: lecturer_2_lecturer_enrolment_no_groups
supervisor_enrolment: lecturer_2_lecturer_enrolment_no_groups
)

student_1_project_instance_4 = ProjectInstance.create!(
Expand All @@ -899,7 +899,7 @@
created_by: student1,
title: 'Student 1 Project 4',
status: :approved,
enrolment: lecturer_1_lecturer_enrolment_no_groups
supervisor_enrolment: lecturer_1_lecturer_enrolment_no_groups
)

student_2_project_instance_1 = ProjectInstance.create!(
Expand All @@ -908,7 +908,7 @@
created_by: student2,
title: 'Student 2 Project',
status: :pending,
enrolment: lecturer_1_lecturer_enrolment_no_groups
supervisor_enrolment: lecturer_1_lecturer_enrolment_no_groups
)

student_3_project_instance_1 = ProjectInstance.create!(
Expand All @@ -917,7 +917,7 @@
created_by: student3,
title: 'Student 3 Project',
status: :redo,
enrolment: lecturer_1_lecturer_enrolment_no_groups
supervisor_enrolment: lecturer_1_lecturer_enrolment_no_groups
)

student_4_project_instance_1 = ProjectInstance.create!(
Expand All @@ -926,7 +926,7 @@
created_by: student4,
title: 'Student 4 Project',
status: :rejected,
enrolment: lecturer_1_lecturer_enrolment_no_groups
supervisor_enrolment: lecturer_1_lecturer_enrolment_no_groups
)

# Create Project Instance Fields
Expand Down
2 changes: 1 addition & 1 deletion test/factories/project_instances.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FactoryBot.define do
factory :project_instance do
association :project
association :enrolment
association :supervisor_enrolment, factory: :enrolment
association :created_by, factory: :user
version { 1 }
status { :pending }
Expand Down
2 changes: 1 addition & 1 deletion test/factories/projects.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FactoryBot.define do
factory :project do
association :course
association :enrolment
association :supervisor_enrolment, factory: :enrolment
owner { association :user }
owner_type { 'User' }
status { :pending }
Expand Down
18 changes: 9 additions & 9 deletions test/models/project_instance_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ class ProjectInstanceTest < ActiveSupport::TestCase
@course = FactoryBot.create(:course)
@user = FactoryBot.create(:user, is_staff: false)
@enrolment = FactoryBot.create(:enrolment, user: @user, course: @course, role: :student)
@project = FactoryBot.create(:project, course: @course, owner: @user, enrolment: @enrolment)
@project = FactoryBot.create(:project, course: @course, owner: @user, supervisor_enrolment: @enrolment)
end

test 'project instance created with happy paths' do
instance = ProjectInstance.create!(
project: @project,
enrolment: @enrolment,
supervisor_enrolment: @enrolment,
created_by: @user,
version: 1,
title: 'Test Project Title'
Expand All @@ -25,7 +25,7 @@ class ProjectInstanceTest < ActiveSupport::TestCase
test 'saving project instance updates the parent project status happy path' do
ProjectInstance.create!(
project: @project,
enrolment: @enrolment,
supervisor_enrolment: @enrolment,
created_by: @user,
version: 1,
title: 'Test Project Title'
Expand All @@ -36,12 +36,12 @@ class ProjectInstanceTest < ActiveSupport::TestCase

test 'saving an older instance does not overwrite parent project status sad path' do
instance_v1 = ProjectInstance.create!(
project: @project, enrolment: @enrolment,
project: @project, supervisor_enrolment: @enrolment,
created_by: @user, version: 1,
title: 'Test Project Title', status: :pending
)
ProjectInstance.create!(
project: @project, enrolment: @enrolment,
project: @project, supervisor_enrolment: @enrolment,
created_by: @user, version: 2,
title: 'Test Project Title 2', status: :approved
)
Expand All @@ -53,12 +53,12 @@ class ProjectInstanceTest < ActiveSupport::TestCase

test 'current_instance returns the instance with the highest version' do
ProjectInstance.create!(
project: @project, enrolment: @enrolment,
project: @project, supervisor_enrolment: @enrolment,
created_by: @user, version: 1,
title: 'Test Project Title'
)
instance_v2 = ProjectInstance.create!(
project: @project, enrolment: @enrolment,
project: @project, supervisor_enrolment: @enrolment,
created_by: @user, version: 2,
title: 'Test Project Title 2'
)
Expand All @@ -82,13 +82,13 @@ class ProjectInstanceTest < ActiveSupport::TestCase

test 'cannot create two instances with the same version for the same project' do
ProjectInstance.create!(
project: @project, enrolment: @enrolment,
project: @project, supervisor_enrolment: @enrolment,
created_by: @user, version: 1, title: 'First'
)

assert_raises ActiveRecord::RecordNotUnique do
ProjectInstance.create!(
project: @project, enrolment: @enrolment,
project: @project, supervisor_enrolment: @enrolment,
created_by: @user, version: 1, title: 'Duplicate'
)
end
Expand Down
Loading
Loading