From e797bfcd38a17c0d6a5a91290d0e21fd46897c03 Mon Sep 17 00:00:00 2001 From: as-op Date: Wed, 12 Jul 2023 16:15:30 +0200 Subject: [PATCH 1/5] [#48649] Only show the attributes in the pdf export enabled for the work package type * refactor to avoid duplication in filtering https://community.openproject.org/work_packages/48649 --- .../pdf_export/work_package_detail.rb | 55 ++++++++----------- 1 file changed, 23 insertions(+), 32 deletions(-) diff --git a/app/models/work_package/pdf_export/work_package_detail.rb b/app/models/work_package/pdf_export/work_package_detail.rb index 57c961c684fa..75945e963d8f 100644 --- a/app/models/work_package/pdf_export/work_package_detail.rb +++ b/app/models/work_package/pdf_export/work_package_detail.rb @@ -74,11 +74,7 @@ def write_work_package_level!(level_path, text_style) end def write_attributes_table!(work_package) - rows = if respond_to?(:column_objects) - build_columns_table_rows(work_package) - else - build_attributes_table_rows(work_package) - end + rows = attribute_table_rows(work_package) with_margin(styles.wp_attributes_table_margins) do pdf.table( rows, @@ -95,54 +91,49 @@ def attributes_table_column_widths widths.map { |w| w * ratio } end - def build_columns_table_rows(work_package) - list = column_objects.reject { |column| column.name == :subject } + def attribute_table_rows(work_package) + list = if respond_to?(:column_objects) + attributes_list_by_columns(work_package) + else + attributes_list_by_wp(work_package) + end 0.step(list.length - 1, 2).map do |i| build_columns_table_cells(list[i], work_package) + build_columns_table_cells(list[i + 1], work_package) end end - def build_attributes_table_rows(work_package) - # get work package attribute table rows data [[label, value, label, value]] - attrs = %i[ + def attributes_list_by_columns(_work_package) + column_objects + .reject { |column| column.name == :subject } + .map { |column| [column.caption || '', column.name] } + end + + def attributes_list_by_wp(_work_package) + col_names = %i[ id updated_at type created_at status due_date - version - priority duration - work - category + priority assigned_to + responsible ] - 0.step(attrs.length - 1, 2).map do |i| - build_attributes_table_cells(attrs[i], work_package) + - build_attributes_table_cells(attrs[i + 1], work_package) + col_names.map do |col_name| + [WorkPackage.human_attribute_name(col_name), col_name] end end - def build_attributes_table_cells(attribute, work_package) - # get work package attribute table cell data: [label, value] - return ['', ''] if attribute.nil? - - build_attributes_row(WorkPackage.human_attribute_name(attribute) || '', attribute.to_sym, work_package) - end - - def build_columns_table_cells(column, work_package) - return ['', ''] if column.nil? - - build_attributes_row(column.caption || '', column.name, work_package) - end + def build_columns_table_cells(title_name_array, work_package) + return ['', ''] if title_name_array.nil? - def build_attributes_row(label, col_name, work_package) # get work package attribute table cell data: [label, value] [ - pdf.make_cell(label.upcase, styles.wp_attributes_table_label_cell), - get_column_value_cell(work_package, col_name) + pdf.make_cell(title_name_array[0].upcase, styles.wp_attributes_table_label_cell), + get_column_value_cell(work_package, title_name_array[1]) ] end From 67a51d9fbc7054bc7acde183d246354b16f419ca Mon Sep 17 00:00:00 2001 From: as-op Date: Tue, 25 Jul 2023 17:38:20 +0200 Subject: [PATCH 2/5] feat(pdf): filter out empty work package attributes --- .../pdf_export/work_package_detail.rb | 33 +++++++++++++------ 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/app/models/work_package/pdf_export/work_package_detail.rb b/app/models/work_package/pdf_export/work_package_detail.rb index 75945e963d8f..059768d206e8 100644 --- a/app/models/work_package/pdf_export/work_package_detail.rb +++ b/app/models/work_package/pdf_export/work_package_detail.rb @@ -92,21 +92,34 @@ def attributes_table_column_widths end def attribute_table_rows(work_package) + list = attribute_data_list(work_package) + 0.step(list.length - 1, 2).map do |i| + build_columns_table_cells(list[i]) + + build_columns_table_cells(list[i + 1]) + end + end + + def attribute_data_list(work_package) list = if respond_to?(:column_objects) attributes_list_by_columns(work_package) else attributes_list_by_wp(work_package) end - 0.step(list.length - 1, 2).map do |i| - build_columns_table_cells(list[i], work_package) + - build_columns_table_cells(list[i + 1], work_package) - end + list + .map { |entry| entry.merge({ value: get_column_value_cell(work_package, entry[:name]) }) } + .select { |attribute_data| can_show_attribute?(attribute_data) } + end + + def can_show_attribute?(attribute_data) + attribute_data[:value].present? end def attributes_list_by_columns(_work_package) column_objects .reject { |column| column.name == :subject } - .map { |column| [column.caption || '', column.name] } + .map do |column| + { label: column.caption || '', name: column.name } + end end def attributes_list_by_wp(_work_package) @@ -123,17 +136,17 @@ def attributes_list_by_wp(_work_package) responsible ] col_names.map do |col_name| - [WorkPackage.human_attribute_name(col_name), col_name] + { label: WorkPackage.human_attribute_name(col_name), name: col_name } end end - def build_columns_table_cells(title_name_array, work_package) - return ['', ''] if title_name_array.nil? + def build_columns_table_cells(attribute_data) + return ['', ''] if attribute_data.nil? # get work package attribute table cell data: [label, value] [ - pdf.make_cell(title_name_array[0].upcase, styles.wp_attributes_table_label_cell), - get_column_value_cell(work_package, title_name_array[1]) + pdf.make_cell(attribute_data[:label].upcase, styles.wp_attributes_table_label_cell), + attribute_data[:value] ] end From cd114e0c94b69a32227066ab75a42ed6d65f8ed4 Mon Sep 17 00:00:00 2001 From: Andrej Sandorf <77627197+as-op@users.noreply.github.com> Date: Thu, 27 Jul 2023 08:57:21 +0200 Subject: [PATCH 3/5] Update app/models/work_package/pdf_export/work_package_detail.rb Co-authored-by: Aaron Contreras <61627014+aaron-contreras@users.noreply.github.com> --- app/models/work_package/pdf_export/work_package_detail.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/work_package/pdf_export/work_package_detail.rb b/app/models/work_package/pdf_export/work_package_detail.rb index 059768d206e8..66ee76cbbbec 100644 --- a/app/models/work_package/pdf_export/work_package_detail.rb +++ b/app/models/work_package/pdf_export/work_package_detail.rb @@ -122,7 +122,7 @@ def attributes_list_by_columns(_work_package) end end - def attributes_list_by_wp(_work_package) + def attributes_list_by_wp col_names = %i[ id updated_at From d9f894bd13f39f1adeb2d8b189ceba348f0754a1 Mon Sep 17 00:00:00 2001 From: Andrej Sandorf <77627197+as-op@users.noreply.github.com> Date: Thu, 27 Jul 2023 08:57:29 +0200 Subject: [PATCH 4/5] Update app/models/work_package/pdf_export/work_package_detail.rb Co-authored-by: Aaron Contreras <61627014+aaron-contreras@users.noreply.github.com> --- app/models/work_package/pdf_export/work_package_detail.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/work_package/pdf_export/work_package_detail.rb b/app/models/work_package/pdf_export/work_package_detail.rb index 66ee76cbbbec..cb3194fdaf3f 100644 --- a/app/models/work_package/pdf_export/work_package_detail.rb +++ b/app/models/work_package/pdf_export/work_package_detail.rb @@ -114,7 +114,7 @@ def can_show_attribute?(attribute_data) attribute_data[:value].present? end - def attributes_list_by_columns(_work_package) + def attributes_list_by_columns column_objects .reject { |column| column.name == :subject } .map do |column| From fa84113cd9971c9a0a1f8b68a0d09cc8c9c8c694 Mon Sep 17 00:00:00 2001 From: Andrej Sandorf <77627197+as-op@users.noreply.github.com> Date: Thu, 27 Jul 2023 08:57:34 +0200 Subject: [PATCH 5/5] Update app/models/work_package/pdf_export/work_package_detail.rb Co-authored-by: Aaron Contreras <61627014+aaron-contreras@users.noreply.github.com> --- app/models/work_package/pdf_export/work_package_detail.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/work_package/pdf_export/work_package_detail.rb b/app/models/work_package/pdf_export/work_package_detail.rb index cb3194fdaf3f..e078ac50ce6d 100644 --- a/app/models/work_package/pdf_export/work_package_detail.rb +++ b/app/models/work_package/pdf_export/work_package_detail.rb @@ -101,9 +101,9 @@ def attribute_table_rows(work_package) def attribute_data_list(work_package) list = if respond_to?(:column_objects) - attributes_list_by_columns(work_package) + attributes_list_by_columns else - attributes_list_by_wp(work_package) + attributes_list_by_wp end list .map { |entry| entry.merge({ value: get_column_value_cell(work_package, entry[:name]) }) }