From 7e0a9bb092ba0caf962c55c5bdf7c89531d48740 Mon Sep 17 00:00:00 2001 From: Andrej Date: Sun, 17 Nov 2024 12:35:15 +0100 Subject: [PATCH] Fix generating of reports [SCI-11291][SCI-11292] --- app/controllers/reports_controller.rb | 20 ++++++++++++-------- app/helpers/reports_helper.rb | 18 +++++++++++------- app/models/report.rb | 4 +++- 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/app/controllers/reports_controller.rb b/app/controllers/reports_controller.rb index 3a0dcbc73..3b97e8b00 100644 --- a/app/controllers/reports_controller.rb +++ b/app/controllers/reports_controller.rb @@ -9,6 +9,7 @@ class ReportsController < ApplicationController before_action :load_vars_nested, only: %i(create edit update generate_pdf generate_docx new_template_values new_docx_template_values project_contents) before_action :load_wizard_vars, only: %i(new edit) + before_action :load_repositories_vars, only: %i(new edit create update) before_action :load_available_repositories, only: %i(index save_pdf_to_inventory_modal available_repositories) before_action :check_project_read_permissions, only: %i(create edit update generate_pdf generate_docx new_template_values new_docx_template_values project_contents) @@ -355,13 +356,6 @@ class ReportsController < ApplicationController def load_wizard_vars @templates = Extends::REPORT_TEMPLATES @docx_templates = Extends::DOCX_REPORT_TEMPLATES - live_repositories = Repository.viewable_by_user(current_user).sort_by { |r| r.name.downcase } - snapshots_of_deleted = RepositorySnapshot.left_outer_joins(:original_repository) - .where(team: current_team) - .where.not(original_repository: live_repositories) - .select('DISTINCT ON ("repositories"."parent_id") "repositories".*') - .sort_by { |r| r.name.downcase } - @repositories = live_repositories + snapshots_of_deleted @visible_projects = current_team.projects .active .joins(experiments: :my_modules) @@ -375,6 +369,16 @@ class ReportsController < ApplicationController @default_docx_template = Extends::DOCX_REPORT_TEMPLATES.keys.first.to_s if Extends::DOCX_REPORT_TEMPLATES.one? && custom_templates(Extends::DOCX_REPORT_TEMPLATES) end + def load_repositories_vars + live_repositories = Repository.viewable_by_user(current_user).sort_by { |r| r.name.downcase } + snapshots_of_deleted = RepositorySnapshot.left_outer_joins(:original_repository) + .where(team: current_team) + .where.not(original_repository: live_repositories) + .select('DISTINCT ON ("repositories"."parent_id") "repositories".*') + .sort_by { |r| r.name.downcase } + @repositories = live_repositories + snapshots_of_deleted + end + def check_project_read_permissions render_403 unless can_read_project?(@project) end @@ -407,7 +411,7 @@ class ReportsController < ApplicationController def report_params params.require(:report) - .permit(:name, :description, :grouped_by, :report_contents, settings: permit_report_settings_structure(Report::DEFAULT_SETTINGS)) + .permit(:name, :description, :grouped_by, :report_contents, settings: permit_report_settings_structure(Report::DEFAULT_SETTINGS, @repositories)) end def search_params diff --git a/app/helpers/reports_helper.rb b/app/helpers/reports_helper.rb index 47a49e6cf..7d7df14cc 100644 --- a/app/helpers/reports_helper.rb +++ b/app/helpers/reports_helper.rb @@ -107,15 +107,19 @@ module ReportsHelper end end - def permit_report_settings_structure(settings_definition) + def permit_report_settings_structure(settings_definition, repositories) settings_definition.each_with_object([]) do |(key, value), permitted| - permitted << case value - when Hash - { key => permit_report_settings_structure(value) } - when Array - { key => [] } + permitted << if key == :excluded_repository_columns && repositories.present? + { key => repositories.each_with_object({}) { |repository, hash| hash[repository.id.to_s] = [] } } else - key + case value + when Hash + { key => permit_report_settings_structure(value, repositories) } + when Array + { key => [] } + else + key + end end end end diff --git a/app/models/report.rb b/app/models/report.rb index 61f092e33..87d159e6a 100644 --- a/app/models/report.rb +++ b/app/models/report.rb @@ -66,7 +66,9 @@ class Report < ApplicationRecord activities: true, repositories: [], excluded_repository_columns: {} - } + }, + template: 'scinote_template', + docx_template: 'scinote_template' }.freeze def self.search(