2019-10-09 18:53:41 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-06 23:32:39 +08:00
|
|
|
module RepositoryDatatableHelper
|
|
|
|
include InputSanitizeHelper
|
2019-10-25 20:30:45 +08:00
|
|
|
|
2020-04-22 21:21:04 +08:00
|
|
|
def prepare_row_columns(repository_rows, repository, columns_mappings, team, options = {})
|
|
|
|
repository_rows.map do |record|
|
2018-03-06 23:32:39 +08:00
|
|
|
row = {
|
2019-10-09 18:53:41 +08:00
|
|
|
'DT_RowId': record.id,
|
2020-05-07 18:51:18 +08:00
|
|
|
'1': assigned_row(record),
|
2019-10-09 18:53:41 +08:00
|
|
|
'2': record.id,
|
|
|
|
'3': escape_input(record.name),
|
|
|
|
'4': I18n.l(record.created_at, format: :full),
|
|
|
|
'5': escape_input(record.created_by.full_name),
|
2020-05-18 20:12:06 +08:00
|
|
|
'recordInfoUrl': Rails.application.routes.url_helpers.repository_repository_row_path(repository, record)
|
2019-10-09 18:53:41 +08:00
|
|
|
}
|
2018-03-06 23:32:39 +08:00
|
|
|
|
2020-04-07 18:36:01 +08:00
|
|
|
unless options[:view_mode]
|
2020-05-18 20:12:06 +08:00
|
|
|
row['recordUpdateUrl'] =
|
|
|
|
Rails.application.routes.url_helpers.repository_repository_row_path(repository, record)
|
|
|
|
row['recordEditable'] = record.editable?
|
2018-03-06 23:32:39 +08:00
|
|
|
end
|
|
|
|
|
2020-04-29 01:12:17 +08:00
|
|
|
row['0'] = record[:row_assigned] if options[:my_module]
|
2020-04-22 23:55:22 +08:00
|
|
|
|
2020-05-12 22:26:44 +08:00
|
|
|
# Add custom columns
|
|
|
|
record.repository_cells.each do |cell|
|
|
|
|
row[columns_mappings[cell.repository_column.id]] =
|
|
|
|
display_cell_value(cell, team)
|
2020-04-07 18:36:01 +08:00
|
|
|
end
|
2020-05-12 22:26:44 +08:00
|
|
|
|
2020-04-22 21:21:04 +08:00
|
|
|
row
|
2020-04-07 01:52:41 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-12 22:26:44 +08:00
|
|
|
def prepare_simple_view_row_columns(repository_rows)
|
|
|
|
repository_rows.map do |record|
|
|
|
|
{
|
|
|
|
'DT_RowId': record.id,
|
2020-05-21 22:51:07 +08:00
|
|
|
'0': escape_input(record.name),
|
2020-05-18 20:12:06 +08:00
|
|
|
'recordInfoUrl': Rails.application.routes.url_helpers.repository_repository_row_path(record.repository, record)
|
2020-05-12 22:26:44 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def prepare_snapshot_row_columns(repository_rows, columns_mappings, team)
|
2020-04-22 21:21:04 +08:00
|
|
|
repository_rows.map do |record|
|
2020-04-21 20:49:36 +08:00
|
|
|
row = {
|
|
|
|
'DT_RowId': record.id,
|
|
|
|
'1': record.parent_id,
|
|
|
|
'2': escape_input(record.name),
|
|
|
|
'3': I18n.l(record.created_at, format: :full),
|
|
|
|
'4': escape_input(record.created_by.full_name),
|
2020-05-18 20:12:06 +08:00
|
|
|
'recordInfoUrl': Rails.application.routes.url_helpers.repository_repository_row_path(record.repository, record)
|
2020-04-21 20:49:36 +08:00
|
|
|
}
|
|
|
|
|
2020-05-12 22:26:44 +08:00
|
|
|
# Add custom columns
|
|
|
|
record.repository_cells.each do |cell|
|
|
|
|
row[columns_mappings[cell.repository_column.id]] = display_cell_value(cell, team)
|
2020-04-21 20:49:36 +08:00
|
|
|
end
|
2020-05-12 22:26:44 +08:00
|
|
|
|
2020-04-22 21:21:04 +08:00
|
|
|
row
|
2020-04-21 20:49:36 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-07 18:51:18 +08:00
|
|
|
def assigned_row(record)
|
2020-04-24 21:43:10 +08:00
|
|
|
{
|
|
|
|
tasks: record.assigned_my_modules_count,
|
2020-01-23 20:12:58 +08:00
|
|
|
experiments: record.assigned_experiments_count,
|
2020-04-24 21:43:10 +08:00
|
|
|
projects: record.assigned_projects_count,
|
2020-05-07 18:51:18 +08:00
|
|
|
task_list_url: assigned_task_list_repository_repository_row_path(record.repository, record)
|
2020-04-24 21:43:10 +08:00
|
|
|
}
|
2018-03-06 23:32:39 +08:00
|
|
|
end
|
2018-04-13 19:11:29 +08:00
|
|
|
|
|
|
|
def can_perform_repository_actions(repository)
|
2019-07-17 22:00:49 +08:00
|
|
|
can_read_repository?(repository) ||
|
|
|
|
can_manage_repository?(repository) ||
|
2019-07-12 22:43:54 +08:00
|
|
|
can_create_repositories?(repository.team) ||
|
|
|
|
can_manage_repository_rows?(repository)
|
2018-04-13 19:11:29 +08:00
|
|
|
end
|
2018-04-20 15:22:23 +08:00
|
|
|
|
2019-11-20 22:05:44 +08:00
|
|
|
def default_table_order_as_js_array
|
|
|
|
Constants::REPOSITORY_TABLE_DEFAULT_STATE['order'].to_json
|
|
|
|
end
|
|
|
|
|
2018-04-20 15:22:23 +08:00
|
|
|
def default_table_columns
|
2019-11-19 23:12:20 +08:00
|
|
|
Constants::REPOSITORY_TABLE_DEFAULT_STATE['columns'].to_json
|
2018-04-20 15:22:23 +08:00
|
|
|
end
|
2019-10-09 18:53:41 +08:00
|
|
|
|
2020-04-21 20:49:36 +08:00
|
|
|
def default_snapshot_table_order_as_js_array
|
|
|
|
Constants::REPOSITORY_SNAPSHOT_TABLE_DEFAULT_STATE['order'].to_json
|
|
|
|
end
|
|
|
|
|
|
|
|
def default_snapshot_table_columns
|
|
|
|
Constants::REPOSITORY_SNAPSHOT_TABLE_DEFAULT_STATE['columns'].to_json
|
|
|
|
end
|
|
|
|
|
2020-01-10 19:56:57 +08:00
|
|
|
def display_cell_value(cell, team)
|
2020-01-24 18:33:30 +08:00
|
|
|
value_name = cell.repository_column.data_type.demodulize.underscore
|
2020-01-13 23:09:07 +08:00
|
|
|
serializer_class = "RepositoryDatatable::#{cell.repository_column.data_type}Serializer".constantize
|
|
|
|
serializer_class.new(
|
|
|
|
cell.__send__(value_name),
|
|
|
|
scope: { team: team, user: current_user, column: cell.repository_column }
|
|
|
|
).serializable_hash
|
2019-10-09 18:53:41 +08:00
|
|
|
end
|
2018-11-19 20:03:39 +08:00
|
|
|
end
|