From 232e4d78bedbe8fb2426ad658905cfe6722d9c12 Mon Sep 17 00:00:00 2001 From: aignatov-bio Date: Fri, 25 Oct 2019 15:57:03 +0200 Subject: [PATCH] Add serializers to datatable values --- app/assets/javascripts/application.js.erb | 2 +- .../view_renderers.js} | 0 .../repositories/repository_datatable.js | 4 +-- app/helpers/repository_datatable_helper.rb | 31 +++---------------- .../repository_asset_value_serializer.rb | 19 ++++++++++++ .../repository_list_value_serializer.rb | 11 +++++++ .../repository_text_value_serializer.rb | 11 +++++++ 7 files changed, 48 insertions(+), 30 deletions(-) rename app/assets/javascripts/repositories/{renders/view_renders.js => renderers/view_renderers.js} (100%) create mode 100644 app/serializers/repository_datatable/repository_asset_value_serializer.rb create mode 100644 app/serializers/repository_datatable/repository_list_value_serializer.rb create mode 100644 app/serializers/repository_datatable/repository_text_value_serializer.rb diff --git a/app/assets/javascripts/application.js.erb b/app/assets/javascripts/application.js.erb index 4f6508088..fefa13e71 100644 --- a/app/assets/javascripts/application.js.erb +++ b/app/assets/javascripts/application.js.erb @@ -43,7 +43,7 @@ //= require global_activities/side_pane //= require protocols/header //= require marvinjslauncher -//= require_directory ./repositories/renders +//= require_directory ./repositories/renderers //= require turbolinks diff --git a/app/assets/javascripts/repositories/renders/view_renders.js b/app/assets/javascripts/repositories/renderers/view_renderers.js similarity index 100% rename from app/assets/javascripts/repositories/renders/view_renders.js rename to app/assets/javascripts/repositories/renderers/view_renderers.js diff --git a/app/assets/javascripts/repositories/repository_datatable.js b/app/assets/javascripts/repositories/repository_datatable.js index efee43df4..a55d798d2 100644 --- a/app/assets/javascripts/repositories/repository_datatable.js +++ b/app/assets/javascripts/repositories/repository_datatable.js @@ -511,8 +511,8 @@ var RepositoryDatatable = (function(global) { }, { targets: '_all', render: function(data) { - if (typeof data === 'object' && $.fn.dataTable.render[data.cell_type]) { - return $.fn.dataTable.render[data.cell_type](data); + if (typeof data === 'object' && $.fn.dataTable.render[data.value_type]) { + return $.fn.dataTable.render[data.value_type](data); } return data; } diff --git a/app/helpers/repository_datatable_helper.rb b/app/helpers/repository_datatable_helper.rb index ce26ad9d5..6ced02dcb 100644 --- a/app/helpers/repository_datatable_helper.rb +++ b/app/helpers/repository_datatable_helper.rb @@ -6,7 +6,7 @@ module RepositoryDatatableHelper def prepare_row_columns(repository_rows, repository, columns_mappings, - team, + _team, assigned_rows) parsed_records = [] repository_rows.each do |record| @@ -34,7 +34,7 @@ module RepositoryDatatableHelper # Add custom columns record.repository_cells.each do |cell| row[columns_mappings[cell.repository_column.id]] = - display_cell_value(cell, team) + display_cell_value(cell) end parsed_records << row end @@ -71,30 +71,7 @@ module RepositoryDatatableHelper end.to_json end - def display_cell_value(cell, team) - cell_type = cell.value_type.underscore - 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 + def display_cell_value(cell) + "RepositoryDatatable::#{cell.value_type}Serializer".constantize.new(cell).serializable_hash end end diff --git a/app/serializers/repository_datatable/repository_asset_value_serializer.rb b/app/serializers/repository_datatable/repository_asset_value_serializer.rb new file mode 100644 index 000000000..c82d308be --- /dev/null +++ b/app/serializers/repository_datatable/repository_asset_value_serializer.rb @@ -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 diff --git a/app/serializers/repository_datatable/repository_list_value_serializer.rb b/app/serializers/repository_datatable/repository_list_value_serializer.rb new file mode 100644 index 000000000..f963dc21b --- /dev/null +++ b/app/serializers/repository_datatable/repository_list_value_serializer.rb @@ -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 diff --git a/app/serializers/repository_datatable/repository_text_value_serializer.rb b/app/serializers/repository_datatable/repository_text_value_serializer.rb new file mode 100644 index 000000000..22664f0b5 --- /dev/null +++ b/app/serializers/repository_datatable/repository_text_value_serializer.rb @@ -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