Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Extend `TableComponent` with an overridable sortable column mapping

Aaron Contreras00df7c6d64df
diff --git a/app/components/table_component.rb b/app/components/table_component.rb
index b1052e5775d..09174529be9 100644
--- a/app/components/table_component.rb
+++ b/app/components/table_component.rb
@@ -92,10 +92,25 @@ class TableComponent < ApplicationComponent
def initialize_sorted_model
helpers.sort_init *initial_sort.map(&:to_s)
- helpers.sort_update sortable_columns.map(&:to_s)
+ helpers.sort_update sortable_columns_correlation
@model = paginate_collection apply_sort(model)
end
+ ##
+ # If +initial_sort+ or +sortable_columns+ and the table column names in the database
+ # don't map 1 to 1, or there is an ambiguous column dilemma due
+ # to the joining of tables, these correlation methods can be extended by +TableComponent+
+ # subclasses to set the appropriate sort criteria for said edge-cases.
+ #
+ def initial_sort_correlation
+ initial_sort
+ end
+
+ def sortable_columns_correlation
+ sortable_columns.to_h { [_1.to_s, _1.to_s] }
+ .with_indifferent_access
+ end
+
def sort_criteria
helpers.instance_variable_get(:@sort_criteria)
end
@@ -155,7 +170,7 @@ class TableComponent < ApplicationComponent
end
def initial_order
- initial_sort.join(' ')
+ initial_sort_correlation.join(' ')
end
def paginated?