diff --git a/app/controllers/course_editions/project_preferences_controller.rb b/app/controllers/course_editions/project_preferences_controller.rb index 5e787092bad747235dcdb806752660f982da40be..d8752ab24f6235343021004f9db20883ba110251 100644 --- a/app/controllers/course_editions/project_preferences_controller.rb +++ b/app/controllers/course_editions/project_preferences_controller.rb @@ -23,12 +23,12 @@ module CourseEditions # If the user has any preferences stored @any_preferences = current_user.project_preferences?(@course_edition) - if @course_edition.recurrent_edition.nil? - @other_preferences = false - else - @other_preferences = ProjectPreference.where(user: current_user, - course_edition: CourseEdition.where(recurrent_edition: @course_edition.recurrent_edition)).any? - end + @other_preferences = if @course_edition.recurrent_edition.nil? + false + else + ProjectPreference.where(user: current_user, + course_edition: CourseEdition.descendants_of(@course_edition.recurrent_edition)).any? + end @project_name = @course_edition.configuration.project_name_text(false, false) @projects_name = @course_edition.configuration.project_name_text(false, true) diff --git a/app/controllers/course_editions/projects_controller.rb b/app/controllers/course_editions/projects_controller.rb index 8052d2f6f5a6a734b1c471283190d62db694980d..4e6704937055cb8444d43ec833d473b562c22546 100644 --- a/app/controllers/course_editions/projects_controller.rb +++ b/app/controllers/course_editions/projects_controller.rb @@ -54,9 +54,10 @@ module CourseEditions ProjectInterest.joins(:project) .where(user: current_user, projects: { - course_edition_id: CourseEdition.where(recurrent_edition: @course_edition.recurrent_edition) + course_edition_id: CourseEdition.descendants_of(@course_edition.recurrent_edition) }) .any? + end apply_filters apply_ordering paginate_projects diff --git a/app/models/course_edition.rb b/app/models/course_edition.rb index 56a5dc0f0f0fd4c6bca501c4ffaa28e18d961334..3d5fe9cb99ffddcd418c9dcfd657b9952ed930f1 100644 --- a/app/models/course_edition.rb +++ b/app/models/course_edition.rb @@ -223,6 +223,11 @@ class CourseEdition < ApplicationRecord where(recurrent: false) }) + # Descendants of recurrent course edition + scope :descendants_of, lambda { |course_edition| + where(recurrent_edition: course_edition) + } + def display_name "#{course.name} #{name}" end diff --git a/app/models/project.rb b/app/models/project.rb index d22cd1e6cf5af4e6bbebd91bb4d37dd7f4afefdc..00c5a306ac55b094d651477cbbfc7fcdf915500a 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -84,7 +84,7 @@ class Project < ApplicationRecord before_update :unset_approved_at, if: (-> { status_changed?(to: 'rejected') }) def assign_to_recurrent_editions - CourseEdition.where(recurrent_edition_id: course_edition.id).find_each do |course_edition| + CourseEdition.descendants_of(course_edition).find_each do |course_edition| if periods.select { |period| period.id == course_edition.active_period.id }.any? copy_project(course_edition) end @@ -412,7 +412,7 @@ class Project < ApplicationRecord def set_approved_at self.approved_at = updated_at unless !course_edition.recurrent || - Project.where(original_project: self, course_edition: CourseEdition.where(recurrent_edition: course_edition)).any? + Project.where(original_project: self, course_edition: CourseEdition.descendants_of(course_edition)).any? assign_to_recurrent_editions end end diff --git a/app/views/admin/course_editions/show/_periods.html.erb b/app/views/admin/course_editions/show/_periods.html.erb index 288878693102d51ca1dcf2a568581642712a421e..6817510364be86b144f2c1e7928e3546eb0f94b5 100644 --- a/app/views/admin/course_editions/show/_periods.html.erb +++ b/app/views/admin/course_editions/show/_periods.html.erb @@ -188,8 +188,8 @@ <td><%= period.starts_at %></td> <td><%= period.ends_at %></td> <td> - <% if CourseEdition.where(recurrent_edition_id: @course_edition.id).where(active_period: period).any? %> - <% CourseEdition.where(recurrent_edition_id: @course_edition.id) + <% if CourseEdition.descendants_of(@course_edition).where(active_period: period).any? %> + <% CourseEdition.descendants_of(@course_edition) .where(active_period: period) .includes(:course) .each do |e| %> diff --git a/app/views/generic_projects/_form.html.erb b/app/views/generic_projects/_form.html.erb index a74c9df8685f0f1954dc3c433bc8088757c569f7..8a274ec336ba8456df8b6137e1e3e1ce79a06852 100644 --- a/app/views/generic_projects/_form.html.erb +++ b/app/views/generic_projects/_form.html.erb @@ -8,9 +8,8 @@ <br/> If you want the changes to also apply to current instances of the <%= @project_name %>, you can select them here: <%= f.collection_select :projects_modify, - Project.where(course_edition: CourseEdition.where(recurrent_edition_id: @specific_project.course_edition.id), original_project_id: @project.id) - .accessible_by(current_ability, :update) -, + Project.where(course_edition: CourseEdition.descendants_of(@specific_project.course_edition), original_project_id: @project.id) + .accessible_by(current_ability, :update), :id, ->(project) { "#{project.name} (#{project.course_edition.display_name})" }, { required: false, hide_label: true },