Add serializers to datatable values

This commit is contained in:
aignatov-bio 2019-10-25 15:57:03 +02:00 committed by Urban Rotnik
parent 0f3c138a7e
commit 232e4d78be
7 changed files with 48 additions and 30 deletions

View file

@ -43,7 +43,7 @@
//= require global_activities/side_pane //= require global_activities/side_pane
//= require protocols/header //= require protocols/header
//= require marvinjslauncher //= require marvinjslauncher
//= require_directory ./repositories/renders //= require_directory ./repositories/renderers
//= require turbolinks //= require turbolinks

View file

@ -511,8 +511,8 @@ var RepositoryDatatable = (function(global) {
}, { }, {
targets: '_all', targets: '_all',
render: function(data) { render: function(data) {
if (typeof data === 'object' && $.fn.dataTable.render[data.cell_type]) { if (typeof data === 'object' && $.fn.dataTable.render[data.value_type]) {
return $.fn.dataTable.render[data.cell_type](data); return $.fn.dataTable.render[data.value_type](data);
} }
return data; return data;
} }

View file

@ -6,7 +6,7 @@ module RepositoryDatatableHelper
def prepare_row_columns(repository_rows, def prepare_row_columns(repository_rows,
repository, repository,
columns_mappings, columns_mappings,
team, _team,
assigned_rows) assigned_rows)
parsed_records = [] parsed_records = []
repository_rows.each do |record| repository_rows.each do |record|
@ -34,7 +34,7 @@ module RepositoryDatatableHelper
# Add custom columns # Add custom columns
record.repository_cells.each do |cell| record.repository_cells.each do |cell|
row[columns_mappings[cell.repository_column.id]] = row[columns_mappings[cell.repository_column.id]] =
display_cell_value(cell, team) display_cell_value(cell)
end end
parsed_records << row parsed_records << row
end end
@ -71,30 +71,7 @@ module RepositoryDatatableHelper
end.to_json end.to_json
end end
def display_cell_value(cell, team) def display_cell_value(cell)
cell_type = cell.value_type.underscore "RepositoryDatatable::#{cell.value_type}Serializer".constantize.new(cell).serializable_hash
data = if DisplayCellValue.respond_to? cell_type
DisplayCellValue.public_send(cell_type, cell, team)
else
custom_auto_link(display_tooltip(cell.value.data, Constants::NAME_MAX_LENGTH),
simple_format: true,
team: team)
end
{
cell_type: cell.value_type,
data: data
}
end
class DisplayCellValue
def self.repository_asset_value(cell, _team)
asset = cell.value.asset
{
id: asset.id,
url: Rails.application.routes.url_helpers.rails_blob_path(asset.file, disposition: 'attachment'),
preview_url: Rails.application.routes.url_helpers.asset_file_preview_path(asset),
file_name: asset.file_name
}
end
end end
end end

View file

@ -0,0 +1,19 @@
# frozen_string_literal: true
module RepositoryDatatable
class RepositoryAssetValueSerializer < ActiveModel::Serializer
include Rails.application.routes.url_helpers
attributes :data, :value_type
def data
asset = object.value.asset
{
id: asset.id,
url: rails_blob_path(asset.file, disposition: 'attachment'),
preview_url: asset_file_preview_path(asset),
file_name: asset.file_name
}
end
end
end

View file

@ -0,0 +1,11 @@
# frozen_string_literal: true
module RepositoryDatatable
class RepositoryListValueSerializer < ActiveModel::Serializer
attributes :data, :value_type
def data
object.value.data
end
end
end

View file

@ -0,0 +1,11 @@
# frozen_string_literal: true
module RepositoryDatatable
class RepositoryTextValueSerializer < ActiveModel::Serializer
attributes :data, :value_type
def data
object.value.data
end
end
end