diff --git a/app/helpers/reports_helper.rb b/app/helpers/reports_helper.rb index 9d0ae9387..5f9106c99 100644 --- a/app/helpers/reports_helper.rb +++ b/app/helpers/reports_helper.rb @@ -138,19 +138,8 @@ module ReportsHelper end def assigned_repositories_in_project_list(project) - live_repositories = Repository.accessible_by_teams(project.team) - .joins(repository_rows: - { my_module_repository_rows: { my_module: { experiment: :project } } }) - .where(repository_rows: - { my_module_repository_rows: { my_module: { experiments: { project: project } } } }) - .select(:id, :name) - - snapshots = RepositorySnapshot.joins(my_module: { experiment: :project }) - .where('experiments.project_id = ?', project.id) - .left_outer_joins(:original_repository) - .where(original_repositories_repositories: { id: nil }) - .select('DISTINCT ON ("repositories"."parent_id") "repositories".*') - .order(:parent_id, updated_at: :desc) + live_repositories = Repository.assigned_to_project(project) + snapshots = RepositorySnapshot.assigned_to_project(project) snapshots.each { |snapshot| snapshot.name = "#{snapshot.name} #{t('projects.reports.index.deleted')}" } (live_repositories + snapshots).sort_by { |r| r.name.downcase } diff --git a/app/models/project.rb b/app/models/project.rb index 7b42f9d71..501927bd3 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -210,6 +210,12 @@ class Project < ApplicationRecord }) end + def assigned_repositories_and_snapshots + live_repositories = Repository.assigned_to_project(self) + snapshots = RepositorySnapshot.assigned_to_project(self) + (live_repositories + snapshots).sort_by { |r| r.name.downcase } + end + def my_modules_ids ids = active_experiments.map do |exp| exp.my_modules.pluck(:id) if exp.my_modules diff --git a/app/models/report.rb b/app/models/report.rb index f9f63db38..87d49b5e4 100644 --- a/app/models/report.rb +++ b/app/models/report.rb @@ -131,17 +131,11 @@ class Report < ApplicationRecord result_children) end - module_children += - gen_element_content(my_module, nil, 'my_module_activity', true, 'asc') - module_children += - gen_element_content(my_module, - my_module.repository_rows.select(:repository_id) - .distinct.map(&:repository), - 'my_module_repository', true, 'asc') + repositories = project.assigned_repositories_and_snapshots - modules += - gen_element_content(my_module, nil, 'my_module', true, nil, - module_children) + module_children += gen_element_content(my_module, nil, 'my_module_activity', true, 'asc') + module_children += gen_element_content(my_module, repositories, 'my_module_repository', true, 'asc') + modules += gen_element_content(my_module, nil, 'my_module', true, nil, module_children) end report_contents += diff --git a/app/models/repository.rb b/app/models/repository.rb index 05723a28a..0df694b54 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -50,6 +50,12 @@ class Repository < RepositoryBase .distinct } + scope :assigned_to_project, lambda { |project| + accessible_by_teams(project.team) + .joins(repository_rows: { my_module_repository_rows: { my_module: { experiment: :project } } }) + .where(repository_rows: { my_module_repository_rows: { my_module: { experiments: { project: project } } } }) + } + def self.within_global_limits? return true unless Rails.configuration.x.global_repositories_limit.positive? diff --git a/app/models/repository_snapshot.rb b/app/models/repository_snapshot.rb index 37208b4f0..942e82f4a 100644 --- a/app/models/repository_snapshot.rb +++ b/app/models/repository_snapshot.rb @@ -17,6 +17,15 @@ class RepositorySnapshot < RepositoryBase validates :status, presence: true validate :only_one_selected_for_my_module, if: ->(obj) { obj.changed.include? 'selected' } + scope :assigned_to_project, lambda { |project| + joins(my_module: { experiment: :project }) + .where('experiments.project_id = ?', project.id) + .left_outer_joins(:original_repository) + .where(original_repositories_repositories: { id: nil }) + .select('DISTINCT ON ("repositories"."parent_id") "repositories".*') + .order(:parent_id, updated_at: :desc) + } + def default_columns_count Constants::REPOSITORY_SNAPSHOT_TABLE_DEFAULT_STATE['length'] end diff --git a/app/models/team_zip_export.rb b/app/models/team_zip_export.rb index c31f59b8f..e3e07c169 100644 --- a/app/models/team_zip_export.rb +++ b/app/models/team_zip_export.rb @@ -59,17 +59,11 @@ class TeamZipExport < ZipExport inventories = "#{project_path}/Inventories" FileUtils.mkdir_p(inventories) - # Find all assigned inventories through all tasks in the project - task_ids = p.project_my_modules - repo_rows = RepositoryRow.joins(:my_modules) - .where(my_modules: { id: task_ids }) - .distinct + repositories = p.assigned_repositories_and_snapshots # Iterate through every inventory repo and save it to CSV - repo_rows.map(&:repository).uniq.each_with_index do |repo, repo_idx| - curr_repo_rows = repo_rows.select { |x| x.repository_id == repo.id } - obj_filenames[:my_module_repository][repo.id] = - save_inventories_to_csv(inventories, repo, curr_repo_rows, repo_idx) + repositories.each_with_index do |repo, repo_idx| + obj_filenames[:my_module_repository][repo.id] = save_inventories_to_csv(inventories, repo, repo_idx) end # Include all experiments @@ -233,7 +227,7 @@ class TeamZipExport < ZipExport end # Helper method for saving inventories to CSV - def save_inventories_to_csv(path, repo, repo_rows, idx) + def save_inventories_to_csv(path, repo, idx) repo_name = "#{to_filesystem_name(repo.name)} (#{idx})" # Attachment folder @@ -263,14 +257,13 @@ class TeamZipExport < ZipExport end # Generate CSV - csv_data = RepositoryZipExport.to_csv(repo_rows, col_ids, @user, @team, - handle_name_func) + csv_data = RepositoryZipExport.to_csv(repo.repository_rows, col_ids, @user, @team, handle_name_func) File.open(csv_file, 'wb') { |f| f.write(csv_data) } # Save all attachments (it doesn't work directly in callback function assets.each do |asset, asset_path| asset.file.open do |file| - FileUtils.cp(file.path, asset_path) + FileUtils.cp(file.path, asset_path) end end