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..e078ac50ce6d 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,62 @@ 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 = attribute_data_list(work_package) 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) + build_columns_table_cells(list[i]) + + build_columns_table_cells(list[i + 1]) end end - def build_attributes_table_rows(work_package) - # get work package attribute table rows data [[label, value, label, value]] - attrs = %i[ + def attribute_data_list(work_package) + list = if respond_to?(:column_objects) + attributes_list_by_columns + else + attributes_list_by_wp + 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 + column_objects + .reject { |column| column.name == :subject } + .map do |column| + { label: column.caption || '', name: column.name } + end + end + + def attributes_list_by_wp + 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| + { label: WorkPackage.human_attribute_name(col_name), 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(attribute_data) + return ['', ''] if attribute_data.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(attribute_data[:label].upcase, styles.wp_attributes_table_label_cell), + attribute_data[:value] ] end