2020-10-14 12:12:32 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-23 15:19:08 +02:00
|
|
|
class Report < ApplicationRecord
|
2022-10-28 14:47:43 +02:00
|
|
|
ID_PREFIX = 'RP'
|
2022-12-05 13:17:15 +04:00
|
|
|
include PrefixedIdModel
|
2022-10-28 14:47:43 +02:00
|
|
|
SEARCHABLE_ATTRIBUTES = ['reports.name', 'reports.description', PREFIXED_ID_SQL].freeze
|
|
|
|
|
2021-04-08 17:40:16 +02:00
|
|
|
include SettingsModel
|
2022-05-23 13:32:15 +02:00
|
|
|
include Assignable
|
|
|
|
include PermissionCheckableModel
|
2016-02-12 16:52:43 +01:00
|
|
|
include SearchableModel
|
2019-03-25 17:35:54 +01:00
|
|
|
include SearchableByNameModel
|
2016-02-12 16:52:43 +01:00
|
|
|
|
2021-05-17 15:07:45 +02:00
|
|
|
enum pdf_file_status: { pdf_empty: 0, pdf_processing: 1, pdf_ready: 2, pdf_error: 3 }
|
|
|
|
enum docx_file_status: { docx_empty: 0, docx_processing: 1, docx_ready: 2, docx_error: 3 }
|
2021-05-11 13:28:28 +02:00
|
|
|
|
2021-03-15 13:30:03 +01:00
|
|
|
# ActiveStorage configuration
|
|
|
|
has_one_attached :pdf_file
|
|
|
|
has_one_attached :docx_file
|
2021-05-25 15:14:50 +02:00
|
|
|
has_one_attached :docx_preview_file
|
2021-03-15 13:30:03 +01:00
|
|
|
|
2016-09-21 15:35:23 +02:00
|
|
|
auto_strip_attributes :name, :description, nullify: false
|
2016-02-12 16:52:43 +01:00
|
|
|
validates :name,
|
2016-10-05 17:45:20 +02:00
|
|
|
length: { minimum: Constants::NAME_MIN_LENGTH,
|
2021-05-12 14:56:22 +02:00
|
|
|
maximum: Constants::NAME_MAX_LENGTH }
|
2016-10-05 17:45:20 +02:00
|
|
|
validates :description, length: { maximum: Constants::TEXT_MAX_LENGTH }
|
2016-02-12 16:52:43 +01:00
|
|
|
validates :project, presence: true
|
|
|
|
validates :user, presence: true
|
|
|
|
|
2019-07-26 12:40:36 +02:00
|
|
|
belongs_to :project, inverse_of: :reports
|
|
|
|
belongs_to :user, inverse_of: :reports
|
2018-04-18 16:47:52 +02:00
|
|
|
belongs_to :team, inverse_of: :reports
|
2017-06-28 15:21:32 +02:00
|
|
|
belongs_to :last_modified_by,
|
|
|
|
foreign_key: 'last_modified_by_id',
|
|
|
|
class_name: 'User',
|
|
|
|
optional: true
|
2022-05-23 13:32:15 +02:00
|
|
|
has_many :users, through: :user_assignments
|
2021-04-06 13:56:24 +02:00
|
|
|
has_many :report_template_values, dependent: :destroy
|
2016-02-12 16:52:43 +01:00
|
|
|
|
|
|
|
# Report either has many report elements (if grouped by timestamp),
|
|
|
|
# or many module elements (if grouped by module)
|
2021-05-27 16:31:41 +02:00
|
|
|
has_many :report_elements, inverse_of: :report, dependent: :delete_all
|
2016-02-12 16:52:43 +01:00
|
|
|
|
2021-04-08 17:40:16 +02:00
|
|
|
DEFAULT_SETTINGS = {
|
|
|
|
all_tasks: true,
|
|
|
|
task: {
|
|
|
|
protocol: {
|
|
|
|
description: true,
|
|
|
|
completed_steps: true,
|
|
|
|
uncompleted_steps: true,
|
2022-06-01 15:53:40 +02:00
|
|
|
step_texts: true,
|
2021-04-08 17:40:16 +02:00
|
|
|
step_checklists: true,
|
|
|
|
step_files: true,
|
|
|
|
step_tables: true,
|
2023-05-10 16:15:14 +02:00
|
|
|
step_comments: true,
|
|
|
|
step_well_plates: true
|
2021-04-08 17:40:16 +02:00
|
|
|
},
|
2021-05-13 15:44:06 +02:00
|
|
|
file_results: true,
|
2021-04-08 17:40:16 +02:00
|
|
|
file_results_previews: false,
|
|
|
|
table_results: true,
|
|
|
|
text_results: true,
|
|
|
|
result_comments: true,
|
2022-12-01 17:13:26 +01:00
|
|
|
result_order: 'new',
|
2022-09-13 09:59:11 +02:00
|
|
|
activities: true,
|
|
|
|
repositories: []
|
2021-04-08 17:40:16 +02:00
|
|
|
}
|
|
|
|
}.freeze
|
|
|
|
|
2016-02-12 16:52:43 +01:00
|
|
|
def self.search(
|
|
|
|
user,
|
|
|
|
include_archived,
|
|
|
|
query = nil,
|
2017-05-05 16:41:23 +02:00
|
|
|
page = 1,
|
|
|
|
_current_team = nil,
|
|
|
|
options = {}
|
2016-02-12 16:52:43 +01:00
|
|
|
)
|
|
|
|
|
2021-10-22 11:43:20 +02:00
|
|
|
project_ids = Project.search(user, include_archived, nil, Constants::SEARCH_NO_LIMIT)
|
|
|
|
.pluck(:id)
|
2016-02-12 16:52:43 +01:00
|
|
|
|
2021-10-22 11:43:20 +02:00
|
|
|
new_query = Report.distinct
|
|
|
|
.where(reports: { project_id: project_ids })
|
2022-10-28 14:47:43 +02:00
|
|
|
.where_attributes_like(SEARCHABLE_ATTRIBUTES, query, options)
|
2016-02-12 16:52:43 +01:00
|
|
|
|
|
|
|
# Show all results if needed
|
2016-10-05 17:45:20 +02:00
|
|
|
if page == Constants::SEARCH_NO_LIMIT
|
2016-02-12 16:52:43 +01:00
|
|
|
new_query
|
|
|
|
else
|
2021-10-22 11:43:20 +02:00
|
|
|
new_query.limit(Constants::SEARCH_LIMIT).offset((page - 1) * Constants::SEARCH_LIMIT)
|
2016-02-12 16:52:43 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-25 17:35:54 +01:00
|
|
|
def self.viewable_by_user(user, teams)
|
2022-10-20 15:35:43 +02:00
|
|
|
with_granted_permissions(user, ReportPermissions::READ)
|
|
|
|
.where(project: Project.viewable_by_user(user, teams))
|
2018-12-13 11:18:09 +01:00
|
|
|
end
|
|
|
|
|
2022-07-07 12:00:35 +02:00
|
|
|
def self.filter_by_teams(teams = [])
|
|
|
|
teams.blank? ? self : where(team: teams)
|
|
|
|
end
|
|
|
|
|
2022-06-06 18:21:57 +02:00
|
|
|
def created_by
|
|
|
|
user
|
|
|
|
end
|
|
|
|
|
|
|
|
def permission_parent
|
|
|
|
team
|
|
|
|
end
|
|
|
|
|
2016-02-12 16:52:43 +01:00
|
|
|
def root_elements
|
2021-05-27 16:31:41 +02:00
|
|
|
report_elements.active.where(parent: nil).order(:position)
|
2016-02-12 16:52:43 +01:00
|
|
|
end
|
|
|
|
|
2018-09-16 21:00:09 +02:00
|
|
|
def self.generate_whole_project_report(project, current_user, current_team)
|
2021-05-24 13:16:43 +02:00
|
|
|
content = {
|
2021-05-27 12:44:45 +02:00
|
|
|
'experiments' => [],
|
2021-06-16 16:17:12 +02:00
|
|
|
'repositories' => project.assigned_repositories_and_snapshots.pluck(:id)
|
2021-05-24 13:16:43 +02:00
|
|
|
}
|
|
|
|
project.experiments.includes(:my_modules).each do |experiment|
|
2021-06-22 14:50:47 +02:00
|
|
|
content['experiments'].push(
|
|
|
|
{ id: experiment.id, my_module_ids: experiment.my_module_ids }
|
|
|
|
)
|
2021-05-24 13:16:43 +02:00
|
|
|
end
|
2018-09-16 21:00:09 +02:00
|
|
|
|
2023-07-21 16:44:03 +02:00
|
|
|
report = Report.new(skip_user_assignments: true)
|
2018-10-12 05:59:45 +02:00
|
|
|
report.name = loop do
|
2018-09-16 21:00:09 +02:00
|
|
|
dummy_name = SecureRandom.hex(10)
|
2021-06-08 14:21:34 +02:00
|
|
|
break dummy_name unless Report.exists?(name: dummy_name)
|
2018-09-16 21:00:09 +02:00
|
|
|
end
|
|
|
|
report.project = project
|
|
|
|
report.user = current_user
|
|
|
|
report.team = current_team
|
|
|
|
report.last_modified_by = current_user
|
2021-05-24 13:16:43 +02:00
|
|
|
ReportActions::ReportContent.new(report, content, {}, current_user).save_with_content
|
2018-09-16 21:00:09 +02:00
|
|
|
report
|
|
|
|
end
|
2016-02-12 16:52:43 +01:00
|
|
|
end
|