summaryrefslogtreecommitdiff
path: root/app/models/wiki_page.rb
diff options
context:
space:
mode:
authorAlex Braha Stoll <alexbrahastoll@gmail.com>2016-12-19 02:34:35 -0200
committerAlex Braha Stoll <alexbrahastoll@gmail.com>2016-12-31 16:55:50 -0200
commitf25344e36e9c1b0d0df2211b82b26c6515e96c31 (patch)
treeabbcce871499f4bec7fe737289def2ff5943683a /app/models/wiki_page.rb
parent7f914ec73f9bdb3d2d4e51d48906a36186f496e3 (diff)
downloadgitlab-ce-f25344e36e9c1b0d0df2211b82b26c6515e96c31.tar.gz
Change WikiPage.group_by_directory to order by directory and file alphabetical order
Diffstat (limited to 'app/models/wiki_page.rb')
-rw-r--r--app/models/wiki_page.rb29
1 files changed, 28 insertions, 1 deletions
diff --git a/app/models/wiki_page.rb b/app/models/wiki_page.rb
index a563b0b7a72..a84f84c67cd 100644
--- a/app/models/wiki_page.rb
+++ b/app/models/wiki_page.rb
@@ -12,10 +12,17 @@ class WikiPage
ActiveModel::Name.new(self, nil, 'wiki')
end
+ # Sorts and groups pages by directory.
+ #
+ # pages - an array of WikiPage objects.
+ #
+ # Returns a hash whose keys are directories and whose values are WikiPage
+ # arrays. See WikiPage.sort_by_directory for more info about the ordering.
def self.group_by_directory(pages)
return {} if pages.blank?
- directories = { '/' => [] }
+ pages = sort_by_directory(pages)
+ directories = {}
pages.each do |page|
directories[page.directory] ||= []
directories[page.directory] << page
@@ -199,6 +206,26 @@ class WikiPage
private
+ # Sorts an array of pages by directory and file alphabetical order.
+ # Pages at the root directory will come first. The next pages will be
+ # sorted by their directories. Within directories, pages are sorted by
+ # filename alphabetical order. Pages are sorted in such a fashion that
+ # nested directories will always follow their parents (e.g. pages in
+ # dir_1/nested_dir_1 will follow pages inside dir_1).
+ #
+ # pages - an array of WikiPage objects.
+ #
+ # Returns a sorted array of WikiPage objects.
+ def self.sort_by_directory(pages)
+ pages.sort do |page, next_page|
+ if page.directory == next_page.directory
+ page.slug <=> next_page.slug
+ else
+ page.directory <=> next_page.directory
+ end
+ end
+ end
+
def set_attributes
attributes[:slug] = @page.url_path
attributes[:title] = @page.title