From 507443ba38ab7b8e59500a95dfca6219496af180 Mon Sep 17 00:00:00 2001 From: Urban Rotnik Date: Thu, 16 Jan 2020 14:42:26 +0100 Subject: [PATCH] Add custom method for repository report JSON data --- app/models/my_module.rb | 20 +++++++++++++++++++ .../reports/docx/draw_my_module_repository.rb | 4 ++-- .../reports/docx/repository_helper.rb | 4 ++-- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/app/models/my_module.rb b/app/models/my_module.rb index feb6b9e2c..486e30f10 100644 --- a/app/models/my_module.rb +++ b/app/models/my_module.rb @@ -413,6 +413,26 @@ class MyModule < ApplicationRecord { headers: headers, data: records } end + def repository_docx_json(repository_id) + headers = [ + I18n.t('repositories.table.id'), + I18n.t('repositories.table.row_name'), + I18n.t('repositories.table.added_on'), + I18n.t('repositories.table.added_by') + ] + custom_columns = [] + repository = Repository.find_by(id: repository_id) + return false unless repository + + repository.repository_columns.order(:id).each do |column| + headers.push(column.name) + custom_columns.push(column.id) + end + + records = repository_rows.where(repository_id: repository_id).select(:id, :name, :created_at, :created_by_id) + { headers: headers, rows: records, custom_columns: custom_columns } + end + def deep_clone(current_user) deep_clone_to_experiment(current_user, experiment) end diff --git a/app/services/reports/docx/draw_my_module_repository.rb b/app/services/reports/docx/draw_my_module_repository.rb index 654c80d03..f79118f87 100644 --- a/app/services/reports/docx/draw_my_module_repository.rb +++ b/app/services/reports/docx/draw_my_module_repository.rb @@ -6,8 +6,8 @@ module Reports::Docx::DrawMyModuleRepository return unless my_module repository_id = subject['id']['repository_id'] - repository_data = my_module.repository_json(repository_id, subject['sort_order'], @user) - return false unless repository_data[:data].any? + repository_data = my_module.repository_docx_json(repository_id) + return false unless repository_data[:rows].any? repository = ::Repository.find(repository_id) table = prepare_row_columns(repository_data) diff --git a/app/services/reports/docx/repository_helper.rb b/app/services/reports/docx/repository_helper.rb index 1d330f223..4843f6f3c 100644 --- a/app/services/reports/docx/repository_helper.rb +++ b/app/services/reports/docx/repository_helper.rb @@ -5,7 +5,7 @@ module Reports::Docx::RepositoryHelper def prepare_row_columns(repository_data) result = [repository_data[:headers]] - repository_data[:data].repository_rows.each do |record| + repository_data[:rows].each do |record| row = [] row.push(record.id) row.push(escape_input(record.name)) @@ -18,7 +18,7 @@ module Reports::Docx::RepositoryHelper cell_values[cell.repository_column_id] = cell.value.formatted end - repository_data[:data].mappings.each do |column_id, _position| + repository_data[:custom_columns].each do |column_id| value = cell_values[column_id] row.push(value) end