diff options
author | George Andrinopoulos <geoandri@gmail.com> | 2017-11-06 19:51:24 +0200 |
---|---|---|
committer | George Andrinopoulos <geoandri@gmail.com> | 2017-11-07 06:37:46 +0200 |
commit | f4ed780ef5fc76b7704742d4886ac435c3e5ab98 (patch) | |
tree | 7536ed460f72156638203606ce20dadff5305468 /lib/milestone_array.rb | |
parent | bba020a56cbedd6403b637ec7f9eaee258eb7c67 (diff) | |
download | gitlab-ce-f4ed780ef5fc76b7704742d4886ac435c3e5ab98.tar.gz |
Add Group Milestone sorting
Diffstat (limited to 'lib/milestone_array.rb')
-rw-r--r-- | lib/milestone_array.rb | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/lib/milestone_array.rb b/lib/milestone_array.rb new file mode 100644 index 00000000000..9b3f2acc123 --- /dev/null +++ b/lib/milestone_array.rb @@ -0,0 +1,40 @@ +module MilestoneArray + class << self + def sort(array, sort_method) + case sort_method + when 'due_date_asc' + sort_asc_nulls_last(array, 'due_date') + when 'due_date_desc' + sort_desc_nulls_last(array, 'due_date') + when 'start_date_asc' + sort_asc_nulls_last(array, 'start_date') + when 'start_date_desc' + sort_desc_nulls_last(array, 'start_date') + when 'name_asc' + sort_asc(array, 'title') + when 'name_desc' + sort_desc(array, 'title') + else + array + end + end + + private + + def sort_asc_nulls_last(array, attribute) + array.select(&attribute.to_sym).sort_by(&attribute.to_sym) + array.reject(&attribute.to_sym) + end + + def sort_desc_nulls_last(array, attribute) + array.select(&attribute.to_sym).sort_by(&attribute.to_sym).reverse + array.reject(&attribute.to_sym) + end + + def sort_asc(array, attribute) + array.sort_by(&attribute.to_sym) + end + + def sort_desc(array, attribute) + array.sort_by(&attribute.to_sym).reverse + end + end +end |