Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c2100aa

Browse files
committedJan 13, 2025·
When sorting models for navigation, fallback to model name
In my project, several STI models have the same label stored in their translation file. This is because, from the user's perspective, they represent the same abstract concept, even if the developers know differently. This can sometimes be an issue when rendering the main navigation. As the labels are equal, the listed models can be displayed in either order. It would be better to always display models in a single consistent and deterministic order. This is especially an issue for my project as the end-to-end tests rely on this order to assert correct behavior.
1 parent f4cf4b6 commit c2100aa

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed
 

‎lib/rails_admin/config.rb

+7-1
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,13 @@ def reload!
360360
def visible_models(bindings)
361361
visible_models_with_bindings(bindings).sort do |a, b|
362362
if (weight_order = a.weight <=> b.weight) == 0
363-
a.label.casecmp(b.label)
363+
result = a.label.casecmp(b.label)
364+
if result.zero? && a.try(:model) && b.try(:model)
365+
# If two models have the same label, fallback to the model name
366+
# for a deterministic ordering.
367+
result = a.model.name.casecmp(b.model.name)
368+
end
369+
result
364370
else
365371
weight_order
366372
end

0 commit comments

Comments
 (0)
Please sign in to comment.