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
|
|
|
|
2018-03-09 00:36:10 +08:00
|
|
|
def prepare_row_columns(repository_rows,
|
|
|
|
repository,
|
|
|
|
columns_mappings,
|
2019-10-25 21:57:03 +08:00
|
|
|
_team,
|
2019-12-06 18:12:07 +08:00
|
|
|
assigned_rows)
|
2018-03-06 23:32:39 +08:00
|
|
|
parsed_records = []
|
2019-11-21 06:08:23 +08:00
|
|
|
includes_json = { repository_cells: Extends::REPOSITORY_SEARCH_INCLUDES }
|
|
|
|
|
|
|
|
repository_rows.includes(includes_json).each do |record|
|
2018-03-06 23:32:39 +08:00
|
|
|
row = {
|
2019-10-09 18:53:41 +08:00
|
|
|
'DT_RowId': record.id,
|
|
|
|
'1': assigned_row(record, assigned_rows),
|
|
|
|
'2': record.id,
|
|
|
|
'3': escape_input(record.name),
|
|
|
|
'4': I18n.l(record.created_at, format: :full),
|
|
|
|
'5': escape_input(record.created_by.full_name),
|
|
|
|
'recordEditUrl': Rails.application.routes.url_helpers
|
|
|
|
.edit_repository_repository_row_path(
|
|
|
|
repository,
|
|
|
|
record.id
|
|
|
|
),
|
|
|
|
'recordUpdateUrl': Rails.application.routes.url_helpers
|
|
|
|
.repository_repository_row_path(
|
|
|
|
repository,
|
|
|
|
record.id
|
|
|
|
),
|
|
|
|
'recordInfoUrl': Rails.application.routes.url_helpers
|
|
|
|
.repository_row_path(record.id)
|
|
|
|
}
|
2018-03-06 23:32:39 +08:00
|
|
|
|
|
|
|
# Add custom columns
|
|
|
|
record.repository_cells.each do |cell|
|
|
|
|
row[columns_mappings[cell.repository_column.id]] =
|
2019-12-06 18:12:07 +08:00
|
|
|
display_cell_value(cell)
|
2018-03-06 23:32:39 +08:00
|
|
|
end
|
|
|
|
parsed_records << row
|
|
|
|
end
|
|
|
|
parsed_records
|
|
|
|
end
|
|
|
|
|
2018-03-09 00:36:10 +08:00
|
|
|
def assigned_row(record, assigned_rows)
|
|
|
|
if assigned_rows&.include?(record)
|
2018-11-19 20:03:39 +08:00
|
|
|
"<span class='circle-icon'> </span>"
|
2018-03-09 00:36:10 +08:00
|
|
|
else
|
2018-11-19 20:03:39 +08:00
|
|
|
"<span class='circle-icon disabled'> </span>"
|
2018-03-09 00:36:10 +08:00
|
|
|
end
|
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
|
|
|
|
2019-12-06 18:12:07 +08:00
|
|
|
def display_cell_value(cell)
|
2019-11-27 20:06:46 +08:00
|
|
|
"RepositoryDatatable::#{cell.repository_column.data_type}Serializer"
|
2019-12-06 18:33:30 +08:00
|
|
|
.constantize.new(cell).serializable_hash
|
2019-10-09 18:53:41 +08:00
|
|
|
end
|
2018-11-19 20:03:39 +08:00
|
|
|
end
|